Skip to content

Commit 1b49323

Browse files
committed
refactor(authenticator): extract retry logic to configurable callbacks
- Introduce $decider and $delay callables to customize retry behavior - Move default retry decision logic from inline closure to $decider property - Support optional delay strategy via $delay callable - Improve flexibility for handling 401 responses by invalidating cache key before retry - Preserve existing retry limit of one attempt per request Signed-off-by: guanguans <[email protected]>
1 parent adc134d commit 1b49323

File tree

2 files changed

+14
-6
lines changed

2 files changed

+14
-6
lines changed

src/ZohoCliq/Authenticator.php

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,9 @@ class Authenticator extends NullAuthenticator implements \Stringable
5151
private DataCenter $dataCenter;
5252
private CacheInterface $cache;
5353
private string $cacheKey;
54+
55+
/** @var null|callable */
56+
private $retryDelay;
5457
private Client $client;
5558

5659
/**
@@ -65,11 +68,13 @@ public function __construct(
6568
?string $dataCenter = null,
6669
?CacheInterface $cache = null,
6770
?string $cacheKey = null,
71+
?callable $retryDelay = null,
6872
?Client $client = null,
6973
) {
7074
$this->dataCenter = new DataCenter($dataCenter);
7175
$this->cache = $cache ?? new FileCache;
7276
$this->cacheKey = $cacheKey ?? "zoho_cliq.access_token.$this->dataCenter.$clientId.$clientSecret";
77+
$this->retryDelay = $retryDelay;
7378
$this->client = ($client ?? new Client)->push($this->baseUriMiddleware($this->dataCenter->toOauthBaseUri()));
7479
}
7580

@@ -124,6 +129,7 @@ private function authMiddleware(): callable
124129
}
125130

126131
/**
132+
* @see \GuzzleHttp\RetryMiddleware::exponentialDelay()
127133
* @see \GuzzleHttp\RetryMiddleware::onFulfilled()
128134
* @see \GuzzleHttp\RetryMiddleware::onRejected()
129135
*/
@@ -143,7 +149,8 @@ function (int $retries, RequestInterface $request, ?ResponseInterface $response
143149
}
144150

145151
return false;
146-
}
152+
},
153+
$this->retryDelay
147154
);
148155
}
149156

tests/ZohoCliq/ClientTest.php

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@
3838
// dataCenter: DataCenter::US,
3939
// cache: new MemoryCache,
4040
cache: new NullCache,
41+
retryDelay: fn (): int => 0,
4142
client: (new \Guanguans\Notify\Foundation\Client)->mock(array_pad(
4243
[],
4344
6,
@@ -165,15 +166,15 @@
165166

166167
it('can retry send user message', function (): void {
167168
/** @var \Guanguans\Notify\Foundation\Response $response */
168-
$message = UserMessage::make($this->message)->emailId(fake()->email());
169+
$userMessage = UserMessage::make($this->message)->emailId(fake()->email());
169170
$response = $this
170171
->client
171172
->mock([
172173
response('retries0', 401),
173174
response('retries1', 401),
174175
response('retries2', 401),
175176
response(status: 204),
176-
])->send($message);
177+
])->send($userMessage);
177178
expect($response)
178179
->body()->toBe('retries1')
179180
->status()->toBe(401);
@@ -184,7 +185,7 @@
184185
response('retries0', 400),
185186
response('retries1', 401),
186187
response(status: 204),
187-
])->send($message);
188+
])->send($userMessage);
188189
expect($response)
189190
->body()->toBe('retries0')
190191
->status()->toBe(400);
@@ -195,7 +196,7 @@
195196
response('retries0', 200),
196197
response('retries1', 401),
197198
response(status: 204),
198-
])->send($message);
199+
])->send($userMessage);
199200
expect($response)
200201
->body()->toBe('retries0')
201202
->status()->toBe(200);
@@ -205,7 +206,7 @@
205206
->mock([
206207
response('retries0', 401),
207208
response(status: 204),
208-
])->send($message);
209+
])->send($userMessage);
209210
expect($response)
210211
->body()->toBeEmpty()
211212
->status()->toBe(204);

0 commit comments

Comments
 (0)