Skip to content

Commit eb64433

Browse files
committed
Merge branch '6.3' into 6.4
* 6.3: [HttpClient] Replace a few classes and methods occurrences by their source code link
2 parents 579d0db + f931bdf commit eb64433

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
@@ -732,7 +732,7 @@ original HTTP client::
732732

733733
$client = new RetryableHttpClient(HttpClient::create());
734734

735-
The ``RetryableHttpClient`` uses a
735+
The :class:`Symfony\\Component\\HttpClient\\RetryableHttpClient` uses a
736736
:class:`Symfony\\Component\\HttpClient\\Retry\\RetryStrategyInterface` to
737737
decide if the request should be retried, and to define the waiting time between
738738
each retry.
@@ -823,7 +823,8 @@ called when new data is uploaded or downloaded and at least once per second::
823823
]);
824824

825825
Any exceptions thrown from the callback will be wrapped in an instance of
826-
``TransportExceptionInterface`` and will abort the request.
826+
:class:`Symfony\\Contracts\\HttpClient\\Exception\\TransportExceptionInterface`
827+
and will abort the request.
827828

828829
HTTPS Certificates
829830
~~~~~~~~~~~~~~~~~~
@@ -972,9 +973,10 @@ This component supports both the native PHP streams and cURL to make the HTTP
972973
requests. Although both are interchangeable and provide the same features,
973974
including concurrent requests, HTTP/2 is only supported when using cURL.
974975

975-
``HttpClient::create()`` selects the cURL transport if the `cURL PHP extension`_
976-
is enabled and falls back to PHP streams otherwise. If you prefer to select
977-
the transport explicitly, use the following classes to create the client::
976+
The :method:`Symfony\\Component\\HttpClient\\HttpClient::create` method
977+
selects the cURL transport if the `cURL PHP extension`_ is enabled and falls
978+
back to PHP streams otherwise. If you prefer to select the transport
979+
explicitly, use the following classes to create the client::
978980

979981
use Symfony\Component\HttpClient\CurlHttpClient;
980982
use Symfony\Component\HttpClient\NativeHttpClient;
@@ -1145,8 +1147,9 @@ following methods::
11451147
Streaming Responses
11461148
~~~~~~~~~~~~~~~~~~~
11471149

1148-
Call the ``stream()`` method of the HTTP client to get *chunks* of the
1149-
response sequentially instead of waiting for the entire response::
1150+
Call the :method:`Symfony\\Contracts\\HttpClient\\HttpClientInterface::stream`
1151+
method to get *chunks* of the response sequentially instead of waiting for the
1152+
entire response::
11501153

11511154
$url = 'https://releases.ubuntu.com/18.04.1/ubuntu-18.04.1-desktop-amd64.iso';
11521155
$response = $client->request('GET', $url);
@@ -1176,8 +1179,7 @@ Canceling Responses
11761179

11771180
To abort a request (e.g. because it didn't complete in due time, or you want to
11781181
fetch only the first bytes of the response, etc.), you can either use the
1179-
``cancel()`` method of
1180-
:class:`Symfony\\Contracts\\HttpClient\\ResponseInterface`::
1182+
:method:`Symfony\\Contracts\\HttpClient\\ResponseInterface::cancel`::
11811183

11821184
$response->cancel();
11831185

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

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

13031307
foreach ($client->stream($responses) as $response => $chunk) {
13041308
if ($chunk->isFirst()) {
@@ -1437,7 +1441,8 @@ installed in your application::
14371441
// this won't hit the network if the resource is already in the cache
14381442
$response = $client->request('GET', 'https://example.com/cacheable-resource');
14391443

1440-
``CachingHttpClient`` accepts a third argument to set the options of the ``HttpCache``.
1444+
:class:`Symfony\\Component\\HttpClient\\CachingHttpClient`` accepts a third argument
1445+
to set the options of the :class:`Symfony\\Component\\HttpKernel\\HttpCache\\HttpCache`.
14411446

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

1631-
If you'd like to work with promises, ``HttplugClient`` implements the
1632-
``HttpAsyncClient`` interface. To use it, you need to install the
1674+
If you'd like to work with promises, :class:`Symfony\\Component\\HttpClient\\HttplugClient`
1675+
also implements the ``HttpAsyncClient`` interface. To use it, you need to install the
16331676
``guzzlehttp/promises`` package:
16341677

16351678
.. code-block:: terminal
@@ -1822,20 +1865,24 @@ external service. By not making actual HTTP requests there is no need to worry a
18221865
the service being online or the request changing state, for example deleting
18231866
a resource.
18241867

1825-
``MockHttpClient`` implements the ``HttpClientInterface``, just like any actual
1826-
HTTP client in this component. When you type-hint with ``HttpClientInterface``
1827-
your code will accept the real client outside tests, while replacing it with
1828-
``MockHttpClient`` in the test.
1868+
:class:`Symfony\\Component\\HttpClient\\MockHttpClient` implements the
1869+
:class:`Symfony\\Contracts\\HttpClient\\HttpClientInterface`, just like any actual
1870+
HTTP client in this component. When you type-hint with
1871+
:class:`Symfony\\Contracts\\HttpClient\\HttpClientInterface` your code will accept
1872+
the real client outside tests, while replacing it with
1873+
:class:`Symfony\\Component\\HttpClient\\MockHttpClient` in the test.
18291874

1830-
When the ``request`` method is used on ``MockHttpClient``, it will respond with
1831-
the supplied ``MockResponse``. There are a few ways to use it, as described
1832-
below.
1875+
When the ``request`` method is used on :class:`Symfony\\Component\\HttpClient\\MockHttpClient`,
1876+
it will respond with the supplied
1877+
:class:`Symfony\\Component\\HttpClient\\Response\\MockResponse`. There are a few ways to use
1878+
it, as described below.
18331879

18341880
HTTP Client and Responses
18351881
~~~~~~~~~~~~~~~~~~~~~~~~~
18361882

1837-
The first way of using ``MockHttpClient`` is to pass a list of responses to its
1838-
constructor. These will be yielded in order when requests are made::
1883+
The first way of using :class:`Symfony\\Component\\HttpClient\\MockHttpClient`
1884+
is to pass a list of responses to its constructor. These will be yielded
1885+
in order when requests are made::
18391886

18401887
use Symfony\Component\HttpClient\MockHttpClient;
18411888
use Symfony\Component\HttpClient\Response\MockResponse;
@@ -1850,8 +1897,8 @@ constructor. These will be yielded in order when requests are made::
18501897
$response1 = $client->request('...'); // returns $responses[0]
18511898
$response2 = $client->request('...'); // returns $responses[1]
18521899

1853-
Another way of using ``MockHttpClient`` is to pass a callback that generates the
1854-
responses dynamically when it's called::
1900+
Another way of using :class:`Symfony\\Component\\HttpClient\\MockHttpClient` is to
1901+
pass a callback that generates the responses dynamically when it's called::
18551902

18561903
use Symfony\Component\HttpClient\MockHttpClient;
18571904
use Symfony\Component\HttpClient\Response\MockResponse;
@@ -1888,7 +1935,9 @@ assertions on the request before returning the mocked response::
18881935
.. tip::
18891936

18901937
Instead of using the first argument, you can also set the (list of)
1891-
responses or callbacks using the ``setResponseFactory()`` method::
1938+
responses or callbacks using the
1939+
:method:`Symfony\\Component\\HttpClient\\MockHttpClient::setResponseFactory`
1940+
method::
18921941

18931942
$responses = [
18941943
new MockResponse($body1, $info1),
@@ -1912,10 +1961,12 @@ define the ``http_code`` option::
19121961
$response = $client->request('...');
19131962

19141963
The responses provided to the mock client don't have to be instances of
1915-
``MockResponse``. Any class implementing ``ResponseInterface`` will work (e.g.
1916-
``$this->createMock(ResponseInterface::class)``).
1964+
:class:`Symfony\\Component\\HttpClient\\Response\\MockResponse`. Any class
1965+
implementing :class:`Symfony\\Contracts\\HttpClient\\ResponseInterface`
1966+
will work (e.g. ``$this->createMock(ResponseInterface::class)``).
19171967

1918-
However, using ``MockResponse`` allows simulating chunked responses and timeouts::
1968+
However, using :class:`Symfony\\Component\\HttpClient\\Response\\MockResponse`
1969+
allows simulating chunked responses and timeouts::
19191970

19201971
$body = function () {
19211972
yield 'hello';
@@ -2027,7 +2078,8 @@ You can use :class:`Symfony\\Component\\HttpClient\\Response\\JsonMockResponse`
20272078
Testing Request Data
20282079
~~~~~~~~~~~~~~~~~~~~
20292080

2030-
The ``MockResponse`` class comes with some helper methods to test the request:
2081+
The :class:`Symfony\\Component\\HttpClient\\Response\\MockResponse` class comes
2082+
with some helper methods to test the request:
20312083

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

0 commit comments

Comments
 (0)