@@ -76,3 +76,37 @@ If one or more of the requests throw exceptions, they are added to the
76
76
} catch (BatchException $e) {
77
77
var_dump($e->getResult()->getExceptions());
78
78
}
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