aboutsummaryrefslogtreecommitdiffhomepage
path: root/src/Wallabag/ImportBundle/Import/PocketImport.php
diff options
context:
space:
mode:
authorJeremy Benoist <jeremy.benoist@gmail.com>2015-12-24 15:24:18 +0100
committerJeremy Benoist <jeremy.benoist@gmail.com>2016-01-02 23:27:41 +0100
commit7ec2897ee0ad190dcb9f77032d785f2f9661b754 (patch)
tree7f91eabb9d555628d0c2a1e5da8deb6fc2a41b3c /src/Wallabag/ImportBundle/Import/PocketImport.php
parent0aa344dc247c77376fcbf2112191f9f8b3dfc846 (diff)
downloadwallabag-7ec2897ee0ad190dcb9f77032d785f2f9661b754.tar.gz
wallabag-7ec2897ee0ad190dcb9f77032d785f2f9661b754.tar.zst
wallabag-7ec2897ee0ad190dcb9f77032d785f2f9661b754.zip
First test on PocketImport
Giving ability to define the Client add abitliy to easliy test the import.
Diffstat (limited to 'src/Wallabag/ImportBundle/Import/PocketImport.php')
-rw-r--r--src/Wallabag/ImportBundle/Import/PocketImport.php151
1 files changed, 74 insertions, 77 deletions
diff --git a/src/Wallabag/ImportBundle/Import/PocketImport.php b/src/Wallabag/ImportBundle/Import/PocketImport.php
index 85bab0db..e5c86f07 100644
--- a/src/Wallabag/ImportBundle/Import/PocketImport.php
+++ b/src/Wallabag/ImportBundle/Import/PocketImport.php
@@ -44,20 +44,83 @@ class PocketImport implements ImportInterface
44 } 44 }
45 45
46 /** 46 /**
47 * Create a new Client. 47 * {@inheritdoc}
48 */
49 public function oAuthRequest($redirectUri, $callbackUri)
50 {
51 $request = $this->client->createRequest('POST', 'https://getpocket.com/v3/oauth/request',
52 [
53 'body' => json_encode([
54 'consumer_key' => $this->consumerKey,
55 'redirect_uri' => $redirectUri,
56 ]),
57 ]
58 );
59
60 $response = $this->client->send($request);
61 $values = $response->json();
62
63 // store code in session for callback method
64 $this->session->set('pocketCode', $values['code']);
65
66 return 'https://getpocket.com/auth/authorize?request_token='.$values['code'].'&redirect_uri='.$callbackUri;
67 }
68
69 /**
70 * {@inheritdoc}
71 */
72 public function oAuthAuthorize()
73 {
74 $request = $this->client->createRequest('POST', 'https://getpocket.com/v3/oauth/authorize',
75 [
76 'body' => json_encode([
77 'consumer_key' => $this->consumerKey,
78 'code' => $this->session->get('pocketCode'),
79 ]),
80 ]
81 );
82
83 $response = $this->client->send($request);
84
85 return $response->json()['access_token'];
86 }
87
88 /**
89 * {@inheritdoc}
90 */
91 public function import($accessToken)
92 {
93 $request = $this->client->createRequest('POST', 'https://getpocket.com/v3/get',
94 [
95 'body' => json_encode([
96 'consumer_key' => $this->consumerKey,
97 'access_token' => $accessToken,
98 'detailType' => 'complete',
99 'state' => 'all',
100 'sort' => 'oldest',
101 ]),
102 ]
103 );
104
105 $response = $this->client->send($request);
106 $entries = $response->json();
107
108 $this->parsePocketEntries($entries['list']);
109
110 $this->session->getFlashBag()->add(
111 'notice',
112 $this->importedEntries.' entries imported, '.$this->skippedEntries.' already saved.'
113 );
114 }
115
116 /**
117 * Set the Guzzle client.
48 * 118 *
49 * @return Client 119 * @param Client $client
50 */ 120 */
51 private function createClient() 121 public function setClient(Client $client)
52 { 122 {
53 return new Client([ 123 $this->client = $client;
54 'defaults' => [
55 'headers' => [
56 'content-type' => 'application/json',
57 'X-Accept' => 'application/json',
58 ],
59 ],
60 ]);
61 } 124 }
62 125
63 /** 126 /**
@@ -165,70 +228,4 @@ class PocketImport implements ImportInterface
165 228
166 $this->em->flush(); 229 $this->em->flush();
167 } 230 }
168
169 public function oAuthRequest($redirectUri, $callbackUri)
170 {
171 $client = $this->createClient();
172 $request = $client->createRequest('POST', 'https://getpocket.com/v3/oauth/request',
173 [
174 'body' => json_encode([
175 'consumer_key' => $this->consumerKey,
176 'redirect_uri' => $redirectUri,
177 ]),
178 ]
179 );
180
181 $response = $client->send($request);
182 $values = $response->json();
183
184 // store code in session for callback method
185 $this->session->set('pocketCode', $values['code']);
186
187 return 'https://getpocket.com/auth/authorize?request_token='.$values['code'].'&redirect_uri='.$callbackUri;
188 }
189
190 public function oAuthAuthorize()
191 {
192 $client = $this->createClient();
193
194 $request = $client->createRequest('POST', 'https://getpocket.com/v3/oauth/authorize',
195 [
196 'body' => json_encode([
197 'consumer_key' => $this->consumerKey,
198 'code' => $this->session->get('pocketCode'),
199 ]),
200 ]
201 );
202
203 $response = $client->send($request);
204
205 return $response->json()['access_token'];
206 }
207
208 public function import($accessToken)
209 {
210 $client = $this->createClient();
211
212 $request = $client->createRequest('POST', 'https://getpocket.com/v3/get',
213 [
214 'body' => json_encode([
215 'consumer_key' => $this->consumerKey,
216 'access_token' => $accessToken,
217 'detailType' => 'complete',
218 'state' => 'all',
219 'sort' => 'oldest',
220 ]),
221 ]
222 );
223
224 $response = $client->send($request);
225 $entries = $response->json();
226
227 $this->parsePocketEntries($entries['list']);
228
229 $this->session->getFlashBag()->add(
230 'notice',
231 $this->importedEntries.' entries imported, '.$this->skippedEntries.' already saved.'
232 );
233 }
234} 231}