@@ -732,7 +732,7 @@ original HTTP client::
732
732
733
733
$client = new RetryableHttpClient(HttpClient::create());
734
734
735
- The `` RetryableHttpClient ` ` uses a
735
+ The :class: ` Symfony \\ Component \\ HttpClient \\ RetryableHttpClient ` uses a
736
736
:class: `Symfony\\ Component\\ HttpClient\\ Retry\\ RetryStrategyInterface ` to
737
737
decide if the request should be retried, and to define the waiting time between
738
738
each retry.
@@ -823,7 +823,8 @@ called when new data is uploaded or downloaded and at least once per second::
823
823
]);
824
824
825
825
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.
827
828
828
829
HTTPS Certificates
829
830
~~~~~~~~~~~~~~~~~~
@@ -972,9 +973,10 @@ This component supports both the native PHP streams and cURL to make the HTTP
972
973
requests. Although both are interchangeable and provide the same features,
973
974
including concurrent requests, HTTP/2 is only supported when using cURL.
974
975
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::
978
980
979
981
use Symfony\Component\HttpClient\CurlHttpClient;
980
982
use Symfony\Component\HttpClient\NativeHttpClient;
@@ -1145,8 +1147,9 @@ following methods::
1145
1147
Streaming Responses
1146
1148
~~~~~~~~~~~~~~~~~~~
1147
1149
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::
1150
1153
1151
1154
$url = 'https://releases.ubuntu.com/18.04.1/ubuntu-18.04.1-desktop-amd64.iso';
1152
1155
$response = $client->request('GET', $url);
@@ -1176,8 +1179,7 @@ Canceling Responses
1176
1179
1177
1180
To abort a request (e.g. because it didn't complete in due time, or you want to
1178
1181
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 `::
1181
1183
1182
1184
$response->cancel();
1183
1185
@@ -1295,10 +1297,12 @@ If you look again at the snippet above, responses are read in requests' order.
1295
1297
But maybe the 2nd response came back before the 1st? Fully asynchronous operations
1296
1298
require being able to deal with the responses in whatever order they come back.
1297
1299
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::
1302
1306
1303
1307
foreach ($client->stream($responses) as $response => $chunk) {
1304
1308
if ($chunk->isFirst()) {
@@ -1437,7 +1441,8 @@ installed in your application::
1437
1441
// this won't hit the network if the resource is already in the cache
1438
1442
$response = $client->request('GET', 'https://example.com/cacheable-resource');
1439
1443
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 `.
1441
1446
1442
1447
Consuming Server-Sent Events
1443
1448
----------------------------
@@ -1625,11 +1630,49 @@ The `HTTPlug`_ v1 specification was published before PSR-18 and is superseded by
1625
1630
it. As such, you should not use it in newly written code. The component is still
1626
1631
interoperable with libraries that require it thanks to the
1627
1632
: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);
1630
1673
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
1633
1676
``guzzlehttp/promises `` package:
1634
1677
1635
1678
.. code-block :: terminal
@@ -1822,20 +1865,24 @@ external service. By not making actual HTTP requests there is no need to worry a
1822
1865
the service being online or the request changing state, for example deleting
1823
1866
a resource.
1824
1867
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.
1829
1874
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.
1833
1879
1834
1880
HTTP Client and Responses
1835
1881
~~~~~~~~~~~~~~~~~~~~~~~~~
1836
1882
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::
1839
1886
1840
1887
use Symfony\Component\HttpClient\MockHttpClient;
1841
1888
use Symfony\Component\HttpClient\Response\MockResponse;
@@ -1850,8 +1897,8 @@ constructor. These will be yielded in order when requests are made::
1850
1897
$response1 = $client->request('...'); // returns $responses[0]
1851
1898
$response2 = $client->request('...'); // returns $responses[1]
1852
1899
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::
1855
1902
1856
1903
use Symfony\Component\HttpClient\MockHttpClient;
1857
1904
use Symfony\Component\HttpClient\Response\MockResponse;
@@ -1888,7 +1935,9 @@ assertions on the request before returning the mocked response::
1888
1935
.. tip ::
1889
1936
1890
1937
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::
1892
1941
1893
1942
$responses = [
1894
1943
new MockResponse($body1, $info1),
@@ -1912,10 +1961,12 @@ define the ``http_code`` option::
1912
1961
$response = $client->request('...');
1913
1962
1914
1963
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) ``).
1917
1967
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::
1919
1970
1920
1971
$body = function () {
1921
1972
yield 'hello';
@@ -2027,7 +2078,8 @@ You can use :class:`Symfony\\Component\\HttpClient\\Response\\JsonMockResponse`
2027
2078
Testing Request Data
2028
2079
~~~~~~~~~~~~~~~~~~~~
2029
2080
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:
2031
2083
2032
2084
* ``getRequestMethod() `` - returns the HTTP method;
2033
2085
* ``getRequestUrl() `` - returns the URL the request would be sent to;
0 commit comments