From 9b915e24aff1c1805f1951ea78c09becc2a36271 Mon Sep 17 00:00:00 2001 From: Sean Molenaar Date: Sun, 17 Mar 2019 20:17:40 +0100 Subject: Use a copy of the FeedIO client that doesn't specify a useragent --- lib/Config/FetcherConfig.php | 3 +- lib/Config/LegacyGuzzleClient.php | 65 ---------------------- lib/Config/LegacyGuzzleResponse.php | 86 ----------------------------- lib/Fetcher/Client/FeedIoClient.php | 63 +++++++++++++++++++++ lib/Fetcher/Client/LegacyGuzzleClient.php | 64 +++++++++++++++++++++ lib/Fetcher/Client/LegacyGuzzleResponse.php | 86 +++++++++++++++++++++++++++++ 6 files changed, 215 insertions(+), 152 deletions(-) delete mode 100644 lib/Config/LegacyGuzzleClient.php delete mode 100644 lib/Config/LegacyGuzzleResponse.php create mode 100644 lib/Fetcher/Client/FeedIoClient.php create mode 100644 lib/Fetcher/Client/LegacyGuzzleClient.php create mode 100644 lib/Fetcher/Client/LegacyGuzzleResponse.php 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/Config/LegacyGuzzleClient.php b/lib/Config/LegacyGuzzleClient.php deleted file mode 100644 index bc1364c30..000000000 --- a/lib/Config/LegacyGuzzleClient.php +++ /dev/null @@ -1,65 +0,0 @@ - - * @copyright 2018 Sean Molenaar - */ - -namespace OCA\News\Config; - -use FeedIo\Adapter\ClientInterface as FeedIoClientInterface; -use FeedIo\Adapter\NotFoundException; -use FeedIo\Adapter\ServerErrorException; -use Guzzle\Service\ClientInterface; -use GuzzleHttp\Exception\BadResponseException; - -/** - * Guzzle dependent HTTP client - */ -class LegacyGuzzleClient implements FeedIoClientInterface -{ - /** - * @var ClientInterface - */ - protected $guzzleClient; - - /** - * @param ClientInterface $guzzleClient - */ - public function __construct(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' => [ - 'User-Agent' => 'NextCloud-News/1.0', - 'If-Modified-Since' => $modifiedSince->format(\DateTime::RFC2822) - ] - ]; - - return new LegacyGuzzleResponse($this->guzzleClient->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/LegacyGuzzleResponse.php b/lib/Config/LegacyGuzzleResponse.php deleted file mode 100644 index 9358eba15..000000000 --- a/lib/Config/LegacyGuzzleResponse.php +++ /dev/null @@ -1,86 +0,0 @@ - - * @copyright 2018 Sean Molenaar - */ - -namespace OCA\News\Config; - -use FeedIo\Adapter\ResponseInterface; -use GuzzleHttp\Message\ResponseInterface as GuzzleResponseInterface; - -/** - * Guzzle dependent HTTP Response - */ -class LegacyGuzzleResponse implements ResponseInterface -{ - const HTTP_LAST_MODIFIED = 'Last-Modified'; - - /** - * @var \GuzzleHttp\Message\ResponseInterface - */ - protected $response; - - /** - * @param \GuzzleHttp\Message\ResponseInterface - */ - public function __construct(GuzzleResponseInterface $psrResponse) - { - $this->response = $psrResponse; - } - - /** - * @return boolean - */ - public function isModified() - { - return $this->response->getStatusCode() !== 304 && $this->response->getBody()->getSize() > 0; - } - - /** - * @return \Psr\Http\Message\StreamInterface - */ - public function getBody() - { - return $this->response->getBody(); - } - - /** - * @return \DateTime|null - */ - public function getLastModified() - { - if ($this->response->hasHeader(static::HTTP_LAST_MODIFIED)) { - $lastModified = \DateTime::createFromFormat( - \DateTime::RFC2822, - $this->getHeader(static::HTTP_LAST_MODIFIED) - ); - - return false === $lastModified ? null : $lastModified; - } - - return; - } - - /** - * @return array - */ - public function getHeaders() - { - return $this->response->getHeaders(); - } - - /** - * @param string $name - * @return string[] - */ - public function getHeader($name) - { - return current($this->response->getHeader($name)); - } -} 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 @@ + + * + * 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/Fetcher/Client/LegacyGuzzleClient.php b/lib/Fetcher/Client/LegacyGuzzleClient.php new file mode 100644 index 000000000..8e41221c9 --- /dev/null +++ b/lib/Fetcher/Client/LegacyGuzzleClient.php @@ -0,0 +1,64 @@ + + * @copyright 2018 Sean Molenaar + */ + +namespace OCA\News\Fetcher\Client; + +use FeedIo\Adapter\ClientInterface as FeedIoClientInterface; +use FeedIo\Adapter\NotFoundException; +use FeedIo\Adapter\ServerErrorException; +use Guzzle\Service\ClientInterface; +use GuzzleHttp\Exception\BadResponseException; + +/** + * Guzzle dependent HTTP client + */ +class LegacyGuzzleClient implements FeedIoClientInterface +{ + /** + * @var ClientInterface + */ + protected $guzzleClient; + + /** + * @param ClientInterface $guzzleClient + */ + public function __construct(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 LegacyGuzzleResponse($this->guzzleClient->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/Fetcher/Client/LegacyGuzzleResponse.php b/lib/Fetcher/Client/LegacyGuzzleResponse.php new file mode 100644 index 000000000..1f3b2ddf1 --- /dev/null +++ b/lib/Fetcher/Client/LegacyGuzzleResponse.php @@ -0,0 +1,86 @@ + + * @copyright 2018 Sean Molenaar + */ + +namespace OCA\News\Fetcher\Client; + +use FeedIo\Adapter\ResponseInterface; +use GuzzleHttp\Message\ResponseInterface as GuzzleResponseInterface; + +/** + * Guzzle dependent HTTP Response + */ +class LegacyGuzzleResponse implements ResponseInterface +{ + const HTTP_LAST_MODIFIED = 'Last-Modified'; + + /** + * @var \GuzzleHttp\Message\ResponseInterface + */ + protected $response; + + /** + * @param \GuzzleHttp\Message\ResponseInterface + */ + public function __construct(GuzzleResponseInterface $psrResponse) + { + $this->response = $psrResponse; + } + + /** + * @return boolean + */ + public function isModified() + { + return $this->response->getStatusCode() !== 304 && $this->response->getBody()->getSize() > 0; + } + + /** + * @return \Psr\Http\Message\StreamInterface + */ + public function getBody() + { + return $this->response->getBody(); + } + + /** + * @return \DateTime|null + */ + public function getLastModified() + { + if ($this->response->hasHeader(static::HTTP_LAST_MODIFIED)) { + $lastModified = \DateTime::createFromFormat( + \DateTime::RFC2822, + $this->getHeader(static::HTTP_LAST_MODIFIED) + ); + + return false === $lastModified ? null : $lastModified; + } + + return; + } + + /** + * @return array + */ + public function getHeaders() + { + return $this->response->getHeaders(); + } + + /** + * @param string $name + * @return string[] + */ + public function getHeader($name) + { + return current($this->response->getHeader($name)); + } +} -- cgit v1.2.3