Skip to content

Commit 26c9257

Browse files
committed
Merge pull request #54 from joelwurtz/feature/batch-requests
Add sendRequests method as a trait
2 parents 2e16453 + 2627a80 commit 26c9257

File tree

2 files changed

+102
-0
lines changed

2 files changed

+102
-0
lines changed

spec/Util/BatchRequestSpec.php

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
<?php
2+
3+
namespace spec\Http\Client\Util;
4+
5+
use Http\Client\HttpPsrClient;
6+
use Http\Client\Util\BatchRequest;
7+
use PhpSpec\ObjectBehavior;
8+
use Psr\Http\Message\RequestInterface;
9+
use Psr\Http\Message\ResponseInterface;
10+
11+
class BatchRequestSpec extends ObjectBehavior
12+
{
13+
function let(HttpPsrClient $client)
14+
{
15+
$this->beAnInstanceOf('spec\Http\Client\Util\BatchRequestStub', [$client]);
16+
}
17+
18+
function it_send_multiple_request_using_send_request(HttpPsrClient $client, RequestInterface $request1, RequestInterface $request2, ResponseInterface $response1, ResponseInterface $response2)
19+
{
20+
$client->sendRequest($request1, [])->willReturn($response1);
21+
$client->sendRequest($request2, [])->willReturn($response2);
22+
23+
$this->sendRequests([$request1, $request2], [])->shouldReturnAnInstanceOf('\Http\Client\BatchResult');
24+
}
25+
26+
function it_throw_batch_exception_if_one_or_more_request_failed(HttpPsrClient $client, RequestInterface $request1, RequestInterface $request2, ResponseInterface $response)
27+
{
28+
$client->sendRequest($request1, [])->willReturn($response);
29+
$client->sendRequest($request2, [])->willThrow('\Http\Client\Exception\HttpException');
30+
31+
$this->shouldThrow('\Http\Client\Exception\BatchException')->duringSendRequests([$request1, $request2], []);
32+
}
33+
}
34+
35+
class BatchRequestStub implements HttpPsrClient
36+
{
37+
use BatchRequest;
38+
39+
protected $client;
40+
41+
public function __construct(HttpPsrClient $client)
42+
{
43+
$this->client = $client;
44+
}
45+
46+
/**
47+
* {@inheritdoc}
48+
*/
49+
public function sendRequest(RequestInterface $request, array $options = [])
50+
{
51+
return $this->client->sendRequest($request, $options);
52+
}
53+
}

src/Util/BatchRequest.php

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
<?php
2+
3+
namespace Http\Client\Util;
4+
5+
use Http\Client\BatchResult;
6+
use Http\Client\Exception;
7+
use Http\Client\Exception\BatchException;
8+
use Psr\Http\Message\RequestInterface;
9+
10+
/**
11+
* Implements sending multiple request for client not supporting parallels requests
12+
*
13+
* Internally, use the sendRequest method
14+
*
15+
* Should be used with Http\Client\HttpPsrClient
16+
*
17+
* @author Joel Wurtz <[email protected]>
18+
*/
19+
trait BatchRequest
20+
{
21+
/**
22+
* {@inheritdoc}
23+
*/
24+
abstract public function sendRequest(RequestInterface $request, array $options = []);
25+
26+
/**
27+
* {@inheritdoc}
28+
*/
29+
public function sendRequests(array $requests, array $options = [])
30+
{
31+
$batchResult = new BatchResult();
32+
$batchException = new BatchException();
33+
$batchException->setResult($batchResult);
34+
35+
foreach ($requests as $request) {
36+
try {
37+
$batchResult->addResponse($request, $this->sendRequest($request, $options));
38+
} catch (Exception $e) {
39+
$batchException->addException($request, $e);
40+
}
41+
}
42+
43+
if (count($batchException->getExceptions()) > 0) {
44+
throw $batchException;
45+
}
46+
47+
return $batchResult;
48+
}
49+
}

0 commit comments

Comments
 (0)