Skip to content

Commit

Permalink
[8.x] Allow passing when callback to Http client retry method (larave…
Browse files Browse the repository at this point in the history
…l#38531)

* Allow passing when callback to http client retry method

* linting

* add callable type

* update phpdoc on factory and facade

* Update PendingRequest.php

Co-authored-by: Taylor Otwell <[email protected]>
  • Loading branch information
2 people authored and victorvilella committed Oct 12, 2021
1 parent cffea07 commit 6f35caf
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 4 deletions.
2 changes: 1 addition & 1 deletion src/Illuminate/Http/Client/Factory.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@
* @method \Illuminate\Http\Client\PendingRequest contentType(string $contentType)
* @method \Illuminate\Http\Client\PendingRequest dd()
* @method \Illuminate\Http\Client\PendingRequest dump()
* @method \Illuminate\Http\Client\PendingRequest retry(int $times, int $sleep = 0)
* @method \Illuminate\Http\Client\PendingRequest retry(int $times, int $sleep = 0, ?callable $when = null)
* @method \Illuminate\Http\Client\PendingRequest sink(string|resource $to)
* @method \Illuminate\Http\Client\PendingRequest stub(callable $callback)
* @method \Illuminate\Http\Client\PendingRequest timeout(int $seconds)
Expand Down
13 changes: 11 additions & 2 deletions src/Illuminate/Http/Client/PendingRequest.php
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,13 @@ class PendingRequest
*/
protected $retryDelay = 100;

/**
* The callback that will determine if the request should be retried.
*
* @var callable|null
*/
protected $retryWhenCallback = null;

/**
* The callbacks that should execute before the request is sent.
*
Expand Down Expand Up @@ -441,12 +448,14 @@ public function timeout(int $seconds)
*
* @param int $times
* @param int $sleep
* @param callable|null $when
* @return $this
*/
public function retry(int $times, int $sleep = 0)
public function retry(int $times, int $sleep = 0, ?callable $when = null)
{
$this->tries = $times;
$this->retryDelay = $sleep;
$this->retryWhenCallback = $when;

return $this;
}
Expand Down Expand Up @@ -679,7 +688,7 @@ public function send(string $method, string $url, array $options = [])

throw new ConnectionException($e->getMessage(), 0, $e);
}
}, $this->retryDelay ?? 100);
}, $this->retryDelay ?? 100, $this->retryWhenCallback);
}

/**
Expand Down
2 changes: 1 addition & 1 deletion src/Illuminate/Support/Facades/Http.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
* @method static \Illuminate\Http\Client\PendingRequest contentType(string $contentType)
* @method static \Illuminate\Http\Client\PendingRequest dd()
* @method static \Illuminate\Http\Client\PendingRequest dump()
* @method static \Illuminate\Http\Client\PendingRequest retry(int $times, int $sleep = 0)
* @method static \Illuminate\Http\Client\PendingRequest retry(int $times, int $sleep = 0, ?callable $when = null)
* @method static \Illuminate\Http\Client\PendingRequest sink(string|resource $to)
* @method static \Illuminate\Http\Client\PendingRequest stub(callable $callback)
* @method static \Illuminate\Http\Client\PendingRequest timeout(int $seconds)
Expand Down

0 comments on commit 6f35caf

Please sign in to comment.