Skip to content

Commit

Permalink
Merge pull request #19 from achmadhadikurnia/main
Browse files Browse the repository at this point in the history
update token on retry if get respond error is 401
  • Loading branch information
achmadhadikurnia authored Feb 24, 2024
2 parents 601413c + 45c6078 commit 321d655
Showing 1 changed file with 31 additions and 8 deletions.
39 changes: 31 additions & 8 deletions src/Siasn.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,9 @@

namespace Kanekescom\Siasn\Api;

use Exception;
use Illuminate\Http\Client\PendingRequest;
use Illuminate\Http\Client\RequestException;
use Illuminate\Support\Facades\Http;
use Kanekescom\Helperia\Support\ClassExtender;
use Kanekescom\Siasn\Api\Credentials\Token;
Expand All @@ -11,16 +14,23 @@ class Siasn extends ClassExtender
{
public function __construct()
{
$apimToken = Token::getApimToken();
$this->class = Http::timeout(config('siasn-api.timeout'))
->retry(2, 0, function (Exception $exception, PendingRequest $request) {
if (! $exception instanceof RequestException || $exception->response->status() !== 401) {
return false;
}

$this->class = Http::retry(3, 100)
->timeout(config('siasn-api.timeout'))
Token::forget();

$request->withToken(Token::getApimToken()->access_token);

return true;
})
->withOptions([
'debug' => Config::getDebug(),
'verify' => false,
])->withToken(
$apimToken->access_token
);
'verify' => Config::getHttpVerify(),
])
->withToken(Token::getApimToken()->access_token);
}

public function withSso()
Expand All @@ -29,6 +39,19 @@ public function withSso()

return $this->class->withHeaders([
'Auth' => "{$ssoToken->token_type} {$ssoToken->access_token}",
]);
])->retry(2, 0, function (Exception $exception, PendingRequest $request) {
if (! $exception instanceof RequestException || $exception->response->status() !== 401) {
return false;
}

Token::forget();
$ssoToken = Token::getSsoToken();

$request->withHeaders([
'Auth' => "{$ssoToken->token_type} {$ssoToken->access_token}",
]);

return true;
});
}
}

0 comments on commit 321d655

Please sign in to comment.