Skip to content

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 10 commits into from
Sep 22, 2015
Merged

Add BatchResult #48

merged 10 commits into from
Sep 22, 2015

Conversation

sagikazarmark
Copy link
Member

Fixes #47

{
if ($this->responses->contains($request)) {
return $this->responses[$request];
}
Copy link
Contributor

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.

Copy link
Member Author

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?)

Copy link
Member Author

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.

Copy link
Contributor

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.

Copy link
Contributor

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...

Copy link
Member Author

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.

Copy link
Contributor

@dbu dbu Sep 22, 2015 via email

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copy link
Member Author

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.

Copy link
Contributor

@dbu dbu Sep 22, 2015 via email

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copy link
Member Author

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
Copy link
Contributor

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
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

s/and exceptions //

@joelwurtz
Copy link
Member

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) {
             ...
         }
    }
}

@sagikazarmark
Copy link
Member Author

@joelwurtz Might be useful in cases where the underlying client does not support parallel requests. Internally it could rely on sendRequest method of the interface. Would you like to open a PR against the terminology branch?

@dbu
Copy link
Contributor

dbu commented Sep 22, 2015 via email

@sagikazarmark
Copy link
Member Author

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?

@joelwurtz
Copy link
Member

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)

@sagikazarmark
Copy link
Member Author

Actually, I think this is ready to merge. @dbu ?

@dbu
Copy link
Contributor

dbu commented Sep 22, 2015 via email

sagikazarmark added a commit that referenced this pull request Sep 22, 2015
@sagikazarmark sagikazarmark merged commit 2e16453 into terminology Sep 22, 2015
@sagikazarmark sagikazarmark deleted the batch_response branch September 22, 2015 19:50
@sagikazarmark
Copy link
Member Author

@joelwurtz PR merged. If you provide a PR, please do that under the Util namespace.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants