11 use React\Promise\Deferred
;
16 class Call
extends Deferred
implements SplObserver
31 * The executed request
37 * The promised response
43 * Create a deferred promise for the response of $request
45 * @param API $api The endpoint of the request
46 * @param Client $client The HTTP client to send the request
47 * @param Request $request The request to execute
49 function __construct(API
$api, Client
$client, Request
$request) {
51 $this->client
= $client;
52 $this->request
= $request;
54 parent
::__construct(function($resolve, $reject) {
55 return $this->cancel($resolve, $reject);
58 $client->attach($this);
59 $client->enqueue($request, function(Response
$response) {
60 $this->response
= $response;
74 * Import the response's data on success and resolve the promise.
76 * @param SplSubject $client The observed HTTP client
77 * @param Request $request The request which generated the update
78 * @param object $progress The progress information
80 function update(SplSubject
$client, Request
$request = null, $progress = null) {
81 if ($request !== $this->request
) {
85 $this->notify((object) compact("client", "request", "progress"));
90 * @param callable $resolve
91 * @param callable $reject
93 private function complete(callable
$resolve, callable
$reject) {
94 $this->client
->detach($this);
96 if ($this->response
) {
98 $resolve($this->api
->import($this->response
));
99 } catch (Exception
$e) {
103 $reject($this->client
->getTransferInfo($this->request
)->error
);
108 * Cancellation callback
109 * @param callable $resolve
110 * @param callable $reject
112 private function cancel(callable
$resolve, callable
$reject) {
113 /* did we finish in the meantime? */
114 if ($this->response
) {
115 $this->complete($resolve, $reject);
117 $this->client
->detach($this);
118 $this->client
->dequeue($this->request
);
119 $reject("Cancelled");