7 use http\Client\Request
;
8 use http\Client\Response
;
9 use React\Promise\Deferred
;
14 class Call
extends Deferred
implements SplObserver
29 * The executed request
35 * The promised response
41 * Create a deferred promise for the response of $request
43 * @var \seekat\API $api The endpoint of the request
44 * @var Client $client The HTTP client to send the request
45 * @var Request The request to execute
47 function __construct(API
$api, Client
$client, Request
$request) {
49 $this->client
= $client;
50 $this->request
= $request;
52 parent
::__construct(function($resolve, $reject) {
53 return $this->cancel($resolve, $reject);
56 $client->attach($this);
57 $client->enqueue($request);
65 * Import the response's data on success and resolve the promise.
67 * @var SplSubject $client The observed HTTP client
68 * @var Request The request which generated the update
69 * @var object $progress The progress information
71 function update(SplSubject
$client, Request
$request = null, $progress = null) {
72 if ($request !== $this->request
) {
76 $this->notify((object) compact("client", "request", "progress"));
78 if ($progress->info
=== "finished") {
79 $this->response
= $this->client
->getResponse();
89 * @param callable $resolve
90 * @param callable $reject
92 private function complete(callable
$resolve, callable
$reject) {
93 $this->client
->detach($this);
95 if ($this->response
) {
97 $resolve($this->api
->import($this->response
));
98 } catch (Exception
$e) {
102 $reject($this->client
->getTransferInfo($this->request
)["error"]);
105 $this->client
->dequeue($this->request
);
109 * Cancellation callback
110 * @param callable $resolve
111 * @param callable $reject
113 private function cancel(callable
$resolve, callable
$reject) {
114 /* did we finish in the meantime? */
115 if ($this->response
) {
116 $this->complete($resolve, $reject);
118 $this->client
->detach($this);
119 $this->client
->dequeue($this->request
);
120 $reject("Cancelled");