diff --git a/composer.json b/composer.json index 38b3567..3e02161 100644 --- a/composer.json +++ b/composer.json @@ -12,8 +12,8 @@ ], "require": { "php": ">=5.4", - "php-http/httplug": "^1.0@dev", - "php-http/utils": "^0.1@dev" + "php-http/httplug": "1.0.0-alpha3", + "php-http/client-tools": "^0.1@dev" }, "require-dev": { "phpspec/phpspec": "^2.4-alpha", diff --git a/spec/AuthenticationPluginSpec.php b/spec/AuthenticationPluginSpec.php index e028cad..ce200d2 100644 --- a/spec/AuthenticationPluginSpec.php +++ b/spec/AuthenticationPluginSpec.php @@ -3,7 +3,7 @@ namespace spec\Http\Client\Plugin; use Http\Authentication\Authentication; -use Http\Client\Promise; +use Http\Promise\Promise; use Psr\Http\Message\RequestInterface; use PhpSpec\ObjectBehavior; use Prophecy\Argument; @@ -29,7 +29,6 @@ function it_sends_an_authenticated_request(Authentication $authentication, Reque { $authentication->authenticate($notAuthedRequest)->willReturn($authedRequest); - $next = function (RequestInterface $request) use($authedRequest, $promise) { if (Argument::is($authedRequest->getWrappedObject())->scoreArgument($request)) { return $promise->getWrappedObject(); diff --git a/spec/CookiePluginSpec.php b/spec/CookiePluginSpec.php index ac2448d..e1faba5 100644 --- a/spec/CookiePluginSpec.php +++ b/spec/CookiePluginSpec.php @@ -2,10 +2,10 @@ namespace spec\Http\Client\Plugin; -use Http\Client\Promise; -use Http\Client\Utils\Promise\FulfilledPromise; +use Http\Client\Tools\Promise\FulfilledPromise; use Http\Cookie\Cookie; use Http\Cookie\CookieJar; +use Http\Promise\Promise; use Psr\Http\Message\RequestInterface; use Psr\Http\Message\ResponseInterface; use Psr\Http\Message\UriInterface; @@ -155,7 +155,7 @@ function it_saves_cookie(CookieJar $cookieJar, RequestInterface $request, Respon $uri->getPath()->willReturn('/'); $promise = $this->handleRequest($request, $next, function () {}); - $promise->shouldHaveType('Http\Client\Promise'); - $promise->getResponse()->shouldReturnAnInstanceOf('Psr\Http\Message\ResponseInterface'); + $promise->shouldHaveType('Http\Promise\Promise'); + $promise->wait()->shouldReturnAnInstanceOf('Psr\Http\Message\ResponseInterface'); } } diff --git a/spec/DecoderPluginSpec.php b/spec/DecoderPluginSpec.php index 43376db..071390c 100644 --- a/spec/DecoderPluginSpec.php +++ b/spec/DecoderPluginSpec.php @@ -2,7 +2,7 @@ namespace spec\Http\Client\Plugin; -use Http\Client\Utils\Promise\FulfilledPromise; +use Http\Client\Tools\Promise\FulfilledPromise; use Psr\Http\Message\RequestInterface; use Psr\Http\Message\ResponseInterface; use Psr\Http\Message\StreamInterface; diff --git a/spec/ErrorPluginSpec.php b/spec/ErrorPluginSpec.php index 2c24655..e4b32a3 100644 --- a/spec/ErrorPluginSpec.php +++ b/spec/ErrorPluginSpec.php @@ -2,7 +2,7 @@ namespace spec\Http\Client\Plugin; -use Http\Client\Utils\Promise\FulfilledPromise; +use Http\Client\Tools\Promise\FulfilledPromise; use Psr\Http\Message\RequestInterface; use Psr\Http\Message\ResponseInterface; use PhpSpec\ObjectBehavior; @@ -30,7 +30,7 @@ function it_throw_request_exception_on_500_error(RequestInterface $request, Resp } }; - $this->handleRequest($request, $next, function () {})->shouldReturnAnInstanceOf('Http\Client\Utils\Promise\RejectedPromise'); + $this->handleRequest($request, $next, function () {})->shouldReturnAnInstanceOf('Http\Client\Tools\Promise\RejectedPromise'); } function it_returns_response(RequestInterface $request, ResponseInterface $response) @@ -43,7 +43,7 @@ function it_returns_response(RequestInterface $request, ResponseInterface $respo } }; - $this->handleRequest($request, $next, function () {})->shouldReturnAnInstanceOf('Http\Client\Utils\Promise\FulfilledPromise'); + $this->handleRequest($request, $next, function () {})->shouldReturnAnInstanceOf('Http\Client\Tools\Promise\FulfilledPromise'); } function it_throws_request_exception_on_custom_regex(RequestInterface $request, ResponseInterface $response) @@ -57,6 +57,6 @@ function it_throws_request_exception_on_custom_regex(RequestInterface $request, } }; - $this->handleRequest($request, $next, function () {})->shouldReturnAnInstanceOf('Http\Client\Utils\Promise\RejectedPromise'); + $this->handleRequest($request, $next, function () {})->shouldReturnAnInstanceOf('Http\Client\Tools\Promise\RejectedPromise'); } } diff --git a/spec/LoggerPluginSpec.php b/spec/LoggerPluginSpec.php index 62ee914..e759f35 100644 --- a/spec/LoggerPluginSpec.php +++ b/spec/LoggerPluginSpec.php @@ -4,9 +4,8 @@ use Http\Client\Exception\HttpException; use Http\Client\Exception\NetworkException; -use Http\Client\Plugin\Normalizer\Normalizer; -use Http\Client\Utils\Promise\FulfilledPromise; -use Http\Client\Utils\Promise\RejectedPromise; +use Http\Client\Tools\Promise\FulfilledPromise; +use Http\Client\Tools\Promise\RejectedPromise; use PhpSpec\ObjectBehavior; use Prophecy\Argument; use Psr\Http\Message\RequestInterface; diff --git a/spec/PluginClientSpec.php b/spec/PluginClientSpec.php index 378f3a3..f02f41c 100644 --- a/spec/PluginClientSpec.php +++ b/spec/PluginClientSpec.php @@ -4,7 +4,7 @@ use Http\Client\HttpAsyncClient; use Http\Client\HttpClient; -use Http\Client\Promise; +use Http\Promise\Promise; use Psr\Http\Message\RequestInterface; use Psr\Http\Message\ResponseInterface; use PhpSpec\ObjectBehavior; diff --git a/spec/RedirectPluginSpec.php b/spec/RedirectPluginSpec.php index af5ce44..43c0f08 100644 --- a/spec/RedirectPluginSpec.php +++ b/spec/RedirectPluginSpec.php @@ -2,9 +2,9 @@ namespace spec\Http\Client\Plugin; -use Http\Client\Utils\Promise\FulfilledPromise; use Http\Client\Plugin\RedirectPlugin; -use Http\Client\Promise; +use Http\Client\Tools\Promise\FulfilledPromise; +use Http\Promise\Promise; use Psr\Http\Message\RequestInterface; use Psr\Http\Message\ResponseInterface; use Psr\Http\Message\UriInterface; @@ -59,13 +59,12 @@ function it_redirects_on_302( } }; - $promise->wait()->shouldBeCalled(); $promise->getState()->willReturn(Promise::FULFILLED); - $promise->getResponse()->willReturn($finalResponse); + $promise->wait()->shouldBeCalled()->willReturn($finalResponse); $finalPromise = $this->handleRequest($request, $next, $first); - $finalPromise->shouldReturnAnInstanceOf('Http\Client\Utils\Promise\FulfilledPromise'); - $finalPromise->getResponse()->shouldReturn($finalResponse); + $finalPromise->shouldReturnAnInstanceOf('Http\Client\Tools\Promise\FulfilledPromise'); + $finalPromise->wait()->shouldReturn($finalResponse); } function it_use_storage_on_301(UriInterface $uriRedirect, RequestInterface $request, RequestInterface $modifiedRequest) @@ -127,9 +126,8 @@ function it_stores_a_301( } }; - $promise->wait()->shouldBeCalled(); $promise->getState()->willReturn(Promise::FULFILLED); - $promise->getResponse()->willReturn($finalResponse); + $promise->wait()->shouldBeCalled()->willReturn($finalResponse); $this->handleRequest($request, $next, $first); $this->hasStorage('/301-url')->shouldReturn(true); @@ -175,9 +173,8 @@ function it_replace_full_url( } }; - $promise->wait()->shouldBeCalled(); $promise->getState()->willReturn(Promise::FULFILLED); - $promise->getResponse()->willReturn($finalResponse); + $promise->wait()->shouldBeCalled()->willReturn($finalResponse); $this->handleRequest($request, $next, $first); } @@ -195,8 +192,8 @@ function it_throws_http_exception_on_no_location(RequestInterface $request, Resp $responseRedirect->hasHeader('Location')->willReturn(false); $promise = $this->handleRequest($request, $next, function () {}); - $promise->shouldReturnAnInstanceOf('Http\Client\Utils\Promise\RejectedPromise'); - $promise->getException()->shouldReturnAnInstanceOf('Http\Client\Exception\HttpException'); + $promise->shouldReturnAnInstanceOf('Http\Client\Tools\Promise\RejectedPromise'); + $promise->shouldThrow('Http\Client\Exception\HttpException')->duringWait(); } function it_throws_http_exception_on_invalid_location(RequestInterface $request, ResponseInterface $responseRedirect) @@ -214,8 +211,8 @@ function it_throws_http_exception_on_invalid_location(RequestInterface $request, $responseRedirect->hasHeader('Location')->willReturn(true); $promise = $this->handleRequest($request, $next, function () {}); - $promise->shouldReturnAnInstanceOf('Http\Client\Utils\Promise\RejectedPromise'); - $promise->getException()->shouldReturnAnInstanceOf('Http\Client\Exception\HttpException'); + $promise->shouldReturnAnInstanceOf('Http\Client\Tools\Promise\RejectedPromise'); + $promise->shouldThrow('Http\Client\Exception\HttpException')->duringWait(); } function it_throw_multi_redirect_exception_on_300(RequestInterface $request, ResponseInterface $responseRedirect) @@ -230,8 +227,8 @@ function it_throw_multi_redirect_exception_on_300(RequestInterface $request, Res $responseRedirect->getStatusCode()->willReturn('300'); $promise = $this->handleRequest($request, $next, function () {}); - $promise->shouldReturnAnInstanceOf('Http\Client\Utils\Promise\RejectedPromise'); - $promise->getException()->shouldReturnAnInstanceOf('Http\Client\Plugin\Exception\MultipleRedirectionException'); + $promise->shouldReturnAnInstanceOf('Http\Client\Tools\Promise\RejectedPromise'); + $promise->shouldThrow('Http\Client\Plugin\Exception\MultipleRedirectionException')->duringWait(); } function it_throw_multi_redirect_exception_on_300_if_no_location(RequestInterface $request, ResponseInterface $responseRedirect) @@ -246,8 +243,8 @@ function it_throw_multi_redirect_exception_on_300_if_no_location(RequestInterfac $responseRedirect->hasHeader('Location')->willReturn(false); $promise = $this->handleRequest($request, $next, function () {}); - $promise->shouldReturnAnInstanceOf('Http\Client\Utils\Promise\RejectedPromise'); - $promise->getException()->shouldReturnAnInstanceOf('Http\Client\Plugin\Exception\MultipleRedirectionException'); + $promise->shouldReturnAnInstanceOf('Http\Client\Tools\Promise\RejectedPromise'); + $promise->shouldThrow('Http\Client\Plugin\Exception\MultipleRedirectionException')->duringWait(); } function it_switch_method_for_302( @@ -289,9 +286,8 @@ function it_switch_method_for_302( } }; - $promise->wait()->shouldBeCalled(); $promise->getState()->willReturn(Promise::FULFILLED); - $promise->getResponse()->willReturn($finalResponse); + $promise->wait()->shouldBeCalled()->willReturn($finalResponse); $this->handleRequest($request, $next, $first); } @@ -338,9 +334,8 @@ function it_clears_headers( } }; - $promise->wait()->shouldBeCalled(); $promise->getState()->willReturn(Promise::FULFILLED); - $promise->getResponse()->willReturn($finalResponse); + $promise->wait()->shouldBeCalled()->willReturn($finalResponse); $this->handleRequest($request, $next, $first); } @@ -375,8 +370,8 @@ function it_throws_circular_redirection_exception(UriInterface $uri, UriInterfac }; $promise = $this->handleRequest($request, $next, $first); - $promise->shouldReturnAnInstanceOf('Http\Client\Utils\Promise\RejectedPromise'); - $promise->getException()->shouldReturnAnInstanceOf('Http\Client\Plugin\Exception\CircularRedirectionException'); + $promise->shouldReturnAnInstanceOf('Http\Client\Tools\Promise\RejectedPromise'); + $promise->shouldThrow('Http\Client\Plugin\Exception\CircularRedirectionException')->duringWait(); } } diff --git a/spec/RetryPluginSpec.php b/spec/RetryPluginSpec.php index 3a25329..c00086f 100644 --- a/spec/RetryPluginSpec.php +++ b/spec/RetryPluginSpec.php @@ -3,8 +3,8 @@ namespace spec\Http\Client\Plugin; use Http\Client\Exception; -use Http\Client\Utils\Promise\FulfilledPromise; -use Http\Client\Utils\Promise\RejectedPromise; +use Http\Client\Tools\Promise\FulfilledPromise; +use Http\Client\Tools\Promise\RejectedPromise; use Psr\Http\Message\RequestInterface; use Psr\Http\Message\ResponseInterface; use PhpSpec\ObjectBehavior; @@ -30,7 +30,7 @@ function it_returns_response(RequestInterface $request, ResponseInterface $respo } }; - $this->handleRequest($request, $next, function () {})->shouldReturnAnInstanceOf('Http\Client\Utils\Promise\FulfilledPromise'); + $this->handleRequest($request, $next, function () {})->shouldReturnAnInstanceOf('Http\Client\Tools\Promise\FulfilledPromise'); } function it_throws_exception_on_multiple_exceptions(RequestInterface $request) @@ -53,8 +53,8 @@ function it_throws_exception_on_multiple_exceptions(RequestInterface $request) }; $promise = $this->handleRequest($request, $next, function () {}); - $promise->shouldReturnAnInstanceOf('Http\Client\Utils\Promise\RejectedPromise'); - $promise->getException()->shouldReturn($exception2); + $promise->shouldReturnAnInstanceOf('Http\Client\Tools\Promise\RejectedPromise'); + $promise->shouldThrow($exception2)->duringWait(); } function it_returns_response_on_second_try(RequestInterface $request, ResponseInterface $response) @@ -76,8 +76,8 @@ function it_returns_response_on_second_try(RequestInterface $request, ResponseIn }; $promise = $this->handleRequest($request, $next, function () {}); - $promise->shouldReturnAnInstanceOf('Http\Client\Utils\Promise\FulfilledPromise'); - $promise->getResponse()->shouldReturn($response); + $promise->shouldReturnAnInstanceOf('Http\Client\Tools\Promise\FulfilledPromise'); + $promise->wait()->shouldReturn($response); } function it_does_not_keep_history_of_old_failure(RequestInterface $request, ResponseInterface $response) @@ -98,7 +98,7 @@ function it_does_not_keep_history_of_old_failure(RequestInterface $request, Resp } }; - $this->handleRequest($request, $next, function () {})->shouldReturnAnInstanceOf('Http\Client\Utils\Promise\FulfilledPromise'); - $this->handleRequest($request, $next, function () {})->shouldReturnAnInstanceOf('Http\Client\Utils\Promise\FulfilledPromise'); + $this->handleRequest($request, $next, function () {})->shouldReturnAnInstanceOf('Http\Client\Tools\Promise\FulfilledPromise'); + $this->handleRequest($request, $next, function () {})->shouldReturnAnInstanceOf('Http\Client\Tools\Promise\FulfilledPromise'); } } diff --git a/spec/StopwatchPluginSpec.php b/spec/StopwatchPluginSpec.php index fa46132..2c68503 100644 --- a/spec/StopwatchPluginSpec.php +++ b/spec/StopwatchPluginSpec.php @@ -3,8 +3,8 @@ namespace spec\Http\Client\Plugin; use Http\Client\Exception\NetworkException; -use Http\Client\Utils\Promise\FulfilledPromise; -use Http\Client\Utils\Promise\RejectedPromise; +use Http\Client\Tools\Promise\FulfilledPromise; +use Http\Client\Tools\Promise\RejectedPromise; use Psr\Http\Message\RequestInterface; use Psr\Http\Message\ResponseInterface; use Symfony\Component\Stopwatch\Stopwatch; diff --git a/src/EmulateAsyncClient.php b/src/EmulateAsyncClient.php new file mode 100644 index 0000000..21fd368 --- /dev/null +++ b/src/EmulateAsyncClient.php @@ -0,0 +1,22 @@ +httpClient = $httpClient; + } +} diff --git a/src/PluginClient.php b/src/PluginClient.php index 366b9d0..f68d5d0 100644 --- a/src/PluginClient.php +++ b/src/PluginClient.php @@ -4,9 +4,7 @@ use Http\Client\HttpAsyncClient; use Http\Client\HttpClient; -use Http\Client\Plugin\Exception\RebootChainException; -use Http\Client\Promise; -use Http\Client\Utils\EmulateAsyncClient; +use Http\Promise\Promise; use Psr\Http\Message\RequestInterface; /** @@ -55,13 +53,8 @@ public function __construct($client, array $plugins = []) public function sendRequest(RequestInterface $request) { $promise = $this->sendAsyncRequest($request); - $promise->wait(); - if ($promise->getState() == Promise::REJECTED) { - throw $promise->getException(); - } - - return $promise->getResponse(); + return $promise->wait(); } /** diff --git a/src/RedirectPlugin.php b/src/RedirectPlugin.php index d201f9e..ce10088 100644 --- a/src/RedirectPlugin.php +++ b/src/RedirectPlugin.php @@ -5,7 +5,7 @@ use Http\Client\Exception\HttpException; use Http\Client\Plugin\Exception\CircularRedirectionException; use Http\Client\Plugin\Exception\MultipleRedirectionException; -use Http\Client\Promise; +use Http\Promise\Promise; use Psr\Http\Message\MessageInterface; use Psr\Http\Message\RequestInterface; use Psr\Http\Message\ResponseInterface; @@ -152,13 +152,8 @@ public function handleRequest(RequestInterface $request, callable $next, callabl // Call redirect request in synchrone $redirectPromise = $first($redirectRequest); - $redirectPromise->wait(); - if ($redirectPromise->getState() == Promise::REJECTED) { - throw $redirectPromise->getException(); - } - - return $redirectPromise->getResponse(); + return $redirectPromise->wait(); }); } diff --git a/src/RetryPlugin.php b/src/RetryPlugin.php index 67eaeb5..2dd8c65 100644 --- a/src/RetryPlugin.php +++ b/src/RetryPlugin.php @@ -4,7 +4,7 @@ use Http\Client\Exception; use Http\Client\Plugin\Exception\RetryException; -use Http\Client\Promise; +use Http\Promise\Promise; use Psr\Http\Message\RequestInterface; use Psr\Http\Message\ResponseInterface; @@ -67,13 +67,8 @@ public function handleRequest(RequestInterface $request, callable $next, callabl // Retry in synchrone $promise = $this->handleRequest($request, $next, $first); - $promise->wait(); - if ($promise->getState() == Promise::REJECTED) { - throw $promise->getException(); - } - - return $promise->getResponse(); + return $promise->wait(); }); } }