Skip to content

Commit

Permalink
Merge pull request #59 from offline-agency/fix-throttle-handler
Browse files Browse the repository at this point in the history
Fix throttle handler
  • Loading branch information
Giacomo92 authored Nov 3, 2021
2 parents dbae7fe + 8c759a2 commit 9d9e696
Showing 1 changed file with 6 additions and 32 deletions.
38 changes: 6 additions & 32 deletions src/Auth.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
namespace OfflineAgency\FattureInCloud;

use Exception;
use Illuminate\Support\Arr;
use OfflineAgency\FattureInCloud\Events\DocumentRequestPerformed;

class Auth
Expand Down Expand Up @@ -51,7 +50,7 @@ private function call(
string $type = ''
) {
try {
$url = config('fatture-in-cloud.endpoint').$url;
$full_url = config('fatture-in-cloud.endpoint').$url;

$options = [
'http' => [
Expand All @@ -62,38 +61,12 @@ private function call(
];

$context = stream_context_create($options);
$response = file_get_contents($url, false, $context);
$response = file_get_contents($full_url, false, $context);

$parsed_header = $this->parseHeaders(
$http_response_header
);

$response_code = Arr::has($parsed_header, 'reponse_code')
? Arr::get($parsed_header, 'reponse_code')
: null;

$attempts = $this->attempts;
$times = config('fatture-in-cloud.times', 3);
if (
$attempts < $times
&& $response_code == 404
) {
$this->attempts++;

usleep(config('fatture-in-cloud.sleep-seconds', 5000000));

$this->call(
$url,
$data,
$method,
$additional_data,
$action,
$type
);
} else {
throw new \Exception('OA0002 - 404 on response');
}

$response = $this->parseResponse(
$response,
$url,
Expand Down Expand Up @@ -146,7 +119,7 @@ private function call(
* @param $additional_data
* @param $action
* @param $type
* @return mixed
* @return mixed|object
*
* @throws Exception
*/
Expand All @@ -166,7 +139,7 @@ private function parseResponse(
) {
$timeout_errors_codes = config('fatture-in-cloud.timeout-errors-codes');
if (in_array($json->error_code, $timeout_errors_codes)) {
$this->handleThrottle(
return $this->handleThrottle(
$json,
$url,
$data,
Expand All @@ -191,6 +164,7 @@ private function parseResponse(
* @param $additional_data
* @param $action
* @param $type
* @return mixed|object
*
* @throws Exception
*/
Expand All @@ -215,7 +189,7 @@ private function handleThrottle(
$seconds = $seconds * 1000;
usleep($seconds);

$this->call(
return $this->call(
$url,
$data,
$method,
Expand Down

0 comments on commit 9d9e696

Please sign in to comment.