-
Notifications
You must be signed in to change notification settings - Fork 56
Add documentation for BatchClient, BatchResult and BatchException #20
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -23,3 +23,55 @@ $foo = $client->get('http://example.com/foo'); | |
$bar = $client->get('http://example.com/bar', ['accept-encoding' => 'application/json']); | ||
$post = $client->post('http://example.com/update', [], 'My post body'); | ||
``` | ||
|
||
## BatchClient | ||
|
||
This client wraps a HttpClient and extends it with the possibility to send an array of requests and to retrieve their responses as a `BatchResult`. | ||
|
||
``` php | ||
use Http\Discovery\HttpClientDiscovery; | ||
use Http\Discovery\MessageFactoryDiscovery; | ||
|
||
$messageFactory = MessageFactoryDiscovery::find(); | ||
|
||
$requests = [ | ||
$messageFactory->createRequest('GET', 'http://example.com/foo'), | ||
$messageFactory->createRequest('POST', 'http://example.com/update', [], 'My post body'), | ||
]; | ||
|
||
$client = new BatchClient( | ||
HttpClientDiscovery::find() | ||
); | ||
|
||
$batchResult = $client->sendRequests($requests); | ||
``` | ||
|
||
The `BatchResult` itself is an object that contains responses for all requests sent. It provides methods that give appropriate information based on a given request. | ||
|
||
``` php | ||
$requests = [ | ||
$messageFactory->createRequest('GET', 'http://example.com/foo'), | ||
$messageFactory->createRequest('POST', 'http://example.com/update', [], 'My post body'), | ||
]; | ||
|
||
$batchResult = $client->sendRequests($requests); | ||
|
||
if ($batchResult->hasResponses()) { | ||
$fooSuccessful = $batchResult->isSuccesful($requests[0]); | ||
$updateResponse = $batchResult->getResponseFor($request[1]); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. this is assuming that the post request did not fail and would throw an exception if it failed. in this flow of code, that can't happen as we would have bailed out with an exception. but that means $fooSuccessful is pointless. i would move the isSuccessful check below to the error handling logic, as its only relevant in the error case to figure out if something went wrong or not. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Wow, my breain blown up while following this reasoning (guess it's sunday evening), so I just agree with you. 😉 There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. yeah, sunday evening here as well :-D i think in the middle i realized that we can just expect the response to be there in this case... but we should separate the examples to make it more clear. we could even mention that we can just assume responses are here as otherwise there would be the exception thrown. |
||
} | ||
``` | ||
|
||
If one or more of the requests throw exceptions, they are added to the `BatchResult` and the `BatchClient` will ultimately throw a `BatchException` containing the `BatchResult` and therefore its exceptions. | ||
|
||
``` php | ||
$requests = [ | ||
$messageFactory->createRequest('GET', 'http://example.com/update'), | ||
]; | ||
|
||
try { | ||
$batchResult = $client->sendRequests($requests); | ||
} catch (BatchException $e) { | ||
var_dump($e->getResult()->getExceptions()); | ||
} | ||
``` |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
this is not needed in this flow. if there was a failure, we drop out with an exception
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Only case it might make sense is when you pass an empty array. In that case there shouldn't be any response.