-
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
Add BatchResult #48
Conversation
{ | ||
if ($this->responses->contains($request)) { | ||
return $this->responses[$request]; | ||
} |
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.
throw the exception we find at $request
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
} | ||
|
||
/** | ||
* Returns all exceptions | ||
* Returns successful responses or null |
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.
Returns a BatchResult object which contains succesful responses and exceptions for failed requests.
use Psr\Http\Message\ResponseInterface; | ||
|
||
/** | ||
* Successful responses and exceptions returned from parallel request execution |
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.
s/and exceptions //
Maybe providing a Trait for the method sendRequests on the HttpPsrClient (using the sendRequest method) would be useful for client/adapter not handling this use case ? trait BatchRequest {
public function sendRequests($requests)
{
foreach ($requests as $request) {
...
}
}
} |
@joelwurtz Might be useful in cases where the underlying client does not support parallel requests. Internally it could rely on |
the idea makes sense. i just wonder how much implementation this
repository should provide. maybe we could have an adapter-base (or
something like that) repository that provides a trait and other utility
things we come up with. the trait not on the same level as the spec itself.
|
Yeah, that's a good point as well. I would say, until we don't have enough utilities, collect them at one place. Before release, we can separate components which are not part of the spec. The utility namespace could even be the same or we can add the Util word to the namespace. WDYT? |
No problem for making a PR @sagikazarmark will wait to have this merged before doing it (so no changes need to be made with the new BatchResult object) |
Actually, I think this is ready to merge. @dbu ? |
agreed. and lets have a Util namespace or something right away
|
@joelwurtz PR merged. If you provide a PR, please do that under the Util namespace. |
Fixes #47