@@ -727,7 +727,7 @@ original HTTP client::
727
727
728
728
$client = new RetryableHttpClient(HttpClient::create());
729
729
730
- The `` RetryableHttpClient ` ` uses a
730
+ The :class: ` Symfony \\ Component \\ HttpClient \\ RetryableHttpClient ` uses a
731
731
:class: `Symfony\\ Component\\ HttpClient\\ Retry\\ RetryStrategyInterface ` to
732
732
decide if the request should be retried, and to define the waiting time between
733
733
each retry.
@@ -818,7 +818,8 @@ called when new data is uploaded or downloaded and at least once per second::
818
818
]);
819
819
820
820
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.
822
823
823
824
HTTPS Certificates
824
825
~~~~~~~~~~~~~~~~~~
@@ -967,9 +968,10 @@ This component supports both the native PHP streams and cURL to make the HTTP
967
968
requests. Although both are interchangeable and provide the same features,
968
969
including concurrent requests, HTTP/2 is only supported when using cURL.
969
970
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::
973
975
974
976
use Symfony\Component\HttpClient\CurlHttpClient;
975
977
use Symfony\Component\HttpClient\NativeHttpClient;
@@ -1140,8 +1142,9 @@ following methods::
1140
1142
Streaming Responses
1141
1143
~~~~~~~~~~~~~~~~~~~
1142
1144
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::
1145
1148
1146
1149
$url = 'https://releases.ubuntu.com/18.04.1/ubuntu-18.04.1-desktop-amd64.iso';
1147
1150
$response = $client->request('GET', $url);
@@ -1171,8 +1174,7 @@ Canceling Responses
1171
1174
1172
1175
To abort a request (e.g. because it didn't complete in due time, or you want to
1173
1176
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 `::
1176
1178
1177
1179
$response->cancel();
1178
1180
@@ -1290,10 +1292,12 @@ If you look again at the snippet above, responses are read in requests' order.
1290
1292
But maybe the 2nd response came back before the 1st? Fully asynchronous operations
1291
1293
require being able to deal with the responses in whatever order they come back.
1292
1294
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::
1297
1301
1298
1302
foreach ($client->stream($responses) as $response => $chunk) {
1299
1303
if ($chunk->isFirst()) {
@@ -1432,7 +1436,8 @@ installed in your application::
1432
1436
// this won't hit the network if the resource is already in the cache
1433
1437
$response = $client->request('GET', 'https://example.com/cacheable-resource');
1434
1438
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 `.
1436
1441
1437
1442
Consuming Server-Sent Events
1438
1443
----------------------------
@@ -1620,11 +1625,49 @@ The `HTTPlug`_ v1 specification was published before PSR-18 and is superseded by
1620
1625
it. As such, you should not use it in newly written code. The component is still
1621
1626
interoperable with libraries that require it thanks to the
1622
1627
: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);
1625
1668
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
1628
1671
``guzzlehttp/promises `` package:
1629
1672
1630
1673
.. code-block :: terminal
@@ -1817,20 +1860,24 @@ external service. By not making actual HTTP requests there is no need to worry a
1817
1860
the service being online or the request changing state, for example deleting
1818
1861
a resource.
1819
1862
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.
1824
1869
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.
1828
1874
1829
1875
HTTP Client and Responses
1830
1876
~~~~~~~~~~~~~~~~~~~~~~~~~
1831
1877
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::
1834
1881
1835
1882
use Symfony\Component\HttpClient\MockHttpClient;
1836
1883
use Symfony\Component\HttpClient\Response\MockResponse;
@@ -1845,8 +1892,8 @@ constructor. These will be yielded in order when requests are made::
1845
1892
$response1 = $client->request('...'); // returns $responses[0]
1846
1893
$response2 = $client->request('...'); // returns $responses[1]
1847
1894
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::
1850
1897
1851
1898
use Symfony\Component\HttpClient\MockHttpClient;
1852
1899
use Symfony\Component\HttpClient\Response\MockResponse;
@@ -1883,7 +1930,9 @@ assertions on the request before returning the mocked response::
1883
1930
.. tip ::
1884
1931
1885
1932
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::
1887
1936
1888
1937
$responses = [
1889
1938
new MockResponse($body1, $info1),
@@ -1907,10 +1956,12 @@ define the ``http_code`` option::
1907
1956
$response = $client->request('...');
1908
1957
1909
1958
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) ``).
1912
1962
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::
1914
1965
1915
1966
$body = function () {
1916
1967
yield 'hello';
@@ -2022,7 +2073,8 @@ You can use :class:`Symfony\\Component\\HttpClient\\Response\\JsonMockResponse`
2022
2073
Testing Request Data
2023
2074
~~~~~~~~~~~~~~~~~~~~
2024
2075
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:
2026
2078
2027
2079
* ``getRequestMethod() `` - returns the HTTP method;
2028
2080
* ``getRequestUrl() `` - returns the URL the request would be sent to;
0 commit comments