/** * @param string $url * @param string $representation * @throws \Exception * @return \Perry\Response */ public function doGetRequest($url, $representation) { if ($data = CacheManager::getInstance()->load($url)) { return new Response($data['value'], $data['representation']); } $response = $this->guzzle->get($url, $this->getOpts($representation)); $data = $response->getBody(); $data = (string) $data; if ($response->hasHeader("Content-Type")) { if (false !== ($retrep = Tool::parseContentTypeToRepresentation($response->getHeader("Content-Type")))) { $representation = $retrep; } } CacheManager::getInstance()->save($url, ["representation" => $representation, "value" => $data]); return new Response($data, $representation); }
/** * @param string $url * @param string $representation * @throws \Exception * @return \Perry\Response */ public function doGetRequest($url, $representation) { if ($data = CacheManager::getInstance()->load($url)) { return new Response($data['value'], $data['representation']); } $context = stream_context_create($this->getOpts($representation)); if (false === ($data = @file_get_contents($url, false, $context))) { if (false === ($headers = @get_headers($url, 1))) { throw new \Exception("could not connect to api"); } throw new \Exception("an error occured with the http request: " . $headers[0]); } else { $headers = @get_headers($url, 1); if (isset($headers['Content-Type'])) { if (false !== ($retrep = Tool::parseContentTypeToRepresentation($headers['Content-Type']))) { $representation = $retrep; } } } CacheManager::getInstance()->save($url, ["representation" => $representation, "value" => $data]); return new Response($data, $representation); }
public function testFileCache() { Setup::getInstance()->cacheImplementation = new FilePool("/tmp"); $this->assertTrue(CacheManager::getInstance()->save("mockurl", ['test' => 'something'])); $this->assertEquals(['test' => 'something'], CacheManager::getInstance()->load("mockurl")); }