Skip to content

Commit ab38cf3

Browse files
committed
feature #12221 [HttpClient] tell about ScopingHttpClient::forBaseUri() (nicolas-grekas)
This PR was merged into the 4.3 branch. Discussion ---------- [HttpClient] tell about ScopingHttpClient::forBaseUri() Fixes symfony/symfony#33274 Commits ------- 5e34a54 [HttpClient] tell about ScopingHttpClient::forBaseUri()
2 parents 1910f04 + 5e34a54 commit ab38cf3

File tree

1 file changed

+48
-31
lines changed

1 file changed

+48
-31
lines changed

components/http_client.rst

Lines changed: 48 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -31,8 +31,8 @@ low-level HTTP client that makes requests, like the following ``GET`` request::
3131

3232
use Symfony\Component\HttpClient\HttpClient;
3333

34-
$httpClient = HttpClient::create();
35-
$response = $httpClient->request('GET', 'https://api.github.com/repos/symfony/symfony-docs');
34+
$client = HttpClient::create();
35+
$response = $client->request('GET', 'https://api.github.com/repos/symfony/symfony-docs');
3636

3737
$statusCode = $response->getStatusCode();
3838
// $statusCode = 200
@@ -68,10 +68,10 @@ the transport explicitly, use the following classes to create the client::
6868
use Symfony\Component\HttpClient\NativeHttpClient;
6969

7070
// uses native PHP streams
71-
$httpClient = new NativeHttpClient();
71+
$client = new NativeHttpClient();
7272

7373
// uses the cURL PHP extension
74-
$httpClient = new CurlHttpClient();
74+
$client = new CurlHttpClient();
7575

7676
When using this component in a full-stack Symfony application, this behavior is
7777
not configurable and cURL will be used automatically if the cURL PHP extension
@@ -84,7 +84,7 @@ When requesting an ``https`` URL, HTTP/2 is enabled by default if libcurl >= 7.3
8484
is used. To force HTTP/2 for ``http`` URLs, you need to enable it explicitly via
8585
the ``http_version`` option::
8686

87-
$httpClient = HttpClient::create(['http_version' => '2.0']);
87+
$client = HttpClient::create(['http_version' => '2.0']);
8888

8989
Support for HTTP/2 PUSH works out of the box when libcurl >= 7.61 is used with
9090
PHP >= 7.2.17 / 7.3.4: pushed responses are put into a temporary cache and are
@@ -96,16 +96,16 @@ Making Requests
9696
The client created with the ``HttpClient`` class provides a single ``request()``
9797
method to perform all kinds of HTTP requests::
9898

99-
$response = $httpClient->request('GET', 'https://...');
100-
$response = $httpClient->request('POST', 'https://...');
101-
$response = $httpClient->request('PUT', 'https://...');
99+
$response = $client->request('GET', 'https://...');
100+
$response = $client->request('POST', 'https://...');
101+
$response = $client->request('PUT', 'https://...');
102102
// ...
103103

104104
Responses are always asynchronous, so that the call to the method returns
105105
immediately instead of waiting to receive the response::
106106

107107
// code execution continues immediately; it doesn't wait to receive the response
108-
$response = $httpClient->request('GET', 'http://releases.ubuntu.com/18.04.2/ubuntu-18.04.2-desktop-amd64.iso');
108+
$response = $client->request('GET', 'http://releases.ubuntu.com/18.04.2/ubuntu-18.04.2-desktop-amd64.iso');
109109

110110
// getting the response headers waits until they arrive
111111
$contentType = $response->getHeaders()['content-type'][0];
@@ -130,7 +130,7 @@ defined globally when creating the client (to apply it to all requests) and to
130130
each request (which overrides any global authentication)::
131131

132132
// Use the same authentication for all requests
133-
$httpClient = HttpClient::create([
133+
$client = HttpClient::create([
134134
// HTTP Basic authentication with only the username and not a password
135135
'auth_basic' => ['the-username'],
136136

@@ -141,7 +141,7 @@ each request (which overrides any global authentication)::
141141
'auth_bearer' => 'the-bearer-token',
142142
]);
143143

144-
$response = $httpClient->request('GET', 'https://...', [
144+
$response = $client->request('GET', 'https://...', [
145145
// use a different HTTP Basic authentication only for this request
146146
'auth_basic' => ['the-username', 'the-password'],
147147

@@ -155,7 +155,7 @@ You can either append them manually to the requested URL, or define them as an
155155
associative array via the ``query`` option, that will be merged with the URL::
156156

157157
// it makes an HTTP GET request to https://httpbin.org/get?token=...&name=...
158-
$response = $httpClient->request('GET', 'https://httpbin.org/get', [
158+
$response = $client->request('GET', 'https://httpbin.org/get', [
159159
// these values are automatically encoded before including them in the URL
160160
'query' => [
161161
'token' => '...',
@@ -170,13 +170,13 @@ Use the ``headers`` option to define both the default headers added to all
170170
requests and the specific headers for each request::
171171

172172
// this header is added to all requests made by this client
173-
$httpClient = HttpClient::create(['headers' => [
173+
$client = HttpClient::create(['headers' => [
174174
'User-Agent' => 'My Fancy App',
175175
]]);
176176

177177
// this header is only included in this request and overrides the value
178178
// of the same header if defined globally by the HTTP client
179-
$response = $httpClient->request('POST', 'https://...', [
179+
$response = $client->request('POST', 'https://...', [
180180
'headers' => [
181181
'Content-Type' => 'text/plain',
182182
],
@@ -189,7 +189,7 @@ This component provides several methods for uploading data using the ``body``
189189
option. You can use regular strings, closures, iterables and resources and they'll be
190190
processed automatically when making the requests::
191191

192-
$response = $httpClient->request('POST', 'https://...', [
192+
$response = $client->request('POST', 'https://...', [
193193
// defining data using a regular string
194194
'body' => 'raw data',
195195

@@ -222,7 +222,7 @@ A generator or any ``Traversable`` can also be used instead of a closure.
222222
given content will be JSON-encoded automatically and the request will add the
223223
``Content-Type: application/json`` automatically too::
224224

225-
$response = $httpClient->request('POST', 'https://...', [
225+
$response = $client->request('POST', 'https://...', [
226226
'json' => ['param1' => 'value1', '...'],
227227
]);
228228

@@ -265,7 +265,7 @@ making a request. Use the ``max_redirects`` setting to configure this behavior
265265
(if the number of redirects is higher than the configured value, you'll get a
266266
:class:`Symfony\\Component\\HttpClient\\Exception\\RedirectionException`)::
267267

268-
$response = $httpClient->request('GET', 'https://...', [
268+
$response = $client->request('GET', 'https://...', [
269269
// 0 means to not follow any redirect
270270
'max_redirects' => 0,
271271
]);
@@ -294,7 +294,7 @@ uploads/downloads as they complete. This callback is guaranteed to be called on
294294
DNS resolution, on arrival of headers and on completion; additionally it is
295295
called when new data is uploaded or downloaded and at least once per second::
296296

297-
$response = $httpClient->request('GET', 'https://...', [
297+
$response = $client->request('GET', 'https://...', [
298298
'on_progress' => function (int $dlNow, int $dlSize, array $info): void {
299299
// $dlNow is the number of bytes downloaded so far
300300
// $dlSize is the total size to be downloaded or -1 if it is unknown
@@ -319,7 +319,7 @@ The response returned by all HTTP clients is an object of type
319319
:class:`Symfony\\Contracts\\HttpClient\\ResponseInterface` which provides the
320320
following methods::
321321

322-
$response = $httpClient->request('GET', 'https://...');
322+
$response = $client->request('GET', 'https://...');
323323

324324
// gets the HTTP status code of the response
325325
$statusCode = $response->getStatusCode();
@@ -358,7 +358,7 @@ Call the ``stream()`` method of the HTTP client to get *chunks* of the
358358
response sequentially instead of waiting for the entire response::
359359

360360
$url = 'https://releases.ubuntu.com/18.04.1/ubuntu-18.04.1-desktop-amd64.iso';
361-
$response = $httpClient->request('GET', $url, [
361+
$response = $client->request('GET', $url, [
362362
// optional: if you don't want to buffer the response in memory
363363
'buffer' => false,
364364
]);
@@ -371,7 +371,7 @@ response sequentially instead of waiting for the entire response::
371371
// get the response contents in chunk and save them in a file
372372
// response chunks implement Symfony\Contracts\HttpClient\ChunkInterface
373373
$fileHandler = fopen('/ubuntu.iso', 'w');
374-
foreach ($httpClient->stream($response) as $chunk) {
374+
foreach ($client->stream($response) as $chunk) {
375375
fwrite($fileHandler, $chunk->getContent());
376376
}
377377

@@ -405,7 +405,7 @@ When the HTTP status code of the response is in the 300-599 range (i.e. 3xx,
405405
``getHeaders()`` and ``getContent()`` methods throw an appropriate exception::
406406

407407
// the response of this request will be a 403 HTTP error
408-
$response = $httpClient->request('GET', 'https://httpbin.org/status/403');
408+
$response = $client->request('GET', 'https://httpbin.org/status/403');
409409

410410
// this code results in a Symfony\Component\HttpClient\Exception\ClientException
411411
// because it doesn't check the status code of the response
@@ -600,25 +600,42 @@ class to autoconfigure the HTTP client based on the requested URL::
600600
'Authorization' => 'token '.$githubToken,
601601
],
602602
],
603+
// ...
603604
]);
604605

606+
You can define several scopes, so that each set of options is added only if a
607+
requested URL matches one of the regular expressions provided as keys.
608+
605609
If the request URL is relative (because you use the ``base_uri`` option), the
606610
scoping HTTP client can't make a match. That's why you can define a third
607611
optional argument in its constructor which will be considered the default
608612
regular expression applied to relative URLs::
609613

610614
// ...
611615

612-
$httpClient = new ScopingHttpClient($client, [
613-
'https://api\.github\.com/' => [
614-
'base_uri' => 'https://api.github.com/',
615-
// ...
616+
$client = new ScopingHttpClient($client,
617+
[
618+
'https://api\.github\.com/' => [
619+
'base_uri' => 'https://api.github.com/',
620+
// ...
621+
],
616622
],
617-
],
618-
// this is the regexp applied to all relative URLs
623+
// this is the index in the previous array that defines
624+
// the base URI that shoud be used to resolve relative URLs
619625
'https://api\.github\.com/'
620626
);
621627

628+
The above example can be reduced to a simpler call::
629+
630+
// ...
631+
632+
$client = ScopingHttpClient::forBaseUri($client, 'https://api.github.com/', [
633+
// ...
634+
]);
635+
636+
This way, the provided options will be used only if the requested URL is relative
637+
or if it matches the ``https://api.github.com/`` base URI.
638+
622639
Interoperability
623640
----------------
624641

@@ -744,11 +761,11 @@ into any services by type-hinting a constructor argument with the
744761

745762
class SomeService
746763
{
747-
private $httpClient;
764+
private $client;
748765

749-
public function __construct(HttpClientInterface $httpClient)
766+
public function __construct(HttpClientInterface $client)
750767
{
751-
$this->httpClient = $httpClient;
768+
$this->client = $client;
752769
}
753770
}
754771

0 commit comments

Comments
 (0)