Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix bugs retry on http client and add request timeout feature #21

Merged
merged 4 commits into from
Feb 24, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
76 changes: 4 additions & 72 deletions config/siasn-api.php
Original file line number Diff line number Diff line change
Expand Up @@ -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',
Expand Down Expand Up @@ -87,43 +42,20 @@
],
],

/*
|--------------------------------------------------------------------------
| 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),
'request_timeout' => env('SIASN_TIMEOUT', 60),

];
20 changes: 11 additions & 9 deletions src/Credentials/Apim.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,14 +22,16 @@ 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::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(),
])->withBasicAuth(
$credential->username,
$credential->password
)->post($credential->url, [
'grant_type' => $credential->grant_type,
]);
}
}
22 changes: 13 additions & 9 deletions src/Credentials/Sso.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,14 +26,18 @@ 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::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(),
'verify' => Config::getHttpVerify(),
])
->post($credential->url, [
'grant_type' => $credential->grant_type,
'client_id' => $credential->client_id,
'username' => $credential->username,
'password' => $credential->password,
]);
}
}
39 changes: 21 additions & 18 deletions src/Siasn.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,15 +14,16 @@ class Siasn extends ClassExtender
{
public function __construct()
{
$this->class = Http::timeout(config('siasn-api.timeout'))
->retry(2, 0, function (Exception $exception, PendingRequest $request) {
$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;
}

Token::forget();

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

return true;
})
Expand All @@ -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(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;
}

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}",
]);
}
}
Loading