From cb3bf0764af98d404f8d671cdfbf4c91f4e14888 Mon Sep 17 00:00:00 2001 From: Achmad Hadi Kurnia Date: Sat, 24 Feb 2024 11:44:33 +0700 Subject: [PATCH 1/3] fix: fix bugs retry on http client --- src/Siasn.php | 35 +++++++++++++++++++---------------- 1 file changed, 19 insertions(+), 16 deletions(-) diff --git a/src/Siasn.php b/src/Siasn.php index 721de7f..a040c1b 100644 --- a/src/Siasn.php +++ b/src/Siasn.php @@ -22,7 +22,8 @@ public function __construct() Token::forget(); - $request->withToken(Token::getApimToken()->access_token); + $request + ->withToken(Token::getApimToken()->access_token); return true; }) @@ -37,23 +38,25 @@ public function withSso() { $ssoToken = Token::getSsoToken(); - 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; - } + return $this->class + ->retry(2, 0, function (Exception $exception, PendingRequest $request) { + if (! $exception instanceof RequestException || $exception->response->status() !== 401) { + return false; + } - Token::forget(); - $ssoToken = Token::getSsoToken(); + Token::forget(); + $ssoToken = Token::getSsoToken(); - $request - ->withToken(Token::getApimToken()->access_token) - ->withHeaders([ - 'Auth' => "{$ssoToken->token_type} {$ssoToken->access_token}", - ]); + $request + ->withToken(Token::getApimToken()->access_token) + ->withHeaders([ + 'Auth' => "{$ssoToken->token_type} {$ssoToken->access_token}", + ]); - return true; - }); + return true; + }) + ->withHeaders([ + 'Auth' => "{$ssoToken->token_type} {$ssoToken->access_token}", + ]); } } From 8da3a71918f4f10e6ac3bba20931965651bb72bb Mon Sep 17 00:00:00 2001 From: Achmad Hadi Kurnia Date: Sat, 24 Feb 2024 11:53:41 +0700 Subject: [PATCH 2/3] feat: add retry on http client --- config/siasn-api.php | 74 ++-------------------------------------- src/Credentials/Apim.php | 19 ++++++----- src/Credentials/Sso.php | 21 +++++++----- src/Siasn.php | 4 +-- 4 files changed, 27 insertions(+), 91 deletions(-) diff --git a/config/siasn-api.php b/config/siasn-api.php index 7d1b145..f12cab1 100644 --- a/config/siasn-api.php +++ b/config/siasn-api.php @@ -2,57 +2,12 @@ return [ - /* - |-------------------------------------------------------------------------- - | Mode - |-------------------------------------------------------------------------- - | - | This value determines the "mode" your SIASN API is currently running in. - | This may determine how you prefer to configure various services the - | application utilizes. - | - | Supported: "training", "production" - | - */ - 'mode' => env('SIASN_MODE', 'training'), - /* - |-------------------------------------------------------------------------- - | Debug Mode - |-------------------------------------------------------------------------- - | - | When in debug mode, detailed error messages with stack traces will be - | shown on every error that occurs within your application. If disabled, - | a simple generic error page is shown. - | - */ - 'http_verify' => (bool) env('SIASN_HTTP_VERIFY', true), - /* - |-------------------------------------------------------------------------- - | Debug Mode - |-------------------------------------------------------------------------- - | - | When in debug mode, detailed error messages with stack traces will be - | shown on every error that occurs within your application. If disabled, - | a simple generic error page is shown. - | - */ - 'debug' => (bool) env('SIASN_DEBUG', env('APP_DEBUG')), - /* - |-------------------------------------------------------------------------- - | Credentials - |-------------------------------------------------------------------------- - | - | This options is for storing credentials for SIASN API Manager and SIASN - | SSO API - | - */ - 'apim' => [ 'production' => [ 'url' => 'https://apimws.bkn.go.id/oauth2/token', @@ -87,42 +42,19 @@ ], ], - /* - |-------------------------------------------------------------------------- - | Constanta - |-------------------------------------------------------------------------- - | - | This options is for storing the param for SIASN API. - | - */ - 'const' => [ 'instansi_id' => env('SIASN_CONST_INSTANSI_ID'), 'satuan_kerja_id' => env('SIASN_CONST_SATUAN_KERJA_ID'), ], - /* - |-------------------------------------------------------------------------- - | Token Age - |-------------------------------------------------------------------------- - | - | This option is to cache token ages in seconds. - | - */ - 'token_age' => [ 'apim' => env('SIASN_APIM_TOKEN_AGE', 3600 - 60), 'sso' => env('SIASN_SSO_TOKEN_AGE', 43200 - 60), ], - /* - |-------------------------------------------------------------------------- - | Timeout - |-------------------------------------------------------------------------- - | - | This option determine the time limit for getting a response in seconds - | - */ + 'max_request_attempts' => env('SIASN_REQUEST_ATTEMPTS', 3), + + 'max_request_wait_attempts' => env('SIASN_REQUEST_WAIT_ATTEMPTS', 3), 'timeout' => env('SIASN_TIMEOUT', 60), diff --git a/src/Credentials/Apim.php b/src/Credentials/Apim.php index 3fc7741..61d7e1a 100644 --- a/src/Credentials/Apim.php +++ b/src/Credentials/Apim.php @@ -22,14 +22,15 @@ public static function getToken(): Response throw new InvalidApimCredentialsException('password must be set'); } - return Http::withOptions([ - 'debug' => Config::getDebug(), - 'verify' => Config::getHttpVerify(), - ])->withBasicAuth( - $credential->username, - $credential->password - )->post($credential->url, [ - 'grant_type' => $credential->grant_type, - ]); + return Http::retry(config('siasn-api.max_request_attempts'), config('siasn-api.max_request_wait_attempts')) + ->withOptions([ + 'debug' => Config::getDebug(), + 'verify' => Config::getHttpVerify(), + ])->withBasicAuth( + $credential->username, + $credential->password + )->post($credential->url, [ + 'grant_type' => $credential->grant_type, + ]); } } diff --git a/src/Credentials/Sso.php b/src/Credentials/Sso.php index 8dfc8f4..41e5c7c 100644 --- a/src/Credentials/Sso.php +++ b/src/Credentials/Sso.php @@ -26,14 +26,17 @@ public static function getToken(): Response throw new InvalidSsoCredentialsException('password must be set'); } - return Http::asForm()->withOptions([ - 'debug' => Config::getDebug(), - 'verify' => Config::getHttpVerify(), - ])->post($credential->url, [ - 'grant_type' => $credential->grant_type, - 'client_id' => $credential->client_id, - 'username' => $credential->username, - 'password' => $credential->password, - ]); + return Http::asForm() + ->retry(config('siasn-api.max_request_attempts'), config('siasn-api.max_request_wait_attempts')) + ->withOptions([ + 'debug' => Config::getDebug(), + 'verify' => Config::getHttpVerify(), + ]) + ->post($credential->url, [ + 'grant_type' => $credential->grant_type, + 'client_id' => $credential->client_id, + 'username' => $credential->username, + 'password' => $credential->password, + ]); } } diff --git a/src/Siasn.php b/src/Siasn.php index a040c1b..8ce4c91 100644 --- a/src/Siasn.php +++ b/src/Siasn.php @@ -15,7 +15,7 @@ class Siasn extends ClassExtender public function __construct() { $this->class = Http::timeout(config('siasn-api.timeout')) - ->retry(2, 0, function (Exception $exception, PendingRequest $request) { + ->retry(config('siasn-api.max_request_attempts'), config('siasn-api.max_request_wait_attempts'), function (Exception $exception, PendingRequest $request) { if (! $exception instanceof RequestException || $exception->response->status() !== 401) { return false; } @@ -39,7 +39,7 @@ public function withSso() $ssoToken = Token::getSsoToken(); return $this->class - ->retry(2, 0, function (Exception $exception, PendingRequest $request) { + ->retry(config('siasn-api.max_request_attempts'), config('siasn-api.max_request_wait_attempts'), function (Exception $exception, PendingRequest $request) { if (! $exception instanceof RequestException || $exception->response->status() !== 401) { return false; } From 9bbd2f46ac90460058ac6dd7e84425f46ab79221 Mon Sep 17 00:00:00 2001 From: Achmad Hadi Kurnia Date: Sat, 24 Feb 2024 12:02:56 +0700 Subject: [PATCH 3/3] feat: add request timeout --- config/siasn-api.php | 2 +- src/Credentials/Apim.php | 3 ++- src/Credentials/Sso.php | 3 ++- src/Siasn.php | 2 +- 4 files changed, 6 insertions(+), 4 deletions(-) diff --git a/config/siasn-api.php b/config/siasn-api.php index f12cab1..9362879 100644 --- a/config/siasn-api.php +++ b/config/siasn-api.php @@ -56,6 +56,6 @@ 'max_request_wait_attempts' => env('SIASN_REQUEST_WAIT_ATTEMPTS', 3), - 'timeout' => env('SIASN_TIMEOUT', 60), + 'request_timeout' => env('SIASN_TIMEOUT', 60), ]; diff --git a/src/Credentials/Apim.php b/src/Credentials/Apim.php index 61d7e1a..8f8a026 100644 --- a/src/Credentials/Apim.php +++ b/src/Credentials/Apim.php @@ -22,7 +22,8 @@ public static function getToken(): Response throw new InvalidApimCredentialsException('password must be set'); } - return Http::retry(config('siasn-api.max_request_attempts'), config('siasn-api.max_request_wait_attempts')) + return Http::timeout(config('siasn-api.request_timeout')) + ->retry(config('siasn-api.max_request_attempts'), config('siasn-api.max_request_wait_attempts')) ->withOptions([ 'debug' => Config::getDebug(), 'verify' => Config::getHttpVerify(), diff --git a/src/Credentials/Sso.php b/src/Credentials/Sso.php index 41e5c7c..977135a 100644 --- a/src/Credentials/Sso.php +++ b/src/Credentials/Sso.php @@ -26,7 +26,8 @@ public static function getToken(): Response throw new InvalidSsoCredentialsException('password must be set'); } - return Http::asForm() + return Http::timeout(config('siasn-api.request_timeout')) + ->asForm() ->retry(config('siasn-api.max_request_attempts'), config('siasn-api.max_request_wait_attempts')) ->withOptions([ 'debug' => Config::getDebug(), diff --git a/src/Siasn.php b/src/Siasn.php index 8ce4c91..1c4681b 100644 --- a/src/Siasn.php +++ b/src/Siasn.php @@ -14,7 +14,7 @@ class Siasn extends ClassExtender { public function __construct() { - $this->class = Http::timeout(config('siasn-api.timeout')) + $this->class = Http::timeout(config('siasn-api.request_timeout')) ->retry(config('siasn-api.max_request_attempts'), config('siasn-api.max_request_wait_attempts'), function (Exception $exception, PendingRequest $request) { if (! $exception instanceof RequestException || $exception->response->status() !== 401) { return false;