Skip to content

Commit 5ab1233

Browse files
committed
Minor tweaks
1 parent b703f5a commit 5ab1233

File tree

1 file changed

+24
-24
lines changed

1 file changed

+24
-24
lines changed

http_client.rst

Lines changed: 24 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -1498,7 +1498,8 @@ This allows using them where native PHP streams are needed::
14981498
Extensibility
14991499
-------------
15001500

1501-
In order to extend the behavior of a base HTTP client, decoration is the way to go::
1501+
If you want to extend the behavior of a base HTTP client, you can use
1502+
:doc:`service decoration </service_container/service_decoration>`::
15021503

15031504
class MyExtendedHttpClient implements HttpClientInterface
15041505
{
@@ -1511,11 +1512,11 @@ In order to extend the behavior of a base HTTP client, decoration is the way to
15111512

15121513
public function request(string $method, string $url, array $options = []): ResponseInterface
15131514
{
1514-
// do what you want here with $method, $url and/or $options
1515+
// process and/or change the $method, $url and/or $options as needed
1516+
$response = $this->decoratedClient->request($method, $url, $options);
15151517

1516-
$response = $this->decoratedClient->request();
1517-
1518-
//!\ calling any method on $response here would break async, see below for a better way
1518+
// if you call here any method on $response, the HTTP request
1519+
// won't be async; see below for a better way
15191520

15201521
return $response;
15211522
}
@@ -1526,13 +1527,13 @@ In order to extend the behavior of a base HTTP client, decoration is the way to
15261527
}
15271528
}
15281529

1529-
A decorator like this one is suited for use cases where processing the
1530-
requests' arguments is enough.
1530+
A decorator like this one is useful in cases where processing the requests'
1531+
arguments is enough. By decorating the ``on_progress`` option, you can
1532+
even implement basic monitoring of the response. However, since calling
1533+
responses' methods forces synchronous operations, doing so inside ``request()``
1534+
will break async.
15311535

1532-
By decorating the ``on_progress`` option, one can
1533-
even implement basic monitoring of the response. But since calling responses'
1534-
methods forces synchronous operations, doing so in ``request()`` breaks async.
1535-
The solution then is to also decorate the response object itself.
1536+
The solution is to also decorate the response object itself.
15361537
:class:`Symfony\\Component\\HttpClient\\TraceableHttpClient` and
15371538
:class:`Symfony\\Component\\HttpClient\\Response\\TraceableResponse` are good
15381539
examples as a starting point.
@@ -1551,10 +1552,9 @@ processing the stream of chunks as they come back from the network::
15511552

15521553
public function request(string $method, string $url, array $options = []): ResponseInterface
15531554
{
1554-
// do what you want here with $method, $url and/or $options
1555+
// process and/or change the $method, $url and/or $options as needed
15551556

15561557
$passthru = function (ChunkInterface $chunk, AsyncContext $context) {
1557-
15581558
// do what you want with chunks, e.g. split them
15591559
// in smaller chunks, group them, skip some, etc.
15601560

@@ -1571,19 +1571,19 @@ it shall return an
15711571
:class:`Symfony\\Component\\HttpClient\\Response\\AsyncResponse`.
15721572

15731573
The custom processing of chunks should happen in ``$passthru``: this generator
1574-
is where you need to write your logic. It will be called for each chunk yielded by
1575-
the underlying client. A ``$passthru`` that does nothing would just ``yield $chunk;``.
1576-
Of course, you could also yield a modified chunk, split the chunk into many
1574+
is where you need to write your logic. It will be called for each chunk yielded
1575+
by the underlying client. A ``$passthru`` that does nothing would just ``yield
1576+
$chunk;``. You could also yield a modified chunk, split the chunk into many
15771577
ones by yielding several times, or even skip a chunk altogether by issuing a
15781578
``return;`` instead of yielding.
15791579

15801580
In order to control the stream, the chunk passthru receives an
15811581
:class:`Symfony\\Component\\HttpClient\\Response\\AsyncContext` as second
15821582
argument. This context object has methods to read the current state of the
1583-
response. It also allows altering the response stream with methods to create new
1584-
chunks of content, pause the stream, cancel the stream, change the info of the
1585-
response, replace the current request by another one or change the chunk passthru
1586-
itself.
1583+
response. It also allows altering the response stream with methods to create
1584+
new chunks of content, pause the stream, cancel the stream, change the info of
1585+
the response, replace the current request by another one or change the chunk
1586+
passthru itself.
15871587

15881588
Checking the test cases implemented in
15891589
:class:`Symfony\\Component\\HttpClient\\Response\\Tests\\AsyncDecoratorTraitTest`
@@ -1594,10 +1594,10 @@ Here are the use cases that it simulates:
15941594
* send a preflight request, e.g. for authentication needs;
15951595
* issue subrequests and include their content in the main response's body.
15961596

1597-
The logic in :class:`Symfony\\Component\\HttpClient\\Response\\AsyncResponse` has
1598-
many safety checks that will throw a ``LogicException`` if the chunk passthru
1599-
doesn't behave correctly; e.g. if a chunk is yielded after an ``isLast()`` one,
1600-
or if a content chunk is yielded before an ``isFirst()`` one, etc.
1597+
The logic in :class:`Symfony\\Component\\HttpClient\\Response\\AsyncResponse`
1598+
has many safety checks that will throw a ``LogicException`` if the chunk
1599+
passthru doesn't behave correctly; e.g. if a chunk is yielded after an ``isLast()``
1600+
one, or if a content chunk is yielded before an ``isFirst()`` one, etc.
16011601

16021602
Testing HTTP Clients and Responses
16031603
----------------------------------

0 commit comments

Comments
 (0)