File tree Expand file tree Collapse file tree 5 files changed +146
-1
lines changed Expand file tree Collapse file tree 5 files changed +146
-1
lines changed Original file line number Diff line number Diff line change 11# Change Log
22
3+ ## 2.0.0 - Unreleased
4+
5+ - The HttpAsyncClient now returns a HttpPromise (instead of a Promise)
6+
7+ ## 1.1.0 - Unreleased
8+
9+ - Added HttpPromise interface to enforce value of rejected and resolved promise
10+ - Added HttpFulfilledPromise and HttpRejectedPromise which respect the HttpAsyncClient interface
311
412## 1.0.0 - 2016-01-26
513
Original file line number Diff line number Diff line change @@ -19,7 +19,7 @@ interface HttpAsyncClient
1919 *
2020 * @param RequestInterface $request
2121 *
22- * @return Promise Resolves a PSR-7 Response or fails with an Http\Client\Exception.
22+ * @return Promise Promise Resolves a PSR-7 Response or fails with an Http\Client\Exception.
2323 *
2424 * @throws \Exception If processing the request is impossible (eg. bad configuration).
2525 */
Original file line number Diff line number Diff line change 1+ <?php
2+
3+ namespace Http \Client \Promise ;
4+
5+ use Http \Client \Exception ;
6+ use Psr \Http \Message \ResponseInterface ;
7+
8+ final class HttpFulfilledPromise implements HttpPromise
9+ {
10+ /**
11+ * @var ResponseInterface
12+ */
13+ private $ response ;
14+
15+ /**
16+ * @param ResponseInterface $response
17+ */
18+ public function __construct (ResponseInterface $ response )
19+ {
20+ $ this ->response = $ response ;
21+ }
22+
23+ /**
24+ * {@inheritdoc}
25+ */
26+ public function then (callable $ onFulfilled = null , callable $ onRejected = null )
27+ {
28+ if (null === $ onFulfilled ) {
29+ return $ this ;
30+ }
31+
32+ try {
33+ return new self ($ onFulfilled ($ this ->response ));
34+ } catch (Exception $ e ) {
35+ return new HttpRejectedPromise ($ e );
36+ }
37+ }
38+
39+ /**
40+ * {@inheritdoc}
41+ */
42+ public function getState ()
43+ {
44+ return HttpPromise::FULFILLED ;
45+ }
46+
47+ /**
48+ * {@inheritdoc}
49+ */
50+ public function wait ($ unwrap = true )
51+ {
52+ if ($ unwrap ) {
53+ return $ this ->response ;
54+ }
55+ }
56+ }
Original file line number Diff line number Diff line change 1+ <?php
2+
3+ namespace Http \Client \Promise ;
4+
5+ use Http \Client \Exception ;
6+ use Http \Promise \Promise ;
7+ use Psr \Http \Message \ResponseInterface ;
8+
9+ interface HttpPromise extends Promise
10+ {
11+ /**
12+ * {@inheritdoc}
13+ *
14+ * @return HttpPromise A new resolved http promise with value of the executed callback (onFulfilled / onRejected).
15+ */
16+ public function then (callable $ onFulfilled = null , callable $ onRejected = null );
17+
18+ /**
19+ * {@inheritdoc}
20+ *
21+ * @return ResponseInterface Resolved response, null if $unwrap is set to false
22+ *
23+ * @throws Exception The http exception if $unwrap is set to true and the request failed.
24+ */
25+ public function wait ($ unwrap = true );
26+ }
Original file line number Diff line number Diff line change 1+ <?php
2+
3+ namespace Http \Client \Promise ;
4+
5+ use Http \Client \Exception ;
6+
7+ final class HttpRejectedPromise implements HttpPromise
8+ {
9+ /**
10+ * @var Exception
11+ */
12+ private $ exception ;
13+
14+ /**
15+ * @param Exception $exception
16+ */
17+ public function __construct (Exception $ exception )
18+ {
19+ $ this ->exception = $ exception ;
20+ }
21+
22+ /**
23+ * {@inheritdoc}
24+ */
25+ public function then (callable $ onFulfilled = null , callable $ onRejected = null )
26+ {
27+ if (null === $ onRejected ) {
28+ return $ this ;
29+ }
30+
31+ try {
32+ return new HttpFulfilledPromise ($ onRejected ($ this ->exception ));
33+ } catch (Exception $ e ) {
34+ return new self ($ e );
35+ }
36+ }
37+
38+ /**
39+ * {@inheritdoc}
40+ */
41+ public function getState ()
42+ {
43+ return HttpPromise::REJECTED ;
44+ }
45+
46+ /**
47+ * {@inheritdoc}
48+ */
49+ public function wait ($ unwrap = true )
50+ {
51+ if ($ unwrap ) {
52+ throw $ this ->exception ;
53+ }
54+ }
55+ }
You can’t perform that action at this time.
0 commit comments