diff options
author | Ewan Dennis <ewandennis@users.noreply.github.com> | 2017-07-10 16:13:28 +0100 |
---|---|---|
committer | GitHub <noreply@github.com> | 2017-07-10 16:13:28 +0100 |
commit | 342ecf62947cfe3b31f2253fb427b1b4a613907b (patch) | |
tree | 0a7ddf7a0dee31d895479ce8af589477b93d8bd4 /lib | |
parent | 4de9c54c7a3554192e50fee9734fa96bf3e7bec3 (diff) | |
parent | 7beccdecb04d24cd055c91cf9b652ccc58eeaf60 (diff) | |
download | php-sparkpost-342ecf62947cfe3b31f2253fb427b1b4a613907b.zip php-sparkpost-342ecf62947cfe3b31f2253fb427b1b4a613907b.tar.gz php-sparkpost-342ecf62947cfe3b31f2253fb427b1b4a613907b.tar.bz2 |
Merge pull request #169 from SparkPost/issue-168
Issue-168: Optional automatic retry on 5xx
Diffstat (limited to 'lib')
-rw-r--r-- | lib/SparkPost/SparkPost.php | 41 |
1 files changed, 37 insertions, 4 deletions
diff --git a/lib/SparkPost/SparkPost.php b/lib/SparkPost/SparkPost.php index 05be7f4..b3a9e36 100644 --- a/lib/SparkPost/SparkPost.php +++ b/lib/SparkPost/SparkPost.php @@ -41,6 +41,7 @@ class SparkPost 'version' => 'v1', 'async' => true, 'debug' => false, + 'retries' => 0 ]; /** @@ -97,13 +98,29 @@ class SparkPost $requestValues = $this->buildRequestValues($method, $uri, $payload, $headers); $request = call_user_func_array(array($this, 'buildRequestInstance'), $requestValues); + $retries = $this->options['retries']; try { - return new SparkPostResponse($this->httpClient->sendRequest($request), $this->ifDebug($requestValues)); + if ($retries > 0) { + $resp = $this->syncReqWithRetry($request, $retries); + } else { + $resp = $this->httpClient->sendRequest($request); + } + return new SparkPostResponse($resp, $this->ifDebug($requestValues)); } catch (\Exception $exception) { throw new SparkPostException($exception, $this->ifDebug($requestValues)); } } + private function syncReqWithRetry($request, $retries) + { + $resp = $this->httpClient->sendRequest($request); + $status = $resp->getStatusCode(); + if ($status >= 500 && $status <= 599 && $retries > 0) { + return $this->syncReqWithRetry($request, $retries-1); + } + return $resp; + } + /** * Sends async request to SparkPost API. * @@ -120,12 +137,28 @@ class SparkPost $requestValues = $this->buildRequestValues($method, $uri, $payload, $headers); $request = call_user_func_array(array($this, 'buildRequestInstance'), $requestValues); - return new SparkPostPromise($this->httpClient->sendAsyncRequest($request), $this->ifDebug($requestValues)); + $retries = $this->options['retries']; + if ($retries > 0) { + return new SparkPostPromise($this->asyncReqWithRetry($request, $retries), $this->ifDebug($requestValues)); + } else { + return new SparkPostPromise($this->httpClient->sendAsyncRequest($request), $this->ifDebug($requestValues)); + } } else { throw new \Exception('Your http client does not support asynchronous requests. Please use a different client or use synchronous requests.'); } } + private function asyncReqWithRetry($request, $retries) + { + return $this->httpClient->sendAsyncRequest($request)->then(function($response) use ($request, $retries) { + $status = $response->getStatusCode(); + if ($status >= 500 && $status <= 599 && $retries > 0) { + return $this->asyncReqWithRetry($request, $retries-1); + } + return $response; + }); + } + /** * Builds request values from given params. * @@ -252,7 +285,7 @@ class SparkPost } $this->httpClient = $httpClient; - + return $this; } @@ -283,7 +316,7 @@ class SparkPost $this->options[$option] = $value; } } - + return $this; } |