function __construct($query, array $ctypes, array $args = []) { $args['ctypes'] = $ctypes; if ($query) { $args['search'] = $query; } parent::__construct($args); }
function __construct(array $args = [], $ctype) { if (!in_array($ctype, Client::$contentTypes)) { throw new Client\ClientException('Invalid content type passed', Client\ClientException::CODE_INVALID_CONTENTYPE); } $args['ctype'] = $ctype; parent::__construct($args); }
function __construct(array $args = [], array $types = []) { if ($types) { foreach ($types as $type) { if (!in_array($type, Client::$contentTypes)) { throw new Client\ClientException('Invalid content type passed', Client\ClientException::CODE_INVALID_CONTENTYPE); } } $args['ctypes'] = $types; } parent::__construct($args); }
/** * @param AbstractRequest $request * @return string * @throws \Exception */ function request(AbstractRequest $request) { $this->logger->info($request->getPath()); $args = $this->buildRequestArgs($request); if ($this->cachepool) { do { $cacheKey = $this->buildCacheKey($request, $args); $cacheKeyLog = preg_replace('/key=[^&]+/', 'key=*****', $cacheKey); $cacheItem = $this->cachepool->getItem($cacheKey); // Disabled on a request basis? if (!$request->isUseCache()) { break; } $this->logger->info('Cache enabled', ['key' => $cacheKeyLog]); $age = null; if (!$cacheItem->isMiss()) { $age = time() - $cacheItem->getCreation()->getTimestamp(); $cachedData = $this->_deserialize($this->cfg->getFormat(), $cacheItem->get()); } // Time to renew $ttr = $request->getTtr() !== null ? $request->getTtr() : $this->cfg->getCacheTtr(); // Return the cached data if it's not time yet to renew $returnCached = !$cacheItem->isMiss() && $age < $ttr; // if ($returnCached) { // // Check the latest hashes // $request->validateCache($this, $cachedData); // } if ($returnCached) { $msg = 'Using cache'; } else { if (!$age) { $msg = 'Cache MISS'; } else { $msg = 'NOT using cache'; } } $this->logger->info($msg, ['age' => $age, 'ttr' => $ttr, 'returnCached' => $returnCached]); if ($returnCached) { return $request->handleResponse($cachedData); } // @todo lock cache } while (false); } try { $uri = $this->cfg->getEndpoint() . $request->getPath() . '.' . $this->cfg->getFormat(); $this->logger->info('Refreshing resource', ['uri' => $uri, 'args' => http_build_query(array_merge($args, ['key' => '*****']))]); $body = $this->guzzle->get($uri, ['query' => $args])->getBody(); } catch (\Exception $e) { // Log and report $this->logger->error((string) $e); if ($this->debug) { throw $e; } // Return stale data if (isset($cacheItem) && isset($cachedData)) { $this->logger->info('Returning stale data!'); return $request->handleResponse($cachedData); } else { // Sad panda throw $e; } } $result = $body->getContents(); $deserialized = $this->_deserialize($this->cfg->getFormat(), $result); // Cache might be disabled, but still renew if there was cached data so that the new result gets returned in // case caching might be enabled again in a later request. if (isset($cacheItem) && ($request->isUseCache() || !$request->isUseCache() && !$cacheItem->isMiss())) { $this->logger->info($cacheItem->isMiss() ? 'Saving cache' : 'Refreshing cache'); $cacheItem->set($result, 365 * 60 * 60 * 24); // ttl not relevant } return $request->handleResponse($deserialized); }