1
0
mirror of https://github.com/guzzle/guzzle.git synced 2025-01-29 19:27:34 +01:00

Merge pull request #1562 from scheb/delay-with-response

Retry-After Header in Guzzle 6
This commit is contained in:
Michael Dowling 2016-08-31 09:51:05 -07:00 committed by GitHub
commit ba573b420d
2 changed files with 8 additions and 6 deletions

View File

@ -5,6 +5,7 @@ use GuzzleHttp\Promise\PromiseInterface;
use GuzzleHttp\Promise\RejectedPromise;
use GuzzleHttp\Psr7;
use Psr\Http\Message\RequestInterface;
use Psr\Http\Message\ResponseInterface;
/**
* Middleware that retries requests based on the boolean result of
@ -25,8 +26,8 @@ class RetryMiddleware
* retried.
* @param callable $nextHandler Next handler to invoke.
* @param callable $delay Function that accepts the number of retries
* and returns the number of milliseconds to
* delay.
* and [response] and returns the number of
* milliseconds to delay.
*/
public function __construct(
callable $decider,
@ -82,7 +83,7 @@ class RetryMiddleware
)) {
return $value;
}
return $this->doRetry($req, $options);
return $this->doRetry($req, $options, $value);
};
}
@ -102,9 +103,9 @@ class RetryMiddleware
};
}
private function doRetry(RequestInterface $request, array $options)
private function doRetry(RequestInterface $request, array $options, ResponseInterface $response = null)
{
$options['delay'] = call_user_func($this->delay, ++$options['retries']);
$options['delay'] = call_user_func($this->delay, ++$options['retries'], $response);
return $this($request, $options);
}

View File

@ -19,9 +19,10 @@ class RetryMiddlewareTest extends \PHPUnit_Framework_TestCase
$calls[] = func_get_args();
return count($calls) < 3;
};
$delay = function ($retries) use (&$delayCalls) {
$delay = function ($retries, $response) use (&$delayCalls) {
$delayCalls++;
$this->assertEquals($retries, $delayCalls);
$this->assertInstanceOf(Response::class, $response);
return 1;
};
$m = Middleware::retry($decider, $delay);