Skip to content

Commit f83da38

Browse files
Merge pull request #3 from StyleCI/retry
Use the retry middleware
2 parents b5874a7 + 1e04fb3 commit f83da38

File tree

2 files changed

+21
-2
lines changed

2 files changed

+21
-2
lines changed

composer.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@
4141
},
4242
"extra": {
4343
"branch-alias": {
44-
"dev-master": "1.0-dev"
44+
"dev-master": "1.1-dev"
4545
}
4646
},
4747
"minimum-stability": "dev",

src/Client.php

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,11 @@
1313

1414
use GuzzleHttp\Client as GuzzleClient;
1515
use GuzzleHttp\ClientInterface;
16+
use GuzzleHttp\Exception\TransferException;
17+
use GuzzleHttp\HandlerStack;
18+
use GuzzleHttp\Middleware;
19+
use Psr\Http\Message\RequestInterface;
20+
use Psr\Http\Message\ResponseInterface;
1621

1722
/**
1823
* This is the client class.
@@ -44,7 +49,21 @@ class Client
4449
*/
4550
public function __construct(ClientInterface $client = null)
4651
{
47-
$this->client = $client ?: new GuzzleClient(['base_uri' => static::BASE_URL, 'headers' => ['Accept' => 'application/json', 'User-Agent' => 'styleci-sdk/1.0']]);
52+
if ($client) {
53+
$this->client = $client;
54+
} else {
55+
$stack = HandlerStack::create();
56+
$stack->push(Middleware::retry(function ($retries, RequestInterface $request, ResponseInterface $response = null, TransferException $exception = null) {
57+
return $retries < 3 && ($exception instanceof ConnectException || ($response && $response->getStatusCode() >= 500));
58+
}, function ($retries) {
59+
return (int) pow(2, $retries) * 1000;
60+
}));
61+
$this->client = new GuzzleClient([
62+
'base_uri' => static::BASE_URL,
63+
'handler' => $stack,
64+
'headers' => ['Accept' => 'application/json', 'User-Agent' => 'styleci-sdk/1.1'],
65+
]);
66+
}
4867
}
4968

5069
/**

0 commit comments

Comments
 (0)