X-Git-Url: https://git.m6w6.name/?a=blobdiff_plain;f=app%2FGithub%2FAPI%2FCall.php;h=21fd45f016fb808802531baa5113baa37dc3ef40;hb=HEAD;hp=22a69516e3f7fb60d466e3eac46b1522e6e13122;hpb=36cfa28cf2dcee3422f0231f91c6692eb28e7824;p=pharext%2Fpharext.org diff --git a/app/Github/API/Call.php b/app/Github/API/Call.php index 22a6951..21fd45f 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,14 +39,19 @@ abstract class Call protected $query; /** - * @var array + * @var \React\Promise\Deferred */ - protected $result; + protected $deferred; /** - * Queue this call to the API client + * @return Request */ - abstract function enqueue(callable $callback); + abstract protected function request(); + + /** + * @return array + */ + abstract protected function response(Response $response); /** * @param API $api @@ -53,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; @@ -62,13 +71,29 @@ abstract class Call } } - function __invoke(callable $callback) { + /** + * @return \React\Promise\Promise + */ + function __invoke() { if ($this->readFromCache($this->result)) { - call_user_func_array($callback, $this->result); + return new Promise\FulfilledPromise($this->result); } else { - $this->enqueue($callback); + $this->api->getClient()->enqueue( + $this->request(), + function($response) { + try { + $result = $this->response($response); + if (!($result instanceof Promise\PromiseInterface)) { + $this->deferred->resolve($result); + } + } catch (\Exception $e) { + $this->deferred->reject($e); + } + return true; + } + ); + return $this->deferred->promise(); } - return $this; } /** @@ -80,14 +105,6 @@ abstract class Call return strtolower(end($parts)); } - /** - * Call Client::send() - */ - function send() { - $this->api->getClient()->send(); - return $this->result; - } - /** * Get associated cache storage * @param int $ttl out param of configure ttl @@ -122,9 +139,17 @@ abstract class Call return false; } if (!$cache->get($key, $cached)) { + if ($cached) { + $this->api->getLogger()->debug( + sprintf("Cache-Stale: $this [TTL=%d]", $cached->getTTL()), + $this->args); + } else { + $this->api->getLogger()->debug("Cache-Miss: $this", $this->args); + } return false; } if (null !== $this->api->getMaxAge() && $cached->getAge() > $this->api->getMaxAge()) { + $this->api->getLogger()->debug("Cache-Refresh: $this", $this->args); return false; } $this->api->getLogger()->debug("Cache-Hit: $this", $this->args); @@ -142,6 +167,7 @@ abstract class Call $key = $this->getCacheKey(); $cache->set($key, new Item($fresh, $ttl)); + $this->api->getLogger()->debug("Cache-Push: $this", $this->args); } } @@ -149,6 +175,7 @@ abstract class Call if (($cache = $this->api->getCacheStorage())) { $key = $this->getCacheKey(); $cache->del($key); + $this->api->getLogger()->debug("Cache-Drop: $this", $this->args); } } }