Skip to content

[RFC] Replace sendRequests with an async approach #74

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

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
36 changes: 0 additions & 36 deletions spec/Exception/BatchExceptionSpec.php

This file was deleted.

103 changes: 0 additions & 103 deletions src/BatchResult.php

This file was deleted.

24 changes: 24 additions & 0 deletions src/HttpAsyncClient.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
<?php

namespace Http\Client;

use Psr\Http\Message\RequestInterface;

/**
* Sends a PSR-7 Request in an asynchronous way by returning a Promise.
*
* @author Joel Wurtz <joel.wurtz@gmail.com>
*/
interface HttpAsyncClient
{
/**
* Sends a PSR-7 request in an asynchronous way.
*
* @param RequestInterface $request
*
* @return Promise
*
* @throws Exception
*/
public function sendAsyncRequest(RequestInterface $request);
}
21 changes: 1 addition & 20 deletions src/HttpClient.php
Original file line number Diff line number Diff line change
Expand Up @@ -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 <geloen.eric@gmail.com>
* @author Márk Sági-Kazár <mark.sagikazar@gmail.com>
Expand All @@ -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);
}
22 changes: 22 additions & 0 deletions src/Promise.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
<?php

namespace Http\Client;

/**
* Promise represents a response that may not be available yet, but will be resolved at some point in future.
* It acts like a proxy to the actual response.
*
* @author Joel Wurtz <joel.wurtz@gmail.com>
*/
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
Copy link
Contributor

Choose a reason for hiding this comment

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

should the callables also receive the request object?

*
* @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);
}