use app\Github\API;
use app\Github\Storage\Item;
+use http\Client\Response;
use http\QueryString;
use http\Url;
use merry\Config;
+use React\Promise;
+
abstract class Call
{
/**
protected $query;
/**
- * Queue this call to the API client
+ * @var \React\Promise\Deferred
+ */
+ protected $deferred;
+
+ /**
+ * @return Request
+ */
+ abstract protected function request();
+
+ /**
+ * @return array
*/
- abstract function enqueue(callable $callback);
+ abstract protected function response(Response $response);
/**
* @param API $api
$this->api = $api;
$this->config = $this->api->getConfig();
$this->url = new Url($this->config->api->url, null, 0);
+ $this->deferred = new Promise\Deferred;
if ($args) {
$this->args = $args;
}
}
- function __invoke(callable $callback) {
- if ($this->readFromCache($cached)) {
- call_user_func_array($callback, $cached);
+ /**
+ * @return \React\Promise\Promise
+ */
+ function __invoke() {
+ if ($this->readFromCache($this->result)) {
+ return new Promise\FulfilledPromise($this->result);
} else {
- $this->enqueue($callback);
+ $this->api->getClient()->enqueue(
+ $this->request(),
+ function($response) {
+ try {
+ $this->deferred->resolve($this->response($response));
+ } catch (\Exception $e) {
+ $this->deferred->reject($e);
+ }
+ return true;
+ }
+ );
+ return $this->deferred->promise();
}
- return $this;
}
/**
return strtolower(end($parts));
}
- /**
- * Call Client::send()
- */
- function send() {
- return $this->api->getClient()->send();
- }
-
/**
* Get associated cache storage
* @param int $ttl out param of configure ttl