diff options
author | Sean Molenaar <sean@seanmolenaar.eu> | 2019-03-17 20:17:40 +0100 |
---|---|---|
committer | Sean Molenaar <SMillerDev@users.noreply.github.com> | 2019-03-18 10:39:32 +0100 |
commit | 9b915e24aff1c1805f1951ea78c09becc2a36271 (patch) | |
tree | 7dc18c927c4fc1b6b1653bb6833f2bf717c6d103 /lib | |
parent | 6ed63bdedd859f6eb4e5ea4a992867a295a200fd (diff) |
Use a copy of the FeedIO client that doesn't specify a useragent
Diffstat (limited to 'lib')
-rw-r--r-- | lib/Config/FetcherConfig.php | 3 | ||||
-rw-r--r-- | lib/Fetcher/Client/FeedIoClient.php | 63 | ||||
-rw-r--r-- | lib/Fetcher/Client/LegacyGuzzleClient.php (renamed from lib/Config/LegacyGuzzleClient.php) | 3 | ||||
-rw-r--r-- | lib/Fetcher/Client/LegacyGuzzleResponse.php (renamed from lib/Config/LegacyGuzzleResponse.php) | 2 |
4 files changed, 67 insertions, 4 deletions
diff --git a/lib/Config/FetcherConfig.php b/lib/Config/FetcherConfig.php index 3110b0018..7631485f5 100644 --- a/lib/Config/FetcherConfig.php +++ b/lib/Config/FetcherConfig.php @@ -15,7 +15,8 @@ namespace OCA\News\Config; use FeedIo\Adapter\ClientInterface; use \GuzzleHttp\Client; -use \FeedIo\Adapter\Guzzle\Client as FeedIoClient; +use OCA\News\Fetcher\Client\FeedIoClient; +use OCA\News\Fetcher\Client\LegacyGuzzleClient; use OCP\IConfig; /** diff --git a/lib/Fetcher/Client/FeedIoClient.php b/lib/Fetcher/Client/FeedIoClient.php new file mode 100644 index 000000000..d9ce8d897 --- /dev/null +++ b/lib/Fetcher/Client/FeedIoClient.php @@ -0,0 +1,63 @@ +<?php +/* + * This file is part of the feed-io package. + * + * (c) Alexandre Debril <alex.debril@gmail.com> + * + * For the full copyright and license information, please view the LICENSE + * file that was distributed with this source code. + */ + +namespace OCA\News\Fetcher\Client; + +use FeedIo\Adapter\ClientInterface; +use FeedIo\Adapter\Guzzle\Response; +use FeedIo\Adapter\NotFoundException; +use FeedIo\Adapter\ServerErrorException; +use GuzzleHttp\Exception\BadResponseException; + +/** + * Guzzle dependent HTTP client + */ +class FeedIoClient implements ClientInterface +{ + /** + * @var \GuzzleHttp\ClientInterface + */ + protected $guzzleClient; + + /** + * @param \GuzzleHttp\ClientInterface $guzzleClient + */ + public function __construct(\GuzzleHttp\ClientInterface $guzzleClient) + { + $this->guzzleClient = $guzzleClient; + } + + /** + * @param string $url + * @param \DateTime $modifiedSince + * @throws \FeedIo\Adapter\NotFoundException + * @throws \FeedIo\Adapter\ServerErrorException + * @return \FeedIo\Adapter\ResponseInterface + */ + public function getResponse($url, \DateTime $modifiedSince) + { + try { + $options = [ + 'headers' => [ + 'If-Modified-Since' => $modifiedSince->format(\DateTime::RFC2822) + ] + ]; + + return new Response($this->guzzleClient->request('get', $url, $options)); + } catch (BadResponseException $e) { + switch ((int) $e->getResponse()->getStatusCode()) { + case 404: + throw new NotFoundException($e->getMessage()); + default: + throw new ServerErrorException($e->getMessage()); + } + } + } +} diff --git a/lib/Config/LegacyGuzzleClient.php b/lib/Fetcher/Client/LegacyGuzzleClient.php index bc1364c30..8e41221c9 100644 --- a/lib/Config/LegacyGuzzleClient.php +++ b/lib/Fetcher/Client/LegacyGuzzleClient.php @@ -9,7 +9,7 @@ * @copyright 2018 Sean Molenaar */ -namespace OCA\News\Config; +namespace OCA\News\Fetcher\Client; use FeedIo\Adapter\ClientInterface as FeedIoClientInterface; use FeedIo\Adapter\NotFoundException; @@ -47,7 +47,6 @@ class LegacyGuzzleClient implements FeedIoClientInterface try { $options = [ 'headers' => [ - 'User-Agent' => 'NextCloud-News/1.0', 'If-Modified-Since' => $modifiedSince->format(\DateTime::RFC2822) ] ]; diff --git a/lib/Config/LegacyGuzzleResponse.php b/lib/Fetcher/Client/LegacyGuzzleResponse.php index 9358eba15..1f3b2ddf1 100644 --- a/lib/Config/LegacyGuzzleResponse.php +++ b/lib/Fetcher/Client/LegacyGuzzleResponse.php @@ -9,7 +9,7 @@ * @copyright 2018 Sean Molenaar */ -namespace OCA\News\Config; +namespace OCA\News\Fetcher\Client; use FeedIo\Adapter\ResponseInterface; use GuzzleHttp\Message\ResponseInterface as GuzzleResponseInterface; |