Skip to content

Commit 92ec747

Browse files
authored
Merge pull request #11 from keboola/adamvyborny-CM-85-do-not-retry-on-401
Do not retry on 401
2 parents 2ea1e70 + 9ae21f6 commit 92ec747

2 files changed

Lines changed: 30 additions & 1 deletion

File tree

src/Google/RestApi.php

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -322,6 +322,9 @@ protected function decideRetry(int $retries, int $maxRetries, ?ResponseInterface
322322
if ($statusCode === 400) {
323323
return false;
324324
}
325+
if ($statusCode === 401 && $retries > 0) { //allow only one retry for refreshing token
326+
return false;
327+
}
325328
if ($statusCode === 403) {
326329
return call_user_func($this->backoffCallback403, $response);
327330
}

tests/RestApiTest.php

Lines changed: 27 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -124,7 +124,7 @@ public function testRetries(): void
124124
'request' => [
125125
'uri' => 'https://www.googleapis.com/auth/invalid-scope',
126126
'headers' => [
127-
'User-Agent' => ['GuzzleHttp/6.5.5 curl/7.74.0 PHP/7.4.30'],
127+
'User-Agent' => ['GuzzleHttp/6.5.5 curl/7.74.0 PHP/7.4.33'],
128128
'Host' => ['www.googleapis.com'],
129129
'Accept' => ['application/json'],
130130
'Authorization' => '*****',
@@ -150,6 +150,32 @@ public function testRetries(): void
150150
}
151151
}
152152

153+
public function testDoNotRetryOnWrongCredentials(): void
154+
{
155+
$testHandler = new TestHandler();
156+
$logger = new Logger('Google Rest API tests');
157+
$logger->pushHandler($testHandler);
158+
$api = new RestApi(
159+
$this->getEnv('CLIENT_ID'),
160+
$this->getEnv('CLIENT_SECRET') . 'invalid',
161+
'',
162+
$this->getEnv('REFRESH_TOKEN'),
163+
$logger
164+
);
165+
166+
try {
167+
$api->request('/oauth2/v3/userinfo');
168+
} catch (ClientException $e) {
169+
$this->assertStringContainsString('401 Unauthorized', $e->getMessage());
170+
}
171+
172+
$this->assertCount(1, $testHandler->getRecords());
173+
$this->assertStringContainsString(
174+
'Retrying request (0x) - reason: Unauthorized',
175+
$testHandler->getRecords()[0]['message']
176+
);
177+
}
178+
153179
protected function getEnv(string $name): string
154180
{
155181
$value = getenv($name);

0 commit comments

Comments
 (0)