From 016d8f3291c1f30d7c0edfb9a829b1e5bad7dc81 Mon Sep 17 00:00:00 2001 From: Joel Wurtz Date: Sun, 25 Oct 2015 22:53:34 +0100 Subject: [PATCH 1/2] Replace sendRequests with an async approach --- spec/Exception/BatchExceptionSpec.php | 36 --------- src/BatchResult.php | 103 -------------------------- src/HttpAsyncClient.php | 24 ++++++ src/HttpClient.php | 21 +----- src/Promise.php | 22 ++++++ 5 files changed, 47 insertions(+), 159 deletions(-) delete mode 100644 spec/Exception/BatchExceptionSpec.php delete mode 100644 src/BatchResult.php create mode 100644 src/HttpAsyncClient.php create mode 100644 src/Promise.php diff --git a/spec/Exception/BatchExceptionSpec.php b/spec/Exception/BatchExceptionSpec.php deleted file mode 100644 index b90dd47..0000000 --- a/spec/Exception/BatchExceptionSpec.php +++ /dev/null @@ -1,36 +0,0 @@ -beConstructedWith($batchResult); - } - - function it_is_initializable() - { - $this->shouldHaveType('Http\Client\Exception\BatchException'); - } - - function it_is_a_runtime_exception() - { - $this->shouldHaveType('RuntimeException'); - } - - function it_is_an_exception() - { - $this->shouldImplement('Http\Client\Exception'); - } - - function it_has_a_batch_result() - { - $this->getResult()->shouldHaveType('Http\Client\BatchResult'); - } -} diff --git a/src/BatchResult.php b/src/BatchResult.php deleted file mode 100644 index 472d21a..0000000 --- a/src/BatchResult.php +++ /dev/null @@ -1,103 +0,0 @@ - - */ -interface BatchResult -{ - /** - * Checks if there are any successful responses at all. - * - * @return boolean - */ - public function hasResponses(); - - /** - * Returns all successful responses. - * - * @return ResponseInterface[] - */ - public function getResponses(); - - /** - * Checks if there is a successful response for a request. - * - * @param RequestInterface $request - * - * @return boolean - */ - public function isSuccessful(RequestInterface $request); - - /** - * Returns the response for a successful request. - * - * @param RequestInterface $request - * - * @return ResponseInterface - * - * @throws \UnexpectedValueException If request was not part of the batch or failed. - */ - public function getResponseFor(RequestInterface $request); - - /** - * Adds a response in an immutable way. - * - * @param RequestInterface $request - * @param ResponseInterface $response - * - * @return BatchResult the new BatchResult with this request-response pair added to it. - */ - public function addResponse(RequestInterface $request, ResponseInterface $response); - - /** - * Checks if there are any unsuccessful requests at all. - * - * @return boolean - */ - public function hasExceptions(); - - /** - * Returns all exceptions for the unsuccessful requests. - * - * @return Exception[] - */ - public function getExceptions(); - - /** - * Checks if there is an exception for a request, meaning the request failed. - * - * @param RequestInterface $request - * - * @return boolean - */ - public function isFailed(RequestInterface $request); - - /** - * Returns the exception for a failed request. - * - * @param RequestInterface $request - * - * @return Exception - * - * @throws \UnexpectedValueException If request was not part of the batch or was successful. - */ - public function getExceptionFor(RequestInterface $request); - - /** - * Adds an exception in an immutable way. - * - * @param RequestInterface $request - * @param Exception $exception - * - * @return BatchResult the new BatchResult with this request-exception pair added to it. - */ - public function addException(RequestInterface $request, Exception $exception); -} diff --git a/src/HttpAsyncClient.php b/src/HttpAsyncClient.php new file mode 100644 index 0000000..737ee63 --- /dev/null +++ b/src/HttpAsyncClient.php @@ -0,0 +1,24 @@ + + */ +interface HttpAsyncClient +{ + /** + * Sends a PSR-7 request in an asynchronous way. + * + * @param RequestInterface $request + * + * @return Promise + * + * @throws Exception + */ + public function sendAsyncRequest(RequestInterface $request); +} diff --git a/src/HttpClient.php b/src/HttpClient.php index d5c2187..fa66c2c 100644 --- a/src/HttpClient.php +++ b/src/HttpClient.php @@ -2,12 +2,11 @@ namespace Http\Client; -use Http\Client\Exception\BatchException; use Psr\Http\Message\RequestInterface; use Psr\Http\Message\ResponseInterface; /** - * Sends one or more PSR-7 Request and returns PSR-7 responses. + * Sends a PSR-7 Request and returns a PSR-7 response. * * @author GeLo * @author Márk Sági-Kazár @@ -25,22 +24,4 @@ interface HttpClient * @throws Exception */ public function sendRequest(RequestInterface $request); - - /** - * Sends several PSR-7 requests. - * - * If the client is able to, these requests should be sent in parallel. Otherwise they will be sent sequentially. - * Either way, the caller may not rely on them being executed in any particular order. - * - * If one or more requests led to an exception, the BatchException is thrown. The BatchException gives access to the - * BatchResult that contains responses for successful calls and exceptions for unsuccessful calls. - * - * @param RequestInterface[] $requests - * - * @return BatchResult If all requests where successful. - * - * @throws Exception On general setup problems. - * @throws BatchException If one or more requests led to exceptions. - */ - public function sendRequests(array $requests); } diff --git a/src/Promise.php b/src/Promise.php new file mode 100644 index 0000000..5b1ad74 --- /dev/null +++ b/src/Promise.php @@ -0,0 +1,22 @@ + + */ +interface Promise +{ + /** + * Add behavior for when the promise is resolved or rejected (response will be available, or error happens) + * + * @param callable $onFulfilled Called when a response will be available, it will receive a Psr\Http\Message\RequestInterface object as the first argument + * @param callable $onRejected Called when an error happens, it will receive a Http\Client\Exception object as the first argument + * + * @return Promise Always returns a new promise which is resolved with value of the executed callback (onFulfilled / onRejected) + */ + public function then(callable $onFulfilled = null, callable $onRejected = null); +} From 4db772fbd5bef3e034597e70cf29ee44808c7f03 Mon Sep 17 00:00:00 2001 From: Joel Wurtz Date: Mon, 26 Oct 2015 00:05:29 +0100 Subject: [PATCH 2/2] Force callback to be set --- src/Promise.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Promise.php b/src/Promise.php index 5b1ad74..c7e7f7a 100644 --- a/src/Promise.php +++ b/src/Promise.php @@ -18,5 +18,5 @@ interface Promise * * @return Promise Always returns a new promise which is resolved with value of the executed callback (onFulfilled / onRejected) */ - public function then(callable $onFulfilled = null, callable $onRejected = null); + public function then(callable $onFulfilled, callable $onRejected); }