-
Notifications
You must be signed in to change notification settings - Fork 53
Add http client router feature #13
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
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,63 @@ | ||
<?php | ||
|
||
namespace spec\Http\Client\Common; | ||
|
||
use Http\Message\RequestMatcher; | ||
use Http\Client\HttpAsyncClient; | ||
use Http\Client\HttpClient; | ||
use Http\Promise\Promise; | ||
use Psr\Http\Message\RequestInterface; | ||
use Psr\Http\Message\ResponseInterface; | ||
use PhpSpec\ObjectBehavior; | ||
|
||
class HttpClientRouterSpec extends ObjectBehavior | ||
{ | ||
function it_is_initializable() | ||
{ | ||
$this->shouldHaveType('Http\Client\Common\HttpClientRouter'); | ||
} | ||
|
||
function it_is_an_http_client() | ||
{ | ||
$this->shouldImplement('Http\Client\HttpClient'); | ||
} | ||
|
||
function it_is_an_async_http_client() | ||
{ | ||
$this->shouldImplement('Http\Client\HttpAsyncClient'); | ||
} | ||
|
||
function it_send_request(RequestMatcher $matcher, HttpClient $client, RequestInterface $request, ResponseInterface $response) | ||
{ | ||
$this->addClient($client, $matcher); | ||
$matcher->matches($request)->willReturn(true); | ||
$client->sendRequest($request)->willReturn($response); | ||
|
||
$this->sendRequest($request)->shouldReturn($response); | ||
} | ||
|
||
function it_send_async_request(RequestMatcher $matcher, HttpAsyncClient $client, RequestInterface $request, Promise $promise) | ||
{ | ||
$this->addClient($client, $matcher); | ||
$matcher->matches($request)->willReturn(true); | ||
$client->sendAsyncRequest($request)->willReturn($promise); | ||
|
||
$this->sendAsyncRequest($request)->shouldReturn($promise); | ||
} | ||
|
||
function it_throw_exception_on_send_request(RequestMatcher $matcher, HttpClient $client, RequestInterface $request) | ||
{ | ||
$this->addClient($client, $matcher); | ||
$matcher->matches($request)->willReturn(false); | ||
|
||
$this->shouldThrow('Http\Client\Exception\RequestException')->duringSendRequest($request); | ||
} | ||
|
||
function it_throw_exception_on_send_async_request(RequestMatcher $matcher, HttpAsyncClient $client, RequestInterface $request) | ||
{ | ||
$this->addClient($client, $matcher); | ||
$matcher->matches($request)->willReturn(false); | ||
|
||
$this->shouldThrow('Http\Client\Exception\RequestException')->duringSendAsyncRequest($request); | ||
} | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,74 @@ | ||
<?php | ||
|
||
namespace Http\Client\Common; | ||
|
||
use Http\Client\Exception\RequestException; | ||
use Http\Client\HttpAsyncClient; | ||
use Http\Client\HttpClient; | ||
use Http\Message\RequestMatcher; | ||
use Psr\Http\Message\RequestInterface; | ||
|
||
/** | ||
* Route a request to a specific client in the stack based using a RequestMatcher. | ||
* | ||
* @author Joel Wurtz <[email protected]> | ||
*/ | ||
final class HttpClientRouter implements HttpClient, HttpAsyncClient | ||
{ | ||
/** | ||
* @var array | ||
*/ | ||
private $clients = []; | ||
|
||
/** | ||
* {@inheritdoc} | ||
*/ | ||
public function sendRequest(RequestInterface $request) | ||
{ | ||
$client = $this->chooseHttpClient($request); | ||
|
||
return $client->sendRequest($request); | ||
} | ||
|
||
/** | ||
* {@inheritdoc} | ||
*/ | ||
public function sendAsyncRequest(RequestInterface $request) | ||
{ | ||
$client = $this->chooseHttpClient($request); | ||
|
||
return $client->sendAsyncRequest($request); | ||
} | ||
|
||
/** | ||
* Add a client to the router. | ||
* | ||
* @param HttpClient|HttpAsyncClient $client | ||
* @param RequestMatcher $requestMatcher | ||
*/ | ||
public function addClient($client, RequestMatcher $requestMatcher) | ||
{ | ||
$this->clients[] = [ | ||
'matcher' => $requestMatcher, | ||
'client' => new FlexibleHttpClient($client), | ||
]; | ||
} | ||
|
||
/** | ||
* Choose an HTTP client given a specific request. | ||
* | ||
* @param RequestInterface $request | ||
* | ||
* @return HttpClient|HttpAsyncClient | ||
*/ | ||
protected function chooseHttpClient(RequestInterface $request) | ||
{ | ||
foreach ($this->clients as $client) { | ||
if ($client['matcher']->matches($request)) { | ||
return $client['client']; | ||
} | ||
} | ||
|
||
throw new RequestException('No client found for the specified request', $request); | ||
} | ||
} |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
Do you think we could find a better way to store these. Although it's an internal detail, it feels a little but weird.
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.
having a member client and a member matcher would make more sense.
re-reading this i am confused about this code. we have a bunch of matcher, but only one single client? if none of the matchers match, we throw an exception, if any matches we always use the same client. that is not what i would expect from a router. i would have expected this to be an array of matcher, client and if a matcher matches, that client is used...
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.
the code also indicates that this was originally the idea.
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.
oh sorry. i misread the code. it does what i would expect. i don't see how we could store these differently. its a list of pairs [matcher, client] - i would not know how to store that differently. you could try some spl hashmap that can use objects as key if that exists, but i think its quite straightforward.
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 think this is what's happening right now. Isn't it?
That one client is only used as a decorator in order to correctly emulate a client if necessary.