Skip to content

Commit 684e6e7

Browse files
committed
Add documentation for HTTP Client router
1 parent 92cd365 commit 684e6e7

File tree

1 file changed

+34
-0
lines changed

1 file changed

+34
-0
lines changed

components/client-common.rst

+34
Original file line numberDiff line numberDiff line change
@@ -76,3 +76,37 @@ If one or more of the requests throw exceptions, they are added to the
7676
} catch (BatchException $e) {
7777
var_dump($e->getResult()->getExceptions());
7878
}
79+
80+
81+
HTTP Client Router
82+
------------------
83+
84+
This client accepts pairs of clients (both sync and async) and request matchers.
85+
Every request is "routed" through this single client, checked against the request matchers
86+
and sent using the appropriate client. If there is no matching client, an exception is thrown.
87+
88+
This allows to inject a single client into multiple services,
89+
but under the hood there can be clients configured for each of them::
90+
91+
use Http\Client\Common\HttpClientRouter;
92+
use Http\Discovery\HttpClientDiscovery;
93+
use Http\Message\RequestMatcher\RequestMatcher;
94+
use Http\Discovery\MessageFactoryDiscovery;
95+
96+
$client = new HttpClientRouter();
97+
98+
$requestMatcher = new RequestMatcher(null, 'api.example.com');
99+
100+
$client->addClient(HttpClientDiscovery::find(), $requestMatcher);
101+
102+
$messageFactory = MessageFactoryDiscovery::find();
103+
104+
$request = $messageFactory->createRequest('GET', 'http://api.example.com/update');
105+
106+
// Returns a response
107+
$client->send($request);
108+
109+
$request = $messageFactory->createRequest('GET', 'http://api2.example.com/update');
110+
111+
// Throws an Http\Client\Exception\RequestException
112+
$client->send($request);

0 commit comments

Comments
 (0)