Skip to content

Commit fb9ccb3

Browse files
committed
Add doc for multiple base_uri as array
1 parent 68cd9f4 commit fb9ccb3

File tree

1 file changed

+55
-0
lines changed

1 file changed

+55
-0
lines changed

http_client.rst

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -731,6 +731,61 @@ The ``RetryableHttpClient`` uses a
731731
decide if the request should be retried, and to define the waiting time between
732732
each retry.
733733

734+
Retry over different uris
735+
.........................
736+
737+
When using a ``RetryableHttpClient``, the ``base_uri`` can be set as an array of
738+
uris. The array will be used in the given order, meaning that the first request
739+
will be made with the first uri in the array. Should it fail, the next one will
740+
be used for the first retry. If the number of configured retries is greater than
741+
the number of given uris, the last uri will be used for every exceding retry.
742+
743+
You can pass the array of base uris directly to the ``request`` method or when
744+
creating a HttpClient with the ``withOptions`` method::
745+
746+
use Symfony\Component\HttpClient\RetryableHttpClient;
747+
748+
$client = new RetryableHttpClient(HttpClient::create());
749+
750+
$response = $client->request('GET', 'some-path', [
751+
'base_uri' => [
752+
// first request will use this base uri
753+
'http://example.com/a/',
754+
// if first request fails, the second base uri will be used
755+
'http://example.com/b/',
756+
],
757+
]);
758+
759+
// You can also use the withOptions method to pass an array of base_uri
760+
$client = $client->withOptions(['base_uri' => [
761+
'http://example.com/a/',
762+
'http://example.com/b/',
763+
]]);
764+
765+
$response = $client->request('GET', 'some-path');
766+
767+
If you wish some or all of the base uris to be shuffled, you can pass a nested
768+
array inside the ``base_uri`` array. Every uri inside the nested array will be
769+
randomized, and the order of the base uris outside this nested array will be
770+
preserved::
771+
772+
use Symfony\Component\HttpClient\RetryableHttpClient;
773+
774+
$client = new RetryableHttpClient(HttpClient::create());
775+
776+
$response = $client->request('GET', 'some-path', [
777+
'base_uri' => [
778+
'http://example.com/a/',
779+
[
780+
// base uri passed inside a nested array will be shuffled
781+
'http://example.com/b/',
782+
'http://example.com/c/',
783+
]
784+
// the order of the non-nested base uris is preserved
785+
'http://example.com/d/',
786+
],
787+
]);
788+
734789
HTTP Proxies
735790
~~~~~~~~~~~~
736791

0 commit comments

Comments
 (0)