Skip to content

Commit

Permalink
Merge pull request #29 from pogromistik/master
Browse files Browse the repository at this point in the history
Fix throws
  • Loading branch information
SonicGD authored May 18, 2018
2 parents e5dc9dd + 4fb47c5 commit 5e2b9bf
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 18 deletions.
8 changes: 7 additions & 1 deletion src/DirectApiService.php
Original file line number Diff line number Diff line change
Expand Up @@ -432,7 +432,13 @@ public function doRequest(RequestInterface $request, int $try = 0, int $maxTry =
throw new DirectApiException('Ошибка при подключении к яндексу: ' . $exception->getMessage() . ' Code: ' . $exception->getCode());
}
} catch (RequestException $exception) {
throw new DirectApiException('Ошибка при отправке запроса к яндексу: ' . $exception->getMessage() . '. Response: ' . $exception->getResponse()->getBody()->getContents() . ' Code: ' . $exception->getCode(), 0, null, $exception->getResponse()->getBody()->getContents());
$response = $exception->getResponse();
if ($response) {
$response = $response->getBody()->getContents();
} else {
$response = "";
}
throw new DirectApiException('Ошибка при отправке запроса к яндексу: ' . $exception->getMessage() . '. Response: ' . $response . ' Code: ' . $exception->getCode(), 0, null, $exception->getResponse()->getBody()->getContents());
} catch (\Throwable $exception) {
throw new DirectApiException('Ошибка при запросе к яндексу' . $exception->getMessage() . ' Code: ' . $exception->getCode());
}
Expand Down
46 changes: 29 additions & 17 deletions src/services/reports/ReportsService.php
Original file line number Diff line number Diff line change
Expand Up @@ -89,9 +89,9 @@ public function getReport(
return $this->getReportResponse($this->getRequesJson($reportDefinition), $mode, $returnMoneyInMicros);
}

public function getRequesJson(ReportDefinition $reportDefinition)
public function getRequesJson(ReportDefinition $reportDefinition)
{
$jsonRequest = ["params"=>$reportDefinition];
$jsonRequest = ["params" => $reportDefinition];
return json_encode($jsonRequest);
}

Expand Down Expand Up @@ -142,26 +142,19 @@ public function getReportResponse($payload, $mode, $returnMoneyInMicros)
$result = null;
while ($code !== 200) {
try {
$result = $this->service->doRequest($request);
//отправка повторного запросы при пустом ответе
if ($result === null) {
$result = $this->service->doRequest($request);
}
$result = $this->getResult(0, $request);
} catch (DirectApiException $ex) {
if ($ex->response) {

$simpleXMLElementResponse = new SimpleXMLElement($ex->response, 0, false, 'http://api.direct.yandex.com/v5/reports');
$errorCode = (int)$simpleXMLElementResponse->xpath('//reports:errorCode')[0];
if ($errorCode === 1002) {
$exResp = json_decode($ex->response, true);
$errorCode = (int)$exResp['error']['error_code'];
if ($errorCode === 500) {
try {
$result = $this->service->doRequest($request);
//отправка повторного запросы при пустом ответе
if ($result === null) {
$result = $this->service->doRequest($request);
}
$result = $this->getResult(0, $request);
} catch (BadResponseException $ex) {
$this->badResponseExceptionAnswer($ex);
}
} else {
$this->badResponseExceptionAnswer($ex);
}
}

Expand All @@ -186,9 +179,28 @@ public function getReportResponse($payload, $mode, $returnMoneyInMicros)
return $this->parseReportResponse($body);
}

/**
* @param $attemptNumber
* @param $request
* @return \Psr\Http\Message\ResponseInterface
* @throws DirectApiException
*/
private function getResult($attemptNumber, $request)
{
while ($attemptNumber < 100) {
$result = $this->service->doRequest($request);
if ($result === null) {
sleep(1);
$this->getResult($attemptNumber + 1, $request);
}
return $result;
}
throw new DirectApiException('Не удалось получить отчёт после 100 попыток');
}

private function parseApiError($errorString)
{
$errorJson= json_decode($errorString);
$errorJson = json_decode($errorString);
$requestId = (string)$errorJson->request_id;
$errorCode = (int)$errorJson->error_code;
$errorMessage = (string)$errorJson->error_string;
Expand Down

0 comments on commit 5e2b9bf

Please sign in to comment.