Skip to content

Commit f931bdf

Browse files
committed
Merge branch '6.2' into 6.3
* 6.2: [HttpClient] Replace a few classes and methods occurrences by their source code link
2 parents 9596db6 + ad85226 commit f931bdf

File tree

1 file changed

+86
-34
lines changed

1 file changed

+86
-34
lines changed

http_client.rst

Lines changed: 86 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -727,7 +727,7 @@ original HTTP client::
727727

728728
$client = new RetryableHttpClient(HttpClient::create());
729729

730-
The ``RetryableHttpClient`` uses a
730+
The :class:`Symfony\\Component\\HttpClient\\RetryableHttpClient` uses a
731731
:class:`Symfony\\Component\\HttpClient\\Retry\\RetryStrategyInterface` to
732732
decide if the request should be retried, and to define the waiting time between
733733
each retry.
@@ -818,7 +818,8 @@ called when new data is uploaded or downloaded and at least once per second::
818818
]);
819819

820820
Any exceptions thrown from the callback will be wrapped in an instance of
821-
``TransportExceptionInterface`` and will abort the request.
821+
:class:`Symfony\\Contracts\\HttpClient\\Exception\\TransportExceptionInterface`
822+
and will abort the request.
822823

823824
HTTPS Certificates
824825
~~~~~~~~~~~~~~~~~~
@@ -967,9 +968,10 @@ This component supports both the native PHP streams and cURL to make the HTTP
967968
requests. Although both are interchangeable and provide the same features,
968969
including concurrent requests, HTTP/2 is only supported when using cURL.
969970

970-
``HttpClient::create()`` selects the cURL transport if the `cURL PHP extension`_
971-
is enabled and falls back to PHP streams otherwise. If you prefer to select
972-
the transport explicitly, use the following classes to create the client::
971+
The :method:`Symfony\\Component\\HttpClient\\HttpClient::create` method
972+
selects the cURL transport if the `cURL PHP extension`_ is enabled and falls
973+
back to PHP streams otherwise. If you prefer to select the transport
974+
explicitly, use the following classes to create the client::
973975

974976
use Symfony\Component\HttpClient\CurlHttpClient;
975977
use Symfony\Component\HttpClient\NativeHttpClient;
@@ -1140,8 +1142,9 @@ following methods::
11401142
Streaming Responses
11411143
~~~~~~~~~~~~~~~~~~~
11421144

1143-
Call the ``stream()`` method of the HTTP client to get *chunks* of the
1144-
response sequentially instead of waiting for the entire response::
1145+
Call the :method:`Symfony\\Contracts\\HttpClient\\HttpClientInterface::stream`
1146+
method to get *chunks* of the response sequentially instead of waiting for the
1147+
entire response::
11451148

11461149
$url = 'https://releases.ubuntu.com/18.04.1/ubuntu-18.04.1-desktop-amd64.iso';
11471150
$response = $client->request('GET', $url);
@@ -1171,8 +1174,7 @@ Canceling Responses
11711174

11721175
To abort a request (e.g. because it didn't complete in due time, or you want to
11731176
fetch only the first bytes of the response, etc.), you can either use the
1174-
``cancel()`` method of
1175-
:class:`Symfony\\Contracts\\HttpClient\\ResponseInterface`::
1177+
:method:`Symfony\\Contracts\\HttpClient\\ResponseInterface::cancel`::
11761178

11771179
$response->cancel();
11781180

@@ -1290,10 +1292,12 @@ If you look again at the snippet above, responses are read in requests' order.
12901292
But maybe the 2nd response came back before the 1st? Fully asynchronous operations
12911293
require being able to deal with the responses in whatever order they come back.
12921294

1293-
In order to do so, the ``stream()`` method of HTTP clients accepts a list of
1294-
responses to monitor. As mentioned :ref:`previously <http-client-streaming-responses>`,
1295-
this method yields response chunks as they arrive from the network. By replacing
1296-
the "foreach" in the snippet with this one, the code becomes fully async::
1295+
In order to do so, the
1296+
:method:`Symfony\\Contracts\\HttpClient\\HttpClientInterface::stream`
1297+
accepts a list of responses to monitor. As mentioned
1298+
:ref:`previously <http-client-streaming-responses>`, this method yields response
1299+
chunks as they arrive from the network. By replacing the "foreach" in the
1300+
snippet with this one, the code becomes fully async::
12971301

12981302
foreach ($client->stream($responses) as $response => $chunk) {
12991303
if ($chunk->isFirst()) {
@@ -1432,7 +1436,8 @@ installed in your application::
14321436
// this won't hit the network if the resource is already in the cache
14331437
$response = $client->request('GET', 'https://example.com/cacheable-resource');
14341438

1435-
``CachingHttpClient`` accepts a third argument to set the options of the ``HttpCache``.
1439+
:class:`Symfony\\Component\\HttpClient\\CachingHttpClient`` accepts a third argument
1440+
to set the options of the :class:`Symfony\\Component\\HttpKernel\\HttpCache\\HttpCache`.
14361441

14371442
Consuming Server-Sent Events
14381443
----------------------------
@@ -1620,11 +1625,49 @@ The `HTTPlug`_ v1 specification was published before PSR-18 and is superseded by
16201625
it. As such, you should not use it in newly written code. The component is still
16211626
interoperable with libraries that require it thanks to the
16221627
:class:`Symfony\\Component\\HttpClient\\HttplugClient` class. Similarly to
1623-
:class:`Symfony\\Component\\HttpClient\\Psr18Client`, ``HttplugClient`` also
1624-
implements relevant parts of PSR-17.
1628+
:class:`Symfony\\Component\\HttpClient\\Psr18Client` implementing relevant parts of PSR-17,
1629+
:class:`Symfony\\Component\\HttpClient\\HttplugClient` also implements the factory methods
1630+
defined in the related ``php-http/message-factory`` package.
1631+
1632+
.. code-block:: terminal
1633+
1634+
# Let's suppose php-http/httplug is already required by the lib you want to use
1635+
1636+
# installs an efficient implementation of response and stream factories
1637+
# with autowiring aliases provided by Symfony Flex
1638+
$ composer require nyholm/psr7
1639+
1640+
# alternatively, install the php-http/discovery package to auto-discover
1641+
# any already installed implementations from common vendors:
1642+
# composer require php-http/discovery
1643+
1644+
Let's say you want to instantiate a class with the following constructor,
1645+
that requires HTTPlug dependencies::
1646+
1647+
use Http\Client\HttpClient;
1648+
use Http\Message\RequestFactory;
1649+
use Http\Message\StreamFactory;
1650+
1651+
class SomeSdk
1652+
{
1653+
public function __construct(
1654+
HttpClient $httpClient,
1655+
RequestFactory $requestFactory,
1656+
StreamFactory $streamFactory
1657+
)
1658+
// [...]
1659+
}
1660+
1661+
Because :class:`Symfony\\Component\\HttpClient\\HttplugClient` implements the
1662+
three interfaces,you can use it this way::
1663+
1664+
use Symfony\Component\HttpClient\HttplugClient;
1665+
1666+
$httpClient = new HttplugClient();
1667+
$apiClient = new SomeSdk($httpClient, $httpClient, $httpClient);
16251668

1626-
If you'd like to work with promises, ``HttplugClient`` implements the
1627-
``HttpAsyncClient`` interface. To use it, you need to install the
1669+
If you'd like to work with promises, :class:`Symfony\\Component\\HttpClient\\HttplugClient`
1670+
also implements the ``HttpAsyncClient`` interface. To use it, you need to install the
16281671
``guzzlehttp/promises`` package:
16291672

16301673
.. code-block:: terminal
@@ -1817,20 +1860,24 @@ external service. By not making actual HTTP requests there is no need to worry a
18171860
the service being online or the request changing state, for example deleting
18181861
a resource.
18191862

1820-
``MockHttpClient`` implements the ``HttpClientInterface``, just like any actual
1821-
HTTP client in this component. When you type-hint with ``HttpClientInterface``
1822-
your code will accept the real client outside tests, while replacing it with
1823-
``MockHttpClient`` in the test.
1863+
:class:`Symfony\\Component\\HttpClient\\MockHttpClient` implements the
1864+
:class:`Symfony\\Contracts\\HttpClient\\HttpClientInterface`, just like any actual
1865+
HTTP client in this component. When you type-hint with
1866+
:class:`Symfony\\Contracts\\HttpClient\\HttpClientInterface` your code will accept
1867+
the real client outside tests, while replacing it with
1868+
:class:`Symfony\\Component\\HttpClient\\MockHttpClient` in the test.
18241869

1825-
When the ``request`` method is used on ``MockHttpClient``, it will respond with
1826-
the supplied ``MockResponse``. There are a few ways to use it, as described
1827-
below.
1870+
When the ``request`` method is used on :class:`Symfony\\Component\\HttpClient\\MockHttpClient`,
1871+
it will respond with the supplied
1872+
:class:`Symfony\\Component\\HttpClient\\Response\\MockResponse`. There are a few ways to use
1873+
it, as described below.
18281874

18291875
HTTP Client and Responses
18301876
~~~~~~~~~~~~~~~~~~~~~~~~~
18311877

1832-
The first way of using ``MockHttpClient`` is to pass a list of responses to its
1833-
constructor. These will be yielded in order when requests are made::
1878+
The first way of using :class:`Symfony\\Component\\HttpClient\\MockHttpClient`
1879+
is to pass a list of responses to its constructor. These will be yielded
1880+
in order when requests are made::
18341881

18351882
use Symfony\Component\HttpClient\MockHttpClient;
18361883
use Symfony\Component\HttpClient\Response\MockResponse;
@@ -1845,8 +1892,8 @@ constructor. These will be yielded in order when requests are made::
18451892
$response1 = $client->request('...'); // returns $responses[0]
18461893
$response2 = $client->request('...'); // returns $responses[1]
18471894

1848-
Another way of using ``MockHttpClient`` is to pass a callback that generates the
1849-
responses dynamically when it's called::
1895+
Another way of using :class:`Symfony\\Component\\HttpClient\\MockHttpClient` is to
1896+
pass a callback that generates the responses dynamically when it's called::
18501897

18511898
use Symfony\Component\HttpClient\MockHttpClient;
18521899
use Symfony\Component\HttpClient\Response\MockResponse;
@@ -1883,7 +1930,9 @@ assertions on the request before returning the mocked response::
18831930
.. tip::
18841931

18851932
Instead of using the first argument, you can also set the (list of)
1886-
responses or callbacks using the ``setResponseFactory()`` method::
1933+
responses or callbacks using the
1934+
:method:`Symfony\\Component\\HttpClient\\MockHttpClient::setResponseFactory`
1935+
method::
18871936

18881937
$responses = [
18891938
new MockResponse($body1, $info1),
@@ -1907,10 +1956,12 @@ define the ``http_code`` option::
19071956
$response = $client->request('...');
19081957

19091958
The responses provided to the mock client don't have to be instances of
1910-
``MockResponse``. Any class implementing ``ResponseInterface`` will work (e.g.
1911-
``$this->createMock(ResponseInterface::class)``).
1959+
:class:`Symfony\\Component\\HttpClient\\Response\\MockResponse`. Any class
1960+
implementing :class:`Symfony\\Contracts\\HttpClient\\ResponseInterface`
1961+
will work (e.g. ``$this->createMock(ResponseInterface::class)``).
19121962

1913-
However, using ``MockResponse`` allows simulating chunked responses and timeouts::
1963+
However, using :class:`Symfony\\Component\\HttpClient\\Response\\MockResponse`
1964+
allows simulating chunked responses and timeouts::
19141965

19151966
$body = function () {
19161967
yield 'hello';
@@ -2022,7 +2073,8 @@ You can use :class:`Symfony\\Component\\HttpClient\\Response\\JsonMockResponse`
20222073
Testing Request Data
20232074
~~~~~~~~~~~~~~~~~~~~
20242075

2025-
The ``MockResponse`` class comes with some helper methods to test the request:
2076+
The :class:`Symfony\\Component\\HttpClient\\Response\\MockResponse` class comes
2077+
with some helper methods to test the request:
20262078

20272079
* ``getRequestMethod()`` - returns the HTTP method;
20282080
* ``getRequestUrl()`` - returns the URL the request would be sent to;

0 commit comments

Comments
 (0)