Skip to content

Commit cc17b17

Browse files
committed
Merge pull request #16 from php-http/feature/promise
Use client tools and new promise package
2 parents 821e8a3 + 7b6d52d commit cc17b17

14 files changed

+73
-75
lines changed

composer.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,8 @@
1212
],
1313
"require": {
1414
"php": ">=5.4",
15-
"php-http/httplug": "^1.0@dev",
16-
"php-http/utils": "^0.1@dev"
15+
"php-http/httplug": "1.0.0-alpha3",
16+
"php-http/client-tools": "^0.1@dev"
1717
},
1818
"require-dev": {
1919
"phpspec/phpspec": "^2.4-alpha",

spec/AuthenticationPluginSpec.php

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
namespace spec\Http\Client\Plugin;
44

55
use Http\Authentication\Authentication;
6-
use Http\Client\Promise;
6+
use Http\Promise\Promise;
77
use Psr\Http\Message\RequestInterface;
88
use PhpSpec\ObjectBehavior;
99
use Prophecy\Argument;
@@ -29,7 +29,6 @@ function it_sends_an_authenticated_request(Authentication $authentication, Reque
2929
{
3030
$authentication->authenticate($notAuthedRequest)->willReturn($authedRequest);
3131

32-
3332
$next = function (RequestInterface $request) use($authedRequest, $promise) {
3433
if (Argument::is($authedRequest->getWrappedObject())->scoreArgument($request)) {
3534
return $promise->getWrappedObject();

spec/CookiePluginSpec.php

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,10 @@
22

33
namespace spec\Http\Client\Plugin;
44

5-
use Http\Client\Promise;
6-
use Http\Client\Utils\Promise\FulfilledPromise;
5+
use Http\Client\Tools\Promise\FulfilledPromise;
76
use Http\Cookie\Cookie;
87
use Http\Cookie\CookieJar;
8+
use Http\Promise\Promise;
99
use Psr\Http\Message\RequestInterface;
1010
use Psr\Http\Message\ResponseInterface;
1111
use Psr\Http\Message\UriInterface;
@@ -155,7 +155,7 @@ function it_saves_cookie(CookieJar $cookieJar, RequestInterface $request, Respon
155155
$uri->getPath()->willReturn('/');
156156

157157
$promise = $this->handleRequest($request, $next, function () {});
158-
$promise->shouldHaveType('Http\Client\Promise');
159-
$promise->getResponse()->shouldReturnAnInstanceOf('Psr\Http\Message\ResponseInterface');
158+
$promise->shouldHaveType('Http\Promise\Promise');
159+
$promise->wait()->shouldReturnAnInstanceOf('Psr\Http\Message\ResponseInterface');
160160
}
161161
}

spec/DecoderPluginSpec.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
namespace spec\Http\Client\Plugin;
44

5-
use Http\Client\Utils\Promise\FulfilledPromise;
5+
use Http\Client\Tools\Promise\FulfilledPromise;
66
use Psr\Http\Message\RequestInterface;
77
use Psr\Http\Message\ResponseInterface;
88
use Psr\Http\Message\StreamInterface;

spec/ErrorPluginSpec.php

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
namespace spec\Http\Client\Plugin;
44

5-
use Http\Client\Utils\Promise\FulfilledPromise;
5+
use Http\Client\Tools\Promise\FulfilledPromise;
66
use Psr\Http\Message\RequestInterface;
77
use Psr\Http\Message\ResponseInterface;
88
use PhpSpec\ObjectBehavior;
@@ -30,7 +30,7 @@ function it_throw_request_exception_on_500_error(RequestInterface $request, Resp
3030
}
3131
};
3232

33-
$this->handleRequest($request, $next, function () {})->shouldReturnAnInstanceOf('Http\Client\Utils\Promise\RejectedPromise');
33+
$this->handleRequest($request, $next, function () {})->shouldReturnAnInstanceOf('Http\Client\Tools\Promise\RejectedPromise');
3434
}
3535

3636
function it_returns_response(RequestInterface $request, ResponseInterface $response)
@@ -43,7 +43,7 @@ function it_returns_response(RequestInterface $request, ResponseInterface $respo
4343
}
4444
};
4545

46-
$this->handleRequest($request, $next, function () {})->shouldReturnAnInstanceOf('Http\Client\Utils\Promise\FulfilledPromise');
46+
$this->handleRequest($request, $next, function () {})->shouldReturnAnInstanceOf('Http\Client\Tools\Promise\FulfilledPromise');
4747
}
4848

4949
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,
5757
}
5858
};
5959

60-
$this->handleRequest($request, $next, function () {})->shouldReturnAnInstanceOf('Http\Client\Utils\Promise\RejectedPromise');
60+
$this->handleRequest($request, $next, function () {})->shouldReturnAnInstanceOf('Http\Client\Tools\Promise\RejectedPromise');
6161
}
6262
}

spec/LoggerPluginSpec.php

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,8 @@
44

55
use Http\Client\Exception\HttpException;
66
use Http\Client\Exception\NetworkException;
7-
use Http\Client\Plugin\Normalizer\Normalizer;
8-
use Http\Client\Utils\Promise\FulfilledPromise;
9-
use Http\Client\Utils\Promise\RejectedPromise;
7+
use Http\Client\Tools\Promise\FulfilledPromise;
8+
use Http\Client\Tools\Promise\RejectedPromise;
109
use PhpSpec\ObjectBehavior;
1110
use Prophecy\Argument;
1211
use Psr\Http\Message\RequestInterface;

spec/PluginClientSpec.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44

55
use Http\Client\HttpAsyncClient;
66
use Http\Client\HttpClient;
7-
use Http\Client\Promise;
7+
use Http\Promise\Promise;
88
use Psr\Http\Message\RequestInterface;
99
use Psr\Http\Message\ResponseInterface;
1010
use PhpSpec\ObjectBehavior;

spec/RedirectPluginSpec.php

Lines changed: 19 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,9 @@
22

33
namespace spec\Http\Client\Plugin;
44

5-
use Http\Client\Utils\Promise\FulfilledPromise;
65
use Http\Client\Plugin\RedirectPlugin;
7-
use Http\Client\Promise;
6+
use Http\Client\Tools\Promise\FulfilledPromise;
7+
use Http\Promise\Promise;
88
use Psr\Http\Message\RequestInterface;
99
use Psr\Http\Message\ResponseInterface;
1010
use Psr\Http\Message\UriInterface;
@@ -59,13 +59,12 @@ function it_redirects_on_302(
5959
}
6060
};
6161

62-
$promise->wait()->shouldBeCalled();
6362
$promise->getState()->willReturn(Promise::FULFILLED);
64-
$promise->getResponse()->willReturn($finalResponse);
63+
$promise->wait()->shouldBeCalled()->willReturn($finalResponse);
6564

6665
$finalPromise = $this->handleRequest($request, $next, $first);
67-
$finalPromise->shouldReturnAnInstanceOf('Http\Client\Utils\Promise\FulfilledPromise');
68-
$finalPromise->getResponse()->shouldReturn($finalResponse);
66+
$finalPromise->shouldReturnAnInstanceOf('Http\Client\Tools\Promise\FulfilledPromise');
67+
$finalPromise->wait()->shouldReturn($finalResponse);
6968
}
7069

7170
function it_use_storage_on_301(UriInterface $uriRedirect, RequestInterface $request, RequestInterface $modifiedRequest)
@@ -127,9 +126,8 @@ function it_stores_a_301(
127126
}
128127
};
129128

130-
$promise->wait()->shouldBeCalled();
131129
$promise->getState()->willReturn(Promise::FULFILLED);
132-
$promise->getResponse()->willReturn($finalResponse);
130+
$promise->wait()->shouldBeCalled()->willReturn($finalResponse);
133131

134132
$this->handleRequest($request, $next, $first);
135133
$this->hasStorage('/301-url')->shouldReturn(true);
@@ -175,9 +173,8 @@ function it_replace_full_url(
175173
}
176174
};
177175

178-
$promise->wait()->shouldBeCalled();
179176
$promise->getState()->willReturn(Promise::FULFILLED);
180-
$promise->getResponse()->willReturn($finalResponse);
177+
$promise->wait()->shouldBeCalled()->willReturn($finalResponse);
181178

182179
$this->handleRequest($request, $next, $first);
183180
}
@@ -195,8 +192,8 @@ function it_throws_http_exception_on_no_location(RequestInterface $request, Resp
195192
$responseRedirect->hasHeader('Location')->willReturn(false);
196193

197194
$promise = $this->handleRequest($request, $next, function () {});
198-
$promise->shouldReturnAnInstanceOf('Http\Client\Utils\Promise\RejectedPromise');
199-
$promise->getException()->shouldReturnAnInstanceOf('Http\Client\Exception\HttpException');
195+
$promise->shouldReturnAnInstanceOf('Http\Client\Tools\Promise\RejectedPromise');
196+
$promise->shouldThrow('Http\Client\Exception\HttpException')->duringWait();
200197
}
201198

202199
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,
214211
$responseRedirect->hasHeader('Location')->willReturn(true);
215212

216213
$promise = $this->handleRequest($request, $next, function () {});
217-
$promise->shouldReturnAnInstanceOf('Http\Client\Utils\Promise\RejectedPromise');
218-
$promise->getException()->shouldReturnAnInstanceOf('Http\Client\Exception\HttpException');
214+
$promise->shouldReturnAnInstanceOf('Http\Client\Tools\Promise\RejectedPromise');
215+
$promise->shouldThrow('Http\Client\Exception\HttpException')->duringWait();
219216
}
220217

221218
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
230227
$responseRedirect->getStatusCode()->willReturn('300');
231228

232229
$promise = $this->handleRequest($request, $next, function () {});
233-
$promise->shouldReturnAnInstanceOf('Http\Client\Utils\Promise\RejectedPromise');
234-
$promise->getException()->shouldReturnAnInstanceOf('Http\Client\Plugin\Exception\MultipleRedirectionException');
230+
$promise->shouldReturnAnInstanceOf('Http\Client\Tools\Promise\RejectedPromise');
231+
$promise->shouldThrow('Http\Client\Plugin\Exception\MultipleRedirectionException')->duringWait();
235232
}
236233

237234
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
246243
$responseRedirect->hasHeader('Location')->willReturn(false);
247244

248245
$promise = $this->handleRequest($request, $next, function () {});
249-
$promise->shouldReturnAnInstanceOf('Http\Client\Utils\Promise\RejectedPromise');
250-
$promise->getException()->shouldReturnAnInstanceOf('Http\Client\Plugin\Exception\MultipleRedirectionException');
246+
$promise->shouldReturnAnInstanceOf('Http\Client\Tools\Promise\RejectedPromise');
247+
$promise->shouldThrow('Http\Client\Plugin\Exception\MultipleRedirectionException')->duringWait();
251248
}
252249

253250
function it_switch_method_for_302(
@@ -289,9 +286,8 @@ function it_switch_method_for_302(
289286
}
290287
};
291288

292-
$promise->wait()->shouldBeCalled();
293289
$promise->getState()->willReturn(Promise::FULFILLED);
294-
$promise->getResponse()->willReturn($finalResponse);
290+
$promise->wait()->shouldBeCalled()->willReturn($finalResponse);
295291

296292
$this->handleRequest($request, $next, $first);
297293
}
@@ -338,9 +334,8 @@ function it_clears_headers(
338334
}
339335
};
340336

341-
$promise->wait()->shouldBeCalled();
342337
$promise->getState()->willReturn(Promise::FULFILLED);
343-
$promise->getResponse()->willReturn($finalResponse);
338+
$promise->wait()->shouldBeCalled()->willReturn($finalResponse);
344339

345340
$this->handleRequest($request, $next, $first);
346341
}
@@ -375,8 +370,8 @@ function it_throws_circular_redirection_exception(UriInterface $uri, UriInterfac
375370
};
376371

377372
$promise = $this->handleRequest($request, $next, $first);
378-
$promise->shouldReturnAnInstanceOf('Http\Client\Utils\Promise\RejectedPromise');
379-
$promise->getException()->shouldReturnAnInstanceOf('Http\Client\Plugin\Exception\CircularRedirectionException');
373+
$promise->shouldReturnAnInstanceOf('Http\Client\Tools\Promise\RejectedPromise');
374+
$promise->shouldThrow('Http\Client\Plugin\Exception\CircularRedirectionException')->duringWait();
380375
}
381376
}
382377

spec/RetryPluginSpec.php

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,8 @@
33
namespace spec\Http\Client\Plugin;
44

55
use Http\Client\Exception;
6-
use Http\Client\Utils\Promise\FulfilledPromise;
7-
use Http\Client\Utils\Promise\RejectedPromise;
6+
use Http\Client\Tools\Promise\FulfilledPromise;
7+
use Http\Client\Tools\Promise\RejectedPromise;
88
use Psr\Http\Message\RequestInterface;
99
use Psr\Http\Message\ResponseInterface;
1010
use PhpSpec\ObjectBehavior;
@@ -30,7 +30,7 @@ function it_returns_response(RequestInterface $request, ResponseInterface $respo
3030
}
3131
};
3232

33-
$this->handleRequest($request, $next, function () {})->shouldReturnAnInstanceOf('Http\Client\Utils\Promise\FulfilledPromise');
33+
$this->handleRequest($request, $next, function () {})->shouldReturnAnInstanceOf('Http\Client\Tools\Promise\FulfilledPromise');
3434
}
3535

3636
function it_throws_exception_on_multiple_exceptions(RequestInterface $request)
@@ -53,8 +53,8 @@ function it_throws_exception_on_multiple_exceptions(RequestInterface $request)
5353
};
5454

5555
$promise = $this->handleRequest($request, $next, function () {});
56-
$promise->shouldReturnAnInstanceOf('Http\Client\Utils\Promise\RejectedPromise');
57-
$promise->getException()->shouldReturn($exception2);
56+
$promise->shouldReturnAnInstanceOf('Http\Client\Tools\Promise\RejectedPromise');
57+
$promise->shouldThrow($exception2)->duringWait();
5858
}
5959

6060
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
7676
};
7777

7878
$promise = $this->handleRequest($request, $next, function () {});
79-
$promise->shouldReturnAnInstanceOf('Http\Client\Utils\Promise\FulfilledPromise');
80-
$promise->getResponse()->shouldReturn($response);
79+
$promise->shouldReturnAnInstanceOf('Http\Client\Tools\Promise\FulfilledPromise');
80+
$promise->wait()->shouldReturn($response);
8181
}
8282

8383
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
9898
}
9999
};
100100

101-
$this->handleRequest($request, $next, function () {})->shouldReturnAnInstanceOf('Http\Client\Utils\Promise\FulfilledPromise');
102-
$this->handleRequest($request, $next, function () {})->shouldReturnAnInstanceOf('Http\Client\Utils\Promise\FulfilledPromise');
101+
$this->handleRequest($request, $next, function () {})->shouldReturnAnInstanceOf('Http\Client\Tools\Promise\FulfilledPromise');
102+
$this->handleRequest($request, $next, function () {})->shouldReturnAnInstanceOf('Http\Client\Tools\Promise\FulfilledPromise');
103103
}
104104
}

spec/StopwatchPluginSpec.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,8 @@
33
namespace spec\Http\Client\Plugin;
44

55
use Http\Client\Exception\NetworkException;
6-
use Http\Client\Utils\Promise\FulfilledPromise;
7-
use Http\Client\Utils\Promise\RejectedPromise;
6+
use Http\Client\Tools\Promise\FulfilledPromise;
7+
use Http\Client\Tools\Promise\RejectedPromise;
88
use Psr\Http\Message\RequestInterface;
99
use Psr\Http\Message\ResponseInterface;
1010
use Symfony\Component\Stopwatch\Stopwatch;

src/EmulateAsyncClient.php

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
<?php
2+
3+
namespace Http\Client\Plugin;
4+
5+
use Http\Client\HttpAsyncClient;
6+
use Http\Client\HttpClient;
7+
use Http\Client\Tools\HttpAsyncClientEmulator;
8+
use Http\Client\Tools\HttpClientDecorator;
9+
10+
/**
11+
* Emulate an async client
12+
*/
13+
class EmulateAsyncClient implements HttpClient, HttpAsyncClient
14+
{
15+
use HttpClientDecorator;
16+
use HttpAsyncClientEmulator;
17+
18+
public function __construct(HttpClient $httpClient)
19+
{
20+
$this->httpClient = $httpClient;
21+
}
22+
}

src/PluginClient.php

Lines changed: 2 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,7 @@
44

55
use Http\Client\HttpAsyncClient;
66
use Http\Client\HttpClient;
7-
use Http\Client\Plugin\Exception\RebootChainException;
8-
use Http\Client\Promise;
9-
use Http\Client\Utils\EmulateAsyncClient;
7+
use Http\Promise\Promise;
108
use Psr\Http\Message\RequestInterface;
119

1210
/**
@@ -55,13 +53,8 @@ public function __construct($client, array $plugins = [])
5553
public function sendRequest(RequestInterface $request)
5654
{
5755
$promise = $this->sendAsyncRequest($request);
58-
$promise->wait();
5956

60-
if ($promise->getState() == Promise::REJECTED) {
61-
throw $promise->getException();
62-
}
63-
64-
return $promise->getResponse();
57+
return $promise->wait();
6558
}
6659

6760
/**

src/RedirectPlugin.php

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
use Http\Client\Exception\HttpException;
66
use Http\Client\Plugin\Exception\CircularRedirectionException;
77
use Http\Client\Plugin\Exception\MultipleRedirectionException;
8-
use Http\Client\Promise;
8+
use Http\Promise\Promise;
99
use Psr\Http\Message\MessageInterface;
1010
use Psr\Http\Message\RequestInterface;
1111
use Psr\Http\Message\ResponseInterface;
@@ -152,13 +152,8 @@ public function handleRequest(RequestInterface $request, callable $next, callabl
152152

153153
// Call redirect request in synchrone
154154
$redirectPromise = $first($redirectRequest);
155-
$redirectPromise->wait();
156155

157-
if ($redirectPromise->getState() == Promise::REJECTED) {
158-
throw $redirectPromise->getException();
159-
}
160-
161-
return $redirectPromise->getResponse();
156+
return $redirectPromise->wait();
162157
});
163158
}
164159

0 commit comments

Comments
 (0)