Skip to content

Commit 35d54ef

Browse files
committed
Merge pull request #83 from php-http/async
Merge Async client and Promise in Httplug
2 parents d20623c + efbeb3b commit 35d54ef

File tree

3 files changed

+115
-1
lines changed

3 files changed

+115
-1
lines changed

CHANGELOG.md

+8-1
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,14 @@
11
# Change Log
22

33

4-
## 1.0.0 - Unreleased
4+
## 1.0.0-alpha2 - UNRELEASED
5+
6+
### Added
7+
8+
- Async client and Promise interface
9+
10+
11+
## 1.0.0-alpha - 2015-10-26
512

613
### Added
714

src/HttpAsyncClient.php

+24
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
<?php
2+
3+
namespace Http\Client;
4+
5+
use Psr\Http\Message\RequestInterface;
6+
7+
/**
8+
* Sends a PSR-7 Request in an asynchronous way by returning a Promise.
9+
*
10+
* @author Joel Wurtz <[email protected]>
11+
*/
12+
interface HttpAsyncClient
13+
{
14+
/**
15+
* Sends a PSR-7 request in an asynchronous way.
16+
*
17+
* @param RequestInterface $request
18+
*
19+
* @return Promise
20+
*
21+
* @throws Exception
22+
*/
23+
public function sendAsyncRequest(RequestInterface $request);
24+
}

src/Promise.php

+83
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,83 @@
1+
<?php
2+
3+
namespace Http\Client;
4+
5+
use Psr\Http\Message\ResponseInterface;
6+
7+
/**
8+
* Promise represents a response that may not be available yet, but will be resolved at some point in future.
9+
* It acts like a proxy to the actual response.
10+
*
11+
* This interface is an extension of the promises/a+ specification https://promisesaplus.com/
12+
* Value is replaced by an object where its class implement a Psr\Http\Message\RequestInterface.
13+
* Reason is replaced by an object where its class implement a Http\Client\Exception.
14+
*
15+
* @author Joel Wurtz <[email protected]>
16+
*/
17+
interface Promise
18+
{
19+
/**
20+
* Pending state, promise has not been fulfilled or rejected.
21+
*/
22+
const PENDING = "pending";
23+
24+
/**
25+
* Fulfilled state, promise has been fulfilled with a ResponseInterface object.
26+
*/
27+
const FULFILLED = "fulfilled";
28+
29+
/**
30+
* Rejected state, promise has been rejected with an Exception object.
31+
*/
32+
const REJECTED = "rejected";
33+
34+
/**
35+
* Add behavior for when the promise is resolved or rejected (response will be available, or error happens).
36+
*
37+
* If you do not care about one of the cases, you can set the corresponding callable to null
38+
* The callback will be called when the response or exception arrived and never more than once.
39+
*
40+
* @param callable $onFulfilled Called when a response will be available.
41+
* @param callable $onRejected Called when an error happens.
42+
*
43+
* You must always return the Response in the interface or throw an Exception.
44+
*
45+
* @return Promise Always returns a new promise which is resolved with value of the executed callback (onFulfilled / onRejected).
46+
*/
47+
public function then(callable $onFulfilled = null, callable $onRejected = null);
48+
49+
/**
50+
* Get the state of the promise, one of PENDING, FULFILLED or REJECTED
51+
*
52+
* @return int
53+
*/
54+
public function getState();
55+
56+
/**
57+
* Return the value of the promise (fulfilled).
58+
*
59+
* @return ResponseInterface Response Object only when the Promise is fulfilled.
60+
*
61+
* @throws \LogicException When the promise is not fulfilled.
62+
*/
63+
public function getResponse();
64+
65+
/**
66+
* Get the reason why the promise was rejected..
67+
*
68+
* If the exception is an instance of Http\Client\Exception\HttpException it will contain
69+
* the response object with the status code and the http reason.
70+
*
71+
* @return Exception Exception Object only when the Promise is rejected.
72+
*
73+
* @throws \LogicException When the promise is not rejected.
74+
*/
75+
public function getException();
76+
77+
/**
78+
* Wait for the promise to be fulfilled or rejected.
79+
*
80+
* When this method returns, the request has been resolved and the appropriate callable has terminated.
81+
*/
82+
public function wait();
83+
}

0 commit comments

Comments
 (0)