/** * Retrieve the cached results of the api $request. * * @param RequestInterface $request A request for which the response may be cached. * * @return ResponseInterface|null */ public function get(RequestInterface $request) { $cached = $this->collection->findOne(['_id' => $request->getUrl()]); if ($cached === null) { return null; } return new Response($cached['httpCode'], $cached['headers'], $cached['body']); }
/** * Simulate sending a request to the API. * * @param RequestInterface $request The request. * * @return ResponseInterface */ public function send(RequestInterface $request) { $allResults = [['id' => 0, 'title' => 'a title for comic 0', 'resourceURI' => Client::BASE_URL . 'comics/0'], ['id' => 1, 'title' => 'a title for comic 1', 'resourceURI' => Client::BASE_URL . 'comics/1'], ['id' => 2, 'title' => 'a title for comic 2', 'resourceURI' => Client::BASE_URL . 'comics/2'], ['id' => 3, 'title' => 'a title for comic 3', 'resourceURI' => Client::BASE_URL . 'comics/3'], ['id' => 4, 'title' => 'a title for comic 4', 'resourceURI' => Client::BASE_URL . 'comics/4']]; $queryString = parse_url($request->getUrl(), PHP_URL_QUERY); $queryParams = []; parse_str($queryString, $queryParams); $offset = (int) $queryParams['offset']; $limit = (int) $queryParams['limit']; $results = array_slice($allResults, $offset, $limit); $count = count($results); return new Response(200, ['Content-type' => 'application/json', 'etag' => 'an etag'], ['code' => 200, 'status' => 'ok', 'etag' => 'an etag', 'data' => ['offset' => $offset, 'limit' => $limit, 'total' => 5, 'count' => $count, 'results' => $results]]); }
/** * Simulate sending a request to the API. * * @param RequestInterface $request The request. * * @return ResponseInterface */ public function send(RequestInterface $request) { $allResults = [['id' => 0, 'name' => 'a name for character 0', 'description' => 'a description for character 0', 'modified' => '2014-01-21T18:01:51-0500', 'resourceURI' => Client::BASE_URL . 'characters/0', 'urls' => [['type' => 'a type', 'url' => 'a url']], 'thumbnail' => ['path' => 'a path', 'extension' => 'an extension'], 'comics' => ['available' => 2, 'returned' => 1, 'collectionURI' => 'a comics collection uri', 'items' => [['resourceURI' => 'a comics resource uri', 'name' => 'a comics name']]], 'stories' => ['available' => 2, 'returned' => 1, 'collectionURI' => 'a stories collection uri', 'items' => [['resourceURI' => 'a stories resource uri', 'name' => 'a stories name']]], 'events' => ['available' => 2, 'returned' => 1, 'collectionURI' => 'a events collection uri', 'items' => [['resourceURI' => 'a events resource uri', 'name' => 'a events name']]], 'series' => ['available' => 2, 'returned' => 1, 'collectionURI' => 'a series collection uri', 'items' => [['resourceURI' => 'a series resource uri', 'name' => 'a series name']]]], ['id' => 1, 'name' => 'a name for character 1', 'description' => 'a description for character 1', 'modified' => '2014-01-21T18:01:51-0500', 'resourceURI' => Client::BASE_URL . 'characters/1', 'urls' => [['type' => 'a type', 'url' => 'a url']], 'thumbnail' => ['path' => 'a path', 'extension' => 'an extension'], 'comics' => ['available' => 2, 'returned' => 1, 'collectionURI' => 'a comics collection uri', 'items' => [['resourceURI' => 'a comics resource uri', 'name' => 'a comics name']]], 'stories' => ['available' => 2, 'returned' => 1, 'collectionURI' => 'a stories collection uri', 'items' => [['resourceURI' => 'a stories resource uri', 'name' => 'a stories name']]], 'events' => ['available' => 2, 'returned' => 1, 'collectionURI' => 'a events collection uri', 'items' => [['resourceURI' => 'a events resource uri', 'name' => 'a events name']]], 'series' => ['available' => 2, 'returned' => 1, 'collectionURI' => 'a series collection uri', 'items' => [['resourceURI' => 'a series resource uri', 'name' => 'a series name']]]], ['id' => 2, 'name' => 'a name for character 2', 'description' => 'a description for character 2', 'modified' => '2014-01-21T18:01:51-0500', 'resourceURI' => Client::BASE_URL . 'characters/2', 'urls' => [['type' => 'a type', 'url' => 'a url']], 'thumbnail' => ['path' => 'a path', 'extension' => 'an extension'], 'comics' => ['available' => 2, 'returned' => 1, 'collectionURI' => 'a comics collection uri', 'items' => [['resourceURI' => 'a comics resource uri', 'name' => 'a comics name']]], 'stories' => ['available' => 2, 'returned' => 1, 'collectionURI' => 'a stories collection uri', 'items' => [['resourceURI' => 'a stories resource uri', 'name' => 'a stories name']]], 'events' => ['available' => 2, 'returned' => 1, 'collectionURI' => 'a events collection uri', 'items' => [['resourceURI' => 'a events resource uri', 'name' => 'a events name']]], 'series' => ['available' => 2, 'returned' => 1, 'collectionURI' => 'a series collection uri', 'items' => [['resourceURI' => 'a series resource uri', 'name' => 'a series name']]]], ['id' => 3, 'name' => 'a name for character 3', 'description' => 'a description for character 3', 'modified' => '2014-01-21T18:01:51-0500', 'resourceURI' => Client::BASE_URL . 'characters/3', 'urls' => [['type' => 'a type', 'url' => 'a url']], 'thumbnail' => ['path' => 'a path', 'extension' => 'an extension'], 'comics' => ['available' => 2, 'returned' => 1, 'collectionURI' => 'a comics collection uri', 'items' => [['resourceURI' => 'a comics resource uri', 'name' => 'a comics name']]], 'stories' => ['available' => 2, 'returned' => 1, 'collectionURI' => 'a stories collection uri', 'items' => [['resourceURI' => 'a stories resource uri', 'name' => 'a stories name']]], 'events' => ['available' => 2, 'returned' => 1, 'collectionURI' => 'a events collection uri', 'items' => [['resourceURI' => 'a events resource uri', 'name' => 'a events name']]], 'series' => ['available' => 2, 'returned' => 1, 'collectionURI' => 'a series collection uri', 'items' => [['resourceURI' => 'a series resource uri', 'name' => 'a series name']]]], ['id' => 4, 'name' => 'a name for character 4', 'description' => 'a description for character 4', 'modified' => '2014-01-21T18:01:51-0500', 'resourceURI' => Client::BASE_URL . 'characters/4', 'urls' => [['type' => 'a type', 'url' => 'a url']], 'thumbnail' => ['path' => 'a path', 'extension' => 'an extension'], 'comics' => ['available' => 2, 'returned' => 1, 'collectionURI' => 'a comics collection uri', 'items' => [['resourceURI' => 'a comics resource uri', 'name' => 'a comics name']]], 'stories' => ['available' => 2, 'returned' => 1, 'collectionURI' => 'a stories collection uri', 'items' => [['resourceURI' => 'a stories resource uri', 'name' => 'a stories name']]], 'events' => ['available' => 2, 'returned' => 1, 'collectionURI' => 'a events collection uri', 'items' => [['resourceURI' => 'a events resource uri', 'name' => 'a events name']]], 'series' => ['available' => 2, 'returned' => 1, 'collectionURI' => 'a series collection uri', 'items' => [['resourceURI' => 'a series resource uri', 'name' => 'a series name']]]]]; $queryString = parse_url($request->getUrl(), PHP_URL_QUERY); $queryParams = []; parse_str($queryString, $queryParams); $this->parameters = $queryParams; $offset = (int) $queryParams['offset']; $limit = (int) $queryParams['limit']; $results = array_slice($allResults, $offset, $limit); $count = count($results); return new Response(200, ['Content-type' => 'application/json', 'etag' => 'an etag'], ['code' => 200, 'status' => 'ok', 'etag' => 'an etag', 'data' => ['offset' => $offset, 'limit' => $limit, 'total' => 5, 'count' => $count, 'results' => $results]]); }
/** * Simulate sending a request to the API. * * @param RequestInterface $request The request. * * @return ResponseInterface */ public function send(RequestInterface $request) { $allResults = self::getAllResults(); $total = count($allResults); $queryString = parse_url($request->getUrl(), PHP_URL_QUERY); $queryParams = []; parse_str($queryString, $queryParams); $path = parse_url($request->getUrl(), PHP_URL_PATH); if (substr($path, -6) !== 'comics') { $parts = explode('/', $request->getUrl()); $id = array_pop($parts); $queryParams['offset'] = $id - 1; $queryParams['limit'] = 1; $total = 1; } $this->parameters = $queryParams; $offset = (int) $queryParams['offset']; $limit = (int) $queryParams['limit']; $results = array_slice($allResults, $offset, $limit); $count = count($results); return new Response(200, ['Content-type' => 'application/json', 'etag' => 'an etag'], ['code' => 200, 'status' => 'ok', 'etag' => 'an etag', 'data' => ['offset' => $offset, 'limit' => $limit, 'total' => $total, 'count' => $count, 'results' => $results]]); }
/** * Simulate sending a request to the API. * * @param RequestInterface $request The request. * * @return ResponseInterface */ public function send(RequestInterface $request) { $allResults = self::getAllResults(); $queryString = parse_url($request->getUrl(), PHP_URL_QUERY); $queryParams = []; parse_str($queryString, $queryParams); $this->parameters = $queryParams; $offset = (int) $queryParams['offset']; $limit = (int) $queryParams['limit']; $results = array_slice($allResults, $offset, $limit); $count = count($results); return new Response(200, ['Content-type' => 'application/json', 'etag' => 'an etag'], ['code' => 200, 'status' => 'ok', 'etag' => 'an etag', 'data' => ['offset' => $offset, 'limit' => $limit, 'total' => 5, 'count' => $count, 'results' => $results]]); }
/** * Retrieve the cached results of the api $request. * * @param RequestInterface $request A request for which the response may be cached. * * @return ResponseInterface|null */ public function get(RequestInterface $request) { $id = $request->getUrl(); $cache = Arrays::get($this->cache, $id); if ($cache === null) { return null; } if ($cache['expires'] >= time()) { return $cache['response']; } unset($this->cache[$id]); return null; }
/** * Retrieve the cached results of the api $request. * * @param RequestInterface $request A request for which the response may be cached. * * @return ResponseInterface|null */ public function get(RequestInterface $request) { $cached = $this->collection->findOne(['_id' => $request->getUrl()]); if ($cached === null) { return null; } $headers = $cached['headers']; if ($headers instanceof BSONArray) { $headers = $headers->getArrayCopy(); } $body = $cached['body']; if ($body instanceof BSONArray) { $body = $body->getArrayCopy(); } return new Response($cached['httpCode'], $headers, $body); }
/** * Execute the specified request against the Marvel API. * * @param RequestInterface $request The request to send. * * @return ResponseInterface * * @throws \Exception Throws on error. */ public function send(RequestInterface $request) { $curlHeaders = ['Expect:']; //stops curl automatically putting in expect 100. foreach ($request->getHeaders() as $key => $value) { $curlHeaders[] = "{$key}: {$value}"; } $curlOptions = [CURLOPT_URL => $request->getUrl(), CURLOPT_RETURNTRANSFER => true, CURLOPT_VERBOSE => false, CURLOPT_HEADER => true, CURLOPT_FORBID_REUSE => true, CURLOPT_HTTPHEADER => $curlHeaders, CURLOPT_ENCODING => 'gzip,deflate']; if (strtoupper($request->getMethod()) !== 'GET') { throw new \Exception("Unsupported method '{$request->getMethod()}' given"); } $curl = Util::ensureNot(false, curl_init(), 'Unable to initialize connection'); Util::ensureNot(false, curl_setopt_array($curl, $curlOptions), 'Unable to prepare connection'); $result = Util::ensureNot(false, curl_exec($curl), curl_error($curl)); $headerSize = Util::ensureNot(false, curl_getinfo($curl, CURLINFO_HEADER_SIZE), 'Unable to determine header size'); $httpCode = Util::ensureNot(false, curl_getinfo($curl, CURLINFO_HTTP_CODE), 'Unable to determine response HTTP code'); $headers = Http::parseHeaders(substr($result, 0, $headerSize - 1)); $body = json_decode(substr($result, $headerSize), true); $error = Arrays::get([JSON_ERROR_DEPTH => 'The maximum stack depth has been exceeded', JSON_ERROR_STATE_MISMATCH => ' Invalid or malformed JSON', JSON_ERROR_CTRL_CHAR => 'Control character error, possibly incorrectly encoded', JSON_ERROR_SYNTAX => 'Syntax error', JSON_ERROR_UTF8 => 'Malformed UTF-8 characters, possibly incorrectly encoded'], json_last_error()); Util::ensure(null, $error, "Unable to parse response: {$error}"); return new Response($httpCode, $headers, $body ?: []); }
/** * Returns an empty Response. * * @param RequestInterface $request The request to send. * * @return ResponseInterface */ public function send(RequestInterface $request) { $this->request = $request; return new Response(404, ['code' => 'ResourceNotFound', 'message' => "{$request->getUrl()} was not found"], ['Response Code' => 404, 'Response Status' => 'Not Found', 'Content-Type' => 'application/json']); }