/** * @test */ public function shouldRenewCacheWhenResourceHasChanged() { $cache = $this->getCacheMock(); $response = new Response(200); $client = $this->getBrowserMock(); $client->expects($this->once())->method('send')->will($this->returnValue($response)); $httpClient = new CachedHttpClient(array('base_url' => ''), $client); $httpClient->setCache($cache); $cache->expects($this->once())->method('set')->with('test', $response); $cache->expects($this->once())->method('getModifiedSince')->with('test')->will($this->returnValue(1256953732)); $httpClient->get('test'); }
/** * {@inheritdoc} * * @return void */ public function initialize() { $httpClient = null; if (!empty($this->_config['useCache'])) { if ($this->_config['useCache'] === true) { $this->_config['useCache'] = 'default'; } $httpClient = new CachedHttpClient(); $httpClient->setCache(new CacheBridge($this->_config['useCache'])); } $this->_client = new Client($httpClient); $this->_authenticate(); }
/** * Register the service provider. * * @return void */ public function register() { $this->package('nwidart/activity'); $this->app->bind('Nwidart\\Activity\\EventFactoryInterface', 'Nwidart\\Activity\\Github\\GithubEventFactory'); $this->app['activity'] = $this->app->share(function ($app) { $driver = $app['config']->get('activity::driver'); $factoryClassName = "Nwidart\\Activity\\{$driver}\\{$driver}EventFactory"; $factory = new $factoryClassName(); $client = new CachedHttpClient(); $client->setCache(new FilesystemCache(app_path() . '/storage/github-api-cache')); $client = new Client($client); $client->authenticate($app['config']->get('activity::github.token'), 'http_token'); return new Activity($factory, $client); }); }
/** * Register the github cached http client class. * * @param \Illuminate\Contracts\Foundation\Application $app * @return void */ protected function registerGithubCachedHttpClient(Application $app) { $app->singleton('blogit.github.cachedHttpClient', function () { $cachedHttpClient = new CachedHttpClient(); $cachedHttpClient->setCache(new FilesystemCache(storage_path() . '/github-api-cache')); return $cachedHttpClient; }); $app->alias('blogit.github.cachedHttpClient', CachedHttpClient::class); }
/** * (non-PHPdoc) * * @see \ShowMeTheIssue\src\ShowMeTheIssue\Repo\RepoInterface::getIssues() * @return ShowMeTheIssue\Entity\Issue[] * @todo inject hydrator * @todo inject IssueService */ public function getIssuesFromRepo($account = null, $repo = null, array $filter = []) { if ($repo == null) { throw new \InvalidArgumentException('No repo parameter specified.'); } if ($account == null) { throw new \InvalidArgumentException('No account parameter specified for this repo: ' . $repo); } $issues = $this->client->api('issue')->all($account, $repo, $filter); $issueList = new IssueCollection(); $issueHydrator = new IssueHydrator(); $i = 0; foreach ($issues as $issue) { $issueObject = new Issue(); $issueHydrator->hydrate($issue, $issueObject); $issueList->append($issueObject); } return $issueList; }
public function request($path, $body = null, $httpMethod = 'GET', array $headers = array(), array $options = array()) { $path = $path . '?per_page=1000'; $cache = $this->getCache(); $has = $cache->has($path); if ($has && $this->maxage != 0) { if ($this->maxage < 0 || time() - $cache->getModifiedSince($path) <= $this->maxage) { return $cache->get($path); } } try { return parent::request($path, $body, $httpMethod, $headers, $options); } catch (Exception $e) { if ($has) { return $cache->get($path); } else { error_log("GitHub API request {$path} failed and no cache available! {$e}"); throw $e; } } }
/** * Use a Guzzle/Doctrine cache instead of the APIs */ private function addCache() { $cachePlugin = new CachePlugin(array('storage' => new DefaultCacheStorage(new DoctrineCacheAdapter(new FilesystemCache($this->app['paths']['cache'] . '/github'))))); $this->client->getHttpClient()->client->addSubscriber($cachePlugin); }
/** * Get a GitHub client. The GitHub client will be set to cache * responses via transients * * @return Client */ function github_client() { $cached = new CachedHttpClient(); $cached->setCache(new TransientCache()); return new Client($cached); }
/** * GitHub Clientのインスタンスを帰す。 * もし引数で設定されていればauth tokenを設定する。 * * @param InputInterface $input * @return Client */ protected function getGitHubClient(InputInterface $input) { $httpClient = null; if ($input->getOption('with-http-cache')) { $httpClient = new CachedHttpClient(); $httpClient->setCache(new FilesystemCache(__DIR__ . '/cache/github-api-cache')); } $client = new Client($httpClient); $token = $input->getOption('token'); if ($token) { $client->authenticate($token, null, Client::AUTH_URL_TOKEN); } return $client; }