Skip to content

Commit

Permalink
Service unavailable malformed json
Browse files Browse the repository at this point in the history
  • Loading branch information
Milan Matějček committed Jan 16, 2018
1 parent ab2f962 commit bca9308
Show file tree
Hide file tree
Showing 4 changed files with 59 additions and 9 deletions.
6 changes: 5 additions & 1 deletion src/FioRead.php
Expand Up @@ -28,6 +28,7 @@ public function __construct(Request\IQueue $queue, Account\FioAccount $account,
* @param string|int|\DateTime $from
* @param string|int|\DateTime $to
* @return TransactionList
* @throws ServiceUnavailableException
*/
public function movements($from = '-1 week', $to = 'now')
{
Expand All @@ -40,6 +41,7 @@ public function movements($from = '-1 week', $to = 'now')
* @param int $id
* @param int|string|NULL $year format YYYY, NULL is current
* @return IFile
* @throws ServiceUnavailableException
*/
public function movementId($id, $year = null)
{
Expand All @@ -53,6 +55,7 @@ public function movementId($id, $year = null)
/**
* Last movements from last breakpoint.
* @return IFile
* @throws ServiceUnavailableException
*/
public function lastDownload()
{
Expand All @@ -64,6 +67,7 @@ public function lastDownload()
* Set break point to id.
* @param int $moveId
* @return void
* @throws ServiceUnavailableException
*/
public function setLastId($moveId)
{
Expand All @@ -73,7 +77,7 @@ public function setLastId($moveId)
/**
* Set breakpoint to date.
* @param mixed $date
* @return void
* @throws ServiceUnavailableException
*/
public function setLastDate($date)
{
Expand Down
56 changes: 50 additions & 6 deletions src/Request/Queue.php
Expand Up @@ -56,14 +56,24 @@ public function setSleep($sleep)
$this->sleep = (bool) $sleep;
}

/**
* @param $token
* @param string $url
* @return mixed|string
* @throws Fio\QueueLimitException
* @throws Fio\ServiceUnavailableException
*/
public function download($token, $url)
{
return $this->request($token, function (GuzzleHttp\Client $client) use ($url) {
return $client->request('GET', $url, $this->downloadOptions);
});
}, 'download');
}

/** @return Pay\IResponse */
/**
* @return Pay\IResponse
* @throws Fio\QueueLimitException
*/
public function upload($url, $token, array $post, $filename)
{
$newPost = [];
Expand All @@ -72,14 +82,21 @@ public function upload($url, $token, array $post, $filename)
}
$newPost[] = ['name' => 'file', 'contents' => fopen($filename, 'r')];

/* @var $response GuzzleHttp\Psr7\Stream */
$response = $this->request($token, function (GuzzleHttp\Client $client) use ($url, $newPost) {
return $client->request('POST', $url, [GuzzleHttp\RequestOptions::MULTIPART => $newPost]);
});
return new Pay\XMLResponse($response->getContents());
}, 'upload');
return self::createXmlResponse($response);
}

private function request($token, $fallback)
/**
* @param $token
* @param $fallback
* @param string $action
* @return GuzzleHttp\Psr7\Stream
* @throws Fio\QueueLimitException
* @throws Fio\ServiceUnavailableException
*/
private function request($token, $fallback, $action)
{
$request = new GuzzleHttp\Client(['headers' => ['X-Powered-By' => 'h4kuna/fio']]);
$tempFile = $this->loadFileName($token);
Expand All @@ -104,9 +121,27 @@ private function request($token, $fallback)
} while ($next);
fclose($file);
touch($tempFile);

if ($action === 'download') {
self::detectDownloadResponse($response);
}

return $response->getBody();
}

private static function detectDownloadResponse($response)
{
/* @var $contentTypeHeaders array */
$contentTypeHeaders = $response->getHeader('Content-Type');
$contentType = array_shift($contentTypeHeaders);
if ($contentType === 'text/xml;charset=UTF-8') {
$xmlResponse = self::createXmlResponse($response);
if ($xmlResponse->getErrorCode() !== 0) {
throw new Fio\ServiceUnavailableException($xmlResponse->getError(), $xmlResponse->getErrorCode());
}
}
}

private static function sleep($filename)
{
$criticalTime = time() - filemtime($filename);
Expand Down Expand Up @@ -134,4 +169,13 @@ private static function safeProtocol($filename)
return Utils\SafeStream::PROTOCOL . '://' . $filename;
}

/**
* @param $response
* @return Pay\XMLResponse
*/
private static function createXmlResponse($response)
{
return new Pay\XMLResponse($response->getContents());
}

}
4 changes: 2 additions & 2 deletions src/Response/Pay/XMLResponse.php
Expand Up @@ -21,7 +21,7 @@ public function __construct($xml)

public function isOk()
{
return $this->getError() == 'ok' && $this->getErrorCode() == 0;
return $this->getError() === 'ok' && $this->getErrorCode() === 0;
}

/**
Expand All @@ -38,7 +38,7 @@ public function getXml()
/** @return int */
public function getErrorCode()
{
return $this->getValue('result/errorCode');
return (int) $this->getValue('result/errorCode');
}

public function getIdInstruction()
Expand Down
2 changes: 2 additions & 0 deletions src/exceptions.php
Expand Up @@ -15,3 +15,5 @@ class QueueLimitException extends FioException {}
class FileUplodException extends FioException {}

class InvalidArgumentException extends FioException {}

class ServiceUnavailableException extends FioException {}

0 comments on commit bca9308

Please sign in to comment.