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..c7e7f7a --- /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, callable $onRejected); +}