X-Git-Url: https://git.m6w6.name/?a=blobdiff_plain;f=app%2FGithub%2FAPI%2FCall.php;h=4e0513db60b73a66212f9ac5a7e24c1df871fa5b;hb=2198c781a021d85ee845f2f4b2f5c322f73e6bd5;hp=d4afd19eeb3c375408162728293622a2f2e2b12c;hpb=eb76e9bb9a39fe2725301f6cf6fc3cf29bbc4e00;p=pharext%2Fpharext.org diff --git a/app/Github/API/Call.php b/app/Github/API/Call.php index d4afd19..4e0513d 100644 --- a/app/Github/API/Call.php +++ b/app/Github/API/Call.php @@ -4,10 +4,13 @@ namespace app\Github\API; 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 { /** @@ -36,9 +39,19 @@ 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 @@ -48,6 +61,7 @@ abstract class Call $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; @@ -57,13 +71,26 @@ abstract class Call } } - 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; } /** @@ -75,13 +102,6 @@ abstract class Call 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