namespace seekat\API;
use Exception;
-use http\Client;
-use http\Client\Request;
-use http\Client\Response;
+use http\ {
+ Client,
+ Client\Request,
+ Client\Response
+};
use React\Promise\Deferred;
use seekat\API;
use SplObserver;
{
/**
* The endpoint
- * @var \seekat\API
+ * @var API
*/
private $api;
/**
* Create a deferred promise for the response of $request
*
- * @var \seekat\API $api The endpoint of the request
- * @var Client $client The HTTP client to send the request
- * @var Request The request to execute
+ * @param API $api The endpoint of the request
+ * @param Client $client The HTTP client to send the request
+ * @param Request $request The request to execute
*/
function __construct(API $api, Client $client, Request $request) {
$this->api = $api;
parent::__construct(function($resolve, $reject) {
return $this->cancel($resolve, $reject);
});
-
+
$client->attach($this);
- $client->enqueue($request);
+ $client->enqueue($request, function(Response $response) {
+ $this->response = $response;
+ $this->complete(
+ [$this, "resolve"],
+ [$this, "reject"]
+ );
+ return true;
+ });
/* start off */
$client->once();
}
*
* Import the response's data on success and resolve the promise.
*
- * @var SplSubject $client The observed HTTP client
- * @var Request The request which generated the update
- * @var object $progress The progress information
+ * @param SplSubject $client The observed HTTP client
+ * @param Request $request The request which generated the update
+ * @param object $progress The progress information
*/
function update(SplSubject $client, Request $request = null, $progress = null) {
if ($request !== $this->request) {
}
$this->notify((object) compact("client", "request", "progress"));
-
- if ($progress->info === "finished") {
- $this->response = $this->client->getResponse();
- $this->complete(
- [$this, "resolve"],
- [$this, "reject"]
- );
- }
}
/**
$reject($e);
}
} else {
- $reject($this->client->getTransferInfo($this->request)["error"]);
+ $reject($this->client->getTransferInfo($this->request)->error);
}
-
- $this->client->dequeue($this->request);
}
/**