-
Notifications
You must be signed in to change notification settings - Fork 39
Add BatchResult #48
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
Merged
Merged
Add BatchResult #48
Changes from all commits
Commits
Show all changes
10 commits
Select commit
Hold shift + click to select a range
cf38fcc
Add BatchResult
sagikazarmark 8d6abb2
Remove decision maker method
sagikazarmark e6acb53
Fix description
sagikazarmark f68ad2c
Move exceptions to BatchException
sagikazarmark 437a2e1
Improve tests
sagikazarmark 3950679
Improve documentation
sagikazarmark 9cb62d9
Add tests
sagikazarmark 708c447
Remove hasExceptions from BatchException
sagikazarmark 3eb7cbc
Throw invalid argument exception if request is not found
sagikazarmark 44e8c01
Add custom UnexpectedValueException, throw exception when request is …
sagikazarmark File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
<?php | ||
|
||
namespace spec\Http\Client; | ||
|
||
use Psr\Http\Message\RequestInterface; | ||
use Psr\Http\Message\ResponseInterface; | ||
use PhpSpec\ObjectBehavior; | ||
|
||
class BatchResultSpec extends ObjectBehavior | ||
{ | ||
function it_is_initializable() | ||
{ | ||
$this->shouldHaveType('Http\Client\BatchResult'); | ||
} | ||
|
||
function it_is_immutable(RequestInterface $request, ResponseInterface $response) | ||
{ | ||
$new = $this->addResponse($request, $response); | ||
|
||
$this->getResponses()->shouldReturn([]); | ||
$new->shouldHaveType('Http\Client\BatchResult'); | ||
$new->getResponses()->shouldReturn([$response]); | ||
} | ||
|
||
function it_has_a_responses(RequestInterface $request, ResponseInterface $response) | ||
{ | ||
$new = $this->addResponse($request, $response); | ||
|
||
$this->hasResponses()->shouldReturn(false); | ||
$this->getResponses()->shouldReturn([]); | ||
$new->hasResponses()->shouldReturn(true); | ||
$new->getResponses()->shouldReturn([$response]); | ||
} | ||
|
||
function it_has_a_response_for_a_request(RequestInterface $request, ResponseInterface $response) | ||
{ | ||
$new = $this->addResponse($request, $response); | ||
|
||
$this->shouldThrow('Http\Client\Exception\UnexpectedValueException')->duringGetResponseFor($request); | ||
$this->hasResponseFor($request)->shouldReturn(false); | ||
$new->getResponseFor($request)->shouldReturn($response); | ||
$new->hasResponseFor($request)->shouldReturn(true); | ||
} | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
<?php | ||
|
||
namespace spec\Http\Client\Exception; | ||
|
||
use PhpSpec\ObjectBehavior; | ||
|
||
class UnexpectedValueExceptionSpec extends ObjectBehavior | ||
{ | ||
function it_is_initializable() | ||
{ | ||
$this->shouldHaveType('Http\Client\Exception\UnexpectedValueException'); | ||
} | ||
|
||
function it_is_invalid_argument_exception() | ||
{ | ||
$this->shouldHaveType('UnexpectedValueException'); | ||
} | ||
|
||
function it_is_exception() | ||
{ | ||
$this->shouldImplement('Http\Client\Exception'); | ||
} | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,104 @@ | ||
<?php | ||
|
||
namespace Http\Client; | ||
|
||
use Http\Client\Exception\UnexpectedValueException; | ||
use Psr\Http\Message\RequestInterface; | ||
use Psr\Http\Message\ResponseInterface; | ||
|
||
/** | ||
* Successful responses returned from parallel request execution | ||
* | ||
* @author Márk Sági-Kazár <[email protected]> | ||
*/ | ||
final class BatchResult | ||
{ | ||
/** | ||
* @var \SplObjectStorage | ||
*/ | ||
private $responses; | ||
|
||
public function __construct() | ||
{ | ||
$this->responses = new \SplObjectStorage(); | ||
} | ||
|
||
/** | ||
* Returns all successful responses | ||
* | ||
* @return ResponseInterface[] | ||
*/ | ||
public function getResponses() | ||
{ | ||
$responses = []; | ||
|
||
foreach ($this->responses as $request) { | ||
$responses[] = $this->responses[$request]; | ||
} | ||
|
||
return $responses; | ||
} | ||
|
||
/** | ||
* Returns a response of a request | ||
* | ||
* @param RequestInterface $request | ||
* | ||
* @return ResponseInterface | ||
* | ||
* @throws UnexpectedValueException | ||
*/ | ||
public function getResponseFor(RequestInterface $request) | ||
{ | ||
try { | ||
return $this->responses[$request]; | ||
} catch (\UnexpectedValueException $e) { | ||
throw new UnexpectedValueException('Request not found', $e->getCode(), $e); | ||
} | ||
} | ||
|
||
/** | ||
* Checks if there are any successful responses at all | ||
* | ||
* @return boolean | ||
*/ | ||
public function hasResponses() | ||
{ | ||
return $this->responses->count() > 0; | ||
} | ||
|
||
/** | ||
* Checks if there is a response of a request | ||
* | ||
* @param RequestInterface $request | ||
* | ||
* @return ResponseInterface | ||
*/ | ||
public function hasResponseFor(RequestInterface $request) | ||
{ | ||
return $this->responses->contains($request); | ||
} | ||
|
||
/** | ||
* Adds a response in an immutable way | ||
* | ||
* @param RequestInterface $request | ||
* @param ResponseInterface $response | ||
* | ||
* @return BatchResult | ||
* | ||
* @internal | ||
*/ | ||
public function addResponse(RequestInterface $request, ResponseInterface $response) | ||
{ | ||
$new = clone $this; | ||
$new->responses->attach($request, $response); | ||
|
||
return $new; | ||
} | ||
|
||
public function __clone() | ||
{ | ||
$this->responses = clone $this->responses; | ||
} | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
otherwise, we should look in the exceptions to see if there is an exception carrying a response and return that. if that is not found, i would throw the ecxeption we find at $request. or an invalid argument exception if the request does not exist.
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.
Currently responses are handled the following way: every response and exception is collected in the BatchResult. If there are any exceptions, The BatchException gets the result and it is thrown.
There is a getExceptionFor method to get the exception. If that exception is HttpException, you can get a response. I would not mix the two methods.
I also wouldn't throw the exception, because that way you cannot process the rest.
The reason why I put exceptions here as well is that an exception cannot be immutable. But it would possibly be better, to collect exceptions somewhere else (eg. right in the batch 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.
I have an ugly idea for that. 😜
In batch exception accept two objects: batch result, exception chain
The exception chain would be an object which accepts a request, an exception and the previous/next exception. THis way you have one object in the end to be injected into the batch exception. You can always get the next exception by calling getNextException, which returns the next in the chain.
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.
hm. when i think of responses, i might want to process all responses,
even unsuccessful ones. but it indeed could get very confusing. so lets
adjust phpdoc to always say "successful responses" and throw an
exception if i try to request a response that does not exist (maybe with
a hint that the request failed, telling the user he should check with
the isSuccessful / isFailed method.
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.
I dunno about throwing it...
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 what I tried first. The problem is that an Exception cannot be cloned, so making it immutable that way doesn't work.
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.
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.
Should we do this in case of the BatchResult as well? I think it would be weird if the Result is immutable, the exception is not.
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.
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.
Done