@@ -32,6 +32,7 @@ class Client
3232 private string $ userAgent = '' ;
3333 private int $ maxRetries = 0 ;
3434 private int $ retryDelay = 1000 ; // milliseconds
35+ private array $ retryStatusCodes = [500 , 503 ];
3536
3637 /**
3738 * @param string $key
@@ -131,6 +132,18 @@ public function setRetryDelay(int $retryDelay): self
131132 return $ this ;
132133 }
133134
135+ /**
136+ * Set the retry status codes.
137+ *
138+ * @param array<int> $retryStatusCodes
139+ * @return self
140+ */
141+ public function setRetryStatusCodes (array $ retryStatusCodes ): self
142+ {
143+ $ this ->retryStatusCodes = $ retryStatusCodes ;
144+ return $ this ;
145+ }
146+
134147 /**
135148 * Flatten request body array to PHP multiple format
136149 *
@@ -161,14 +174,14 @@ private static function flatten(array $data, string $prefix = ''): array
161174 * @return mixed
162175 * @throws \Exception
163176 */
164- private function retry (callable $ callback ): mixed
177+ private function withRetries (callable $ callback ): mixed
165178 {
166179 $ attempts = 1 ;
167180
168181 while (true ) {
169182 $ res = $ callback ();
170183
171- if ($ res ->getStatusCode () !== 500 && $ res -> getStatusCode () !== 503 || $ attempts >= $ this ->maxRetries ) {
184+ if (! in_array ( $ res ->getStatusCode (), $ this -> retryStatusCodes ) || $ attempts >= $ this ->maxRetries ) {
172185 return $ res ;
173186 }
174187
@@ -265,7 +278,7 @@ public function fetch(
265278 };
266279
267280 if ($ this ->maxRetries > 0 ) {
268- $ response = $ this ->retry ($ sendRequest );
281+ $ response = $ this ->withRetries ($ sendRequest );
269282 } else {
270283 $ response = $ sendRequest ();
271284 }
@@ -342,4 +355,14 @@ public function getRetryDelay(): int
342355 {
343356 return $ this ->retryDelay ;
344357 }
358+
359+ /**
360+ * Get the retry status codes.
361+ *
362+ * @return array<int>
363+ */
364+ public function getRetryStatusCodes (): array
365+ {
366+ return $ this ->retryStatusCodes ;
367+ }
345368}
0 commit comments