diff options
author | adev <adev2000@gmail.com> | 2017-10-24 22:55:40 +0200 |
---|---|---|
committer | Jeremy Benoist <jeremy.benoist@gmail.com> | 2019-05-28 11:40:41 +0200 |
commit | bf9ace0643f654e7ccd9c020b8b501ad56cd19de (patch) | |
tree | 4ff47ca915eb326307127b1b4caf69beda4036e3 /src/Wallabag/ImportBundle/Import | |
parent | 92a66835624acf6fd14f5adc5f8aab399658592e (diff) | |
download | wallabag-bf9ace0643f654e7ccd9c020b8b501ad56cd19de.tar.gz wallabag-bf9ace0643f654e7ccd9c020b8b501ad56cd19de.tar.zst wallabag-bf9ace0643f654e7ccd9c020b8b501ad56cd19de.zip |
Use httplug
Diffstat (limited to 'src/Wallabag/ImportBundle/Import')
-rw-r--r-- | src/Wallabag/ImportBundle/Import/PocketImport.php | 90 |
1 files changed, 47 insertions, 43 deletions
diff --git a/src/Wallabag/ImportBundle/Import/PocketImport.php b/src/Wallabag/ImportBundle/Import/PocketImport.php index a39d8156..9467fae2 100644 --- a/src/Wallabag/ImportBundle/Import/PocketImport.php +++ b/src/Wallabag/ImportBundle/Import/PocketImport.php | |||
@@ -2,13 +2,22 @@ | |||
2 | 2 | ||
3 | namespace Wallabag\ImportBundle\Import; | 3 | namespace Wallabag\ImportBundle\Import; |
4 | 4 | ||
5 | use GuzzleHttp\Client; | 5 | use Http\Client\Common\HttpMethodsClient; |
6 | use GuzzleHttp\Exception\RequestException; | 6 | use Http\Client\Common\Plugin\ErrorPlugin; |
7 | use Http\Client\Common\PluginClient; | ||
8 | use Http\Client\HttpClient; | ||
9 | use Http\Discovery\MessageFactoryDiscovery; | ||
10 | use Http\Message\MessageFactory; | ||
11 | use Http\Client\Exception\RequestException; | ||
7 | use Wallabag\CoreBundle\Entity\Entry; | 12 | use Wallabag\CoreBundle\Entity\Entry; |
13 | use Psr\Http\Message\ResponseInterface; | ||
8 | 14 | ||
9 | class PocketImport extends AbstractImport | 15 | class PocketImport extends AbstractImport |
10 | { | 16 | { |
11 | const NB_ELEMENTS = 5000; | 17 | const NB_ELEMENTS = 5000; |
18 | /** | ||
19 | * @var HttpMethodsClient | ||
20 | */ | ||
12 | private $client; | 21 | private $client; |
13 | private $accessToken; | 22 | private $accessToken; |
14 | 23 | ||
@@ -55,24 +64,18 @@ class PocketImport extends AbstractImport | |||
55 | */ | 64 | */ |
56 | public function getRequestToken($redirectUri) | 65 | public function getRequestToken($redirectUri) |
57 | { | 66 | { |
58 | $request = $this->client->createRequest('POST', 'https://getpocket.com/v3/oauth/request', | ||
59 | [ | ||
60 | 'body' => json_encode([ | ||
61 | 'consumer_key' => $this->user->getConfig()->getPocketConsumerKey(), | ||
62 | 'redirect_uri' => $redirectUri, | ||
63 | ]), | ||
64 | ] | ||
65 | ); | ||
66 | |||
67 | try { | 67 | try { |
68 | $response = $this->client->send($request); | 68 | $response = $this->client->post('https://getpocket.com/v3/oauth/request', [], json_encode([ |
69 | 'consumer_key' => $this->user->getConfig()->getPocketConsumerKey(), | ||
70 | 'redirect_uri' => $redirectUri, | ||
71 | ])); | ||
69 | } catch (RequestException $e) { | 72 | } catch (RequestException $e) { |
70 | $this->logger->error(sprintf('PocketImport: Failed to request token: %s', $e->getMessage()), ['exception' => $e]); | 73 | $this->logger->error(sprintf('PocketImport: Failed to request token: %s', $e->getMessage()), ['exception' => $e]); |
71 | 74 | ||
72 | return false; | 75 | return false; |
73 | } | 76 | } |
74 | 77 | ||
75 | return $response->json()['code']; | 78 | return $this->jsonDecode($response)['code']; |
76 | } | 79 | } |
77 | 80 | ||
78 | /** | 81 | /** |
@@ -85,24 +88,19 @@ class PocketImport extends AbstractImport | |||
85 | */ | 88 | */ |
86 | public function authorize($code) | 89 | public function authorize($code) |
87 | { | 90 | { |
88 | $request = $this->client->createRequest('POST', 'https://getpocket.com/v3/oauth/authorize', | ||
89 | [ | ||
90 | 'body' => json_encode([ | ||
91 | 'consumer_key' => $this->user->getConfig()->getPocketConsumerKey(), | ||
92 | 'code' => $code, | ||
93 | ]), | ||
94 | ] | ||
95 | ); | ||
96 | 91 | ||
97 | try { | 92 | try { |
98 | $response = $this->client->send($request); | 93 | $response = $this->client->post('https://getpocket.com/v3/oauth/authorize', [], json_encode([ |
94 | 'consumer_key' => $this->user->getConfig()->getPocketConsumerKey(), | ||
95 | 'code' => $code, | ||
96 | ])); | ||
99 | } catch (RequestException $e) { | 97 | } catch (RequestException $e) { |
100 | $this->logger->error(sprintf('PocketImport: Failed to authorize client: %s', $e->getMessage()), ['exception' => $e]); | 98 | $this->logger->error(sprintf('PocketImport: Failed to authorize client: %s', $e->getMessage()), ['exception' => $e]); |
101 | 99 | ||
102 | return false; | 100 | return false; |
103 | } | 101 | } |
104 | 102 | ||
105 | $this->accessToken = $response->json()['access_token']; | 103 | $this->accessToken = $this->jsonDecode($response)['access_token']; |
106 | 104 | ||
107 | return true; | 105 | return true; |
108 | } | 106 | } |
@@ -114,29 +112,23 @@ class PocketImport extends AbstractImport | |||
114 | { | 112 | { |
115 | static $run = 0; | 113 | static $run = 0; |
116 | 114 | ||
117 | $request = $this->client->createRequest('POST', 'https://getpocket.com/v3/get', | ||
118 | [ | ||
119 | 'body' => json_encode([ | ||
120 | 'consumer_key' => $this->user->getConfig()->getPocketConsumerKey(), | ||
121 | 'access_token' => $this->accessToken, | ||
122 | 'detailType' => 'complete', | ||
123 | 'state' => 'all', | ||
124 | 'sort' => 'newest', | ||
125 | 'count' => self::NB_ELEMENTS, | ||
126 | 'offset' => $offset, | ||
127 | ]), | ||
128 | ] | ||
129 | ); | ||
130 | |||
131 | try { | 115 | try { |
132 | $response = $this->client->send($request); | 116 | $response = $this->client->post('https://getpocket.com/v3/get', [], json_encode([ |
117 | 'consumer_key' => $this->user->getConfig()->getPocketConsumerKey(), | ||
118 | 'access_token' => $this->accessToken, | ||
119 | 'detailType' => 'complete', | ||
120 | 'state' => 'all', | ||
121 | 'sort' => 'newest', | ||
122 | 'count' => self::NB_ELEMENTS, | ||
123 | 'offset' => $offset, | ||
124 | ])); | ||
133 | } catch (RequestException $e) { | 125 | } catch (RequestException $e) { |
134 | $this->logger->error(sprintf('PocketImport: Failed to import: %s', $e->getMessage()), ['exception' => $e]); | 126 | $this->logger->error(sprintf('PocketImport: Failed to import: %s', $e->getMessage()), ['exception' => $e]); |
135 | 127 | ||
136 | return false; | 128 | return false; |
137 | } | 129 | } |
138 | 130 | ||
139 | $entries = $response->json(); | 131 | $entries = $this->jsonDecode($response); |
140 | 132 | ||
141 | if ($this->producer) { | 133 | if ($this->producer) { |
142 | $this->parseEntriesForProducer($entries['list']); | 134 | $this->parseEntriesForProducer($entries['list']); |
@@ -159,13 +151,14 @@ class PocketImport extends AbstractImport | |||
159 | } | 151 | } |
160 | 152 | ||
161 | /** | 153 | /** |
162 | * Set the Guzzle client. | 154 | * Set the Http client. |
163 | * | 155 | * |
164 | * @param Client $client | 156 | * @param HttpClient $client |
157 | * @param MessageFactory|null $messageFactory | ||
165 | */ | 158 | */ |
166 | public function setClient(Client $client) | 159 | public function setClient(HttpClient $client, MessageFactory $messageFactory = null) |
167 | { | 160 | { |
168 | $this->client = $client; | 161 | $this->client = new HttpMethodsClient(new PluginClient($client, [new ErrorPlugin()]), $messageFactory ?: MessageFactoryDiscovery::find()); |
169 | } | 162 | } |
170 | 163 | ||
171 | /** | 164 | /** |
@@ -252,4 +245,15 @@ class PocketImport extends AbstractImport | |||
252 | 245 | ||
253 | return $importedEntry; | 246 | return $importedEntry; |
254 | } | 247 | } |
248 | |||
249 | protected function jsonDecode(ResponseInterface $response) | ||
250 | { | ||
251 | $data = \json_decode((string) $response->getBody(), true); | ||
252 | |||
253 | if (JSON_ERROR_NONE !== json_last_error()) { | ||
254 | throw new \InvalidArgumentException('Unable to parse JSON data: ' . json_last_error_msg()); | ||
255 | } | ||
256 | |||
257 | return $data; | ||
258 | } | ||
255 | } | 259 | } |