api = $api; $this->config = $api->getConfig(); $this->args = $args; $this->url = new Url("https://api.github.com/", null, 0); } function __toString() { $parts = explode("\\", get_class($this)); return strtolower(end($parts)); } abstract function getRequest(); abstract function getException($message, $code, $previous = null); function __invoke(callable $callback) { $this->enqueue($callback); return $this->api->getClient(); } protected function wrap(callable $callback) { return function($response) use($callback) { $rc = $response->getResponseCode(); if ($rc !== 201) { if ($response->getHeader("Content-Type", Header::class)->match("application/json", Header::MATCH_WORD)) { $message = json_decode($response->getBody())->message; } else { $message = $response->getBody(); } throw $this->getException($message, $rc); } $json = json_decode($response->getBody()); if (isset($json->error)) { throw $this->getException($json->error_description, $rc); } $callback($json); return true; }; } function enqueue(callable $callback) { $request = $this->getRequest(); $wrapper = $this->wrap($callback); return $this->api->getClient()->enqueue($request, $wrapper); } }