diff options
author | John Molakvoæ <skjnldsv@users.noreply.github.com> | 2019-02-28 07:35:41 +0100 |
---|---|---|
committer | GitHub <noreply@github.com> | 2019-02-28 07:35:41 +0100 |
commit | 249352c9d3d1f22f1f06d0c46c842984d7910d30 (patch) | |
tree | 11a7e72337010ed79b3440edee3ec41e31916b37 /lib/Config/LegacyGuzzleClient.php | |
parent | 89d9b77994d564f4ef29db7cc7191c5cd87b8705 (diff) | |
parent | 7c17b2c24b1131ace6b464723978841566714f54 (diff) |
Merge pull request #282 from SMillerDev/switch_feedIO
Switch to feed-io for parsing
Diffstat (limited to 'lib/Config/LegacyGuzzleClient.php')
-rw-r--r-- | lib/Config/LegacyGuzzleClient.php | 65 |
1 files changed, 65 insertions, 0 deletions
diff --git a/lib/Config/LegacyGuzzleClient.php b/lib/Config/LegacyGuzzleClient.php new file mode 100644 index 000000000..bc1364c30 --- /dev/null +++ b/lib/Config/LegacyGuzzleClient.php @@ -0,0 +1,65 @@ +<?php +/** + * Nextcloud - News + * + * This file is licensed under the Affero General Public License version 3 or + * later. See the COPYING file. + * + * @author Sean Molenaar <smillernl@me.com> + * @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()); + } + } + } +} |