-
Notifications
You must be signed in to change notification settings - Fork 54
/
BatchClient.php
47 lines (38 loc) · 1.14 KB
/
BatchClient.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
<?php
namespace Http\Client\Common;
use Http\Client\Exception;
use Http\Client\HttpClient;
use Http\Client\Common\Exception\BatchException;
use Psr\Http\Message\RequestInterface;
use Psr\Http\Message\ResponseInterface;
final class BatchClient implements BatchClientInterface
{
/**
* @var HttpClient
*/
private $client;
public function __construct(HttpClient $client)
{
$this->client = $client;
}
public function sendRequest(RequestInterface $request): ResponseInterface
{
return $this->client->sendRequest($request);
}
public function sendRequests(array $requests): BatchResult
{
$batchResult = new BatchResult();
foreach ($requests as $request) {
try {
$response = $this->sendRequest($request);
$batchResult = $batchResult->addResponse($request, $response);
} catch (Exception $e) {
$batchResult = $batchResult->addException($request, $e);
}
}
if ($batchResult->hasExceptions()) {
throw new BatchException($batchResult);
}
return $batchResult;
}
}