-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathReadItLater.php
More file actions
379 lines (323 loc) · 9.7 KB
/
Copy pathReadItLater.php
File metadata and controls
379 lines (323 loc) · 9.7 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
<?php
/**
* Read It Later API library for PHP
*
* LICENSE
*
* This source file is subject to the new BSD license that is bundled
* with this package in the file LICENSE.txt.
*
* @package ReadItLater
* @copyright Copyright (c) 2011 aoiaoi
* @license New BSD License
* @version $Id: ReadItLater.php 2011-11-10 $
*/
/**
* @package ReadItLater
* @copyright Copyright (c) 2011 aoiaoi
* @license New BSD License
* @version $Id: ReadItLater.php 2011-11-10 $
*/
class ReadItLater {
/** URI for other than text method */
const API_URI_BASE = 'https://readitlaterlist.com';
/** URI for text method */
const API_URI_TEXT = 'https://text.readitlaterlist.com';
/** Query paths */
const PATH_ADD = '/v2/add';
const PATH_SEND = '/v2/send';
const PATH_STATS = '/v2/stats';
const PATH_GET = '/v2/get';
const PATH_AUTH = '/v2/auth';
const PATH_SIGNUP = '/v2/signup';
const PATH_TEXT = '/v2/text';
const PATH_API = '/v2/api';
/**
* Your Read It Later API key
*
* @var string
*/
protected $_apiKey;
/**
* The username of the account being authenticated
*
* @var string
*/
protected $_username;
/**
* The password of the account being authenticated
*
* @var string
*/
protected $_password;
/**
* Zend_Rest_Client instance
*
* @var Zend_Rest_Client
*/
protected $_restClient = null;
/**
* Performs object initializations
*
* @param string $apikey
* @param string $username
* @param string $password
* @return void
*/
public function __construct($apikey, $username = null, $password = null) {
$this->setApiKey($apikey);
if (isset($username)) {
$this->setUsername($username);
}
if (isset($password)) {
$this->setPassword($password);
}
/**
* @see Zend_Rest_Client
*/
require_once 'Zend/Rest/Client.php';
$this->_restClient = new Zend_Rest_Client(self::API_URI_BASE);
}
/**
* Set the your Read It Later API key
*
* @param string $value
* @return ReadItLater
*/
public function setApiKey($value) {
$this->_apiKey = (string) $value;
return $this;
}
/**
* Retrieve your Read It Later API key
*
* @return string
*/
public function getApiKey() {
return $this->_apiKey;
}
/**
* Set the username of the account being authenticated
*
* @param string $value
* @return ReadItLater
*/
public function setUsername($value) {
$this->_username = (string) $value;
return $this;
}
/**
* Retrieve the username of the account being authenticated
*
* @return string
*/
public function getUsername() {
return $this->_username;
}
/**
* Set the password of the account being authenticated
*
* @param string $value
* @return ReadItLater
*/
public function setPassword($value) {
$this->_password = (string) $value;
return $this;
}
/**
* Retrieve the password of the account being authenticated
*
* @return string
*/
public function getPassword() {
return $this->_password;
}
/**
* List Management: Add - Add a page to a user's list
*
* @param string $url
* @param array $options : (title|ref_id)
* @return $response
*/
public function add($url, $options = array()) {
$parameters = array('apikey' => $this->getApiKey(),
'username' => $this->getUsername(),
'password' => $this->getPassword(),
'url' => $url);
$parameters = $this->_prepareParameters($parameters, $options);
$this->_validateAddMethod($parameters);
$this->_restClient->getHttpClient()->resetParameters();
$response = $this->_restClient->restGet(self::PATH_ADD, $parameters);
return $response;
}
/**
* List Management: Send - Mark items as read, add multiple pages, change titles, or set tags
*
* @param array $options : (new|read|update_title|update_tags)
* @return $response
*/
public function send($options = array()) {
$parameters = array('apikey' => $this->getApiKey(),
'username' => $this->getUsername(),
'password' => $this->getPassword());
$parameters = $this->_prepareParameters($parameters, $options);
$this->_validateSendMethod($parameters);
$this->_restClient->getHttpClient()->resetParameters();
$response = $this->_restClient->restGet(self::PATH_SEND, $parameters);
return $response;
}
/**
* List Management: Get - Retrieve a user's reading list
*
* @param array $options : (format|state|myAppOnly|since|count|page|tags)
* @return $response
*/
public function get($options = array()) {
$parameters = array('apikey' => $this->getApiKey(),
'username' => $this->getUsername(),
'password' => $this->getPassword());
if (!isset($options['count'])) { $options['count'] = 10; }
$parameters = $this->_prepareParameters($parameters, $options);
$this->_validateGetMethod($parameters);
$this->_restClient->getHttpClient()->resetParameters();
$response = $this->_restClient->restGet(self::PATH_GET, $parameters);
return $response;
}
/**
* List Management: Stats - Retrieve information about a user's list
*
* @param array $options : (format)
* @return $response
*/
public function stats($options = array()) {
$parameters = array('apikey' => $this->getApiKey(),
'username' => $this->getUsername(),
'password' => $this->getPassword());
$parameters = $this->_prepareParameters($parameters, $options);
$this->_validateStatsMetod($parameters);
$this->_restClient->getHttpClient()->resetParameters();
$response = $this->_restClient->restGet(self::PATH_STATS, $parameters);
return $response;
}
/**
* Account Management: Authenticate - Verify a user's account
*
* @return $response
*/
public function auth() {
$parameters = array('apikey' => $this->getApiKey(),
'username' => $this->getUsername(),
'password' => $this->getPassword());
$this->_restClient->getHttpClient()->resetParameters();
$response = $this->_restClient->restGet(self::PATH_AUTH, $parameters);
return $response;
}
/**
* Account Management: Signup - Register a new user
*
* @param string $username
* @param string $password
* @return $response
*/
public function signup($username, $password) {
$parameters = array('apikey' => $this->getApiKey(),
'username' => $username,
'password' => $password);
$this->_restClient->getHttpClient()->resetParameters();
$response = $this->_restClient->restGet(self::PATH_SIGNUP, $parameters);
return $response;
}
/**
* Text: Text - Get the text only version of a url
*
* @param string $url
* @param array $options : (mode|images)
* @return $response
*/
public function text($url, $options = array()) {
$parameters = array('apikey' => $this->getApiKey(),
'url' => $url);
$parameters = $this->_prepareParameters($parameters, $options);
$this->_validateTextMethod($parameters);
$this->_restClient->getHttpClient()->resetParameters();
$this->_restClient->setUri(self::API_URI_TEXT);
$response = $this->_restClient->restGet(self::PATH_TEXT, $parameters);
return $response;
}
/**
* API: API - Return stats / current rate limit information about your API key
*
* @return $response
*/
public function api() {
$parameters = array('apikey' => $this->getApiKey());
$this->_restClient->getHttpClient()->resetParameters();
$response = $this->_restClient->restGet(self::PATH_API, $parameters);
return $response;
}
/**
* Prepare parameters for the request
*
* @param array $parameters
* @param array $options
* @return array Merged array of user and optional/required parameters
*/
protected function _prepareParameters(array $parameters, array $options) {
return array_merge($options, $parameters);
}
/**
* Validate Add Method Parameters
*
* @param array $parameters
* @return void
*/
protected function _validateAddMethod(array $parameters) {
$validParameters = array('apikey', 'username', 'password', 'url', 'title', 'ref_id');
$this->_compareParameters($parameters, $validParameters);
}
/**
* Validate Send Method Parameters
*
* @param array $parameters
* @return void
*/
protected function _validateSendMethod(array $parameters) {
$validParameters = array('apikey', 'username', 'password', 'new', 'read', 'update_title', 'update_tags');
$this->_compareParameters($parameters, $validParameters);
}
/**
* Validate Get Method Parameters
*
* @param array $parameters
* @return void
*/
protected function _validateGetMethod(array $parameters) {
$validParameters = array('apikey', 'username', 'password', 'format', 'state',
'myAppOnly', 'since', 'count', 'page', 'tags');
$this->_compareParameters($parameters, $validParameters);
}
/**
* Validate Text Method Parameters
*
* @param array $parameters
* @return void
*/
protected function _validateTextMethod(array $parameters) {
$validParameters = array('apikey', 'url', 'mode', 'images');
$this->_compareParameters($parameters, $validParameters);
}
/**
* Throws an exception if and only if any user parameters are invalid
*
* @param array $parameters
* @param array $validParameters
* @return void
* @throws Exception
*/
protected function _compareParameters(array $parameters, array $validParameters) {
$difference = array_diff(array_keys($parameters), $validParameters);
if ($difference) {
throw new Exception('The following parameters are invalid: ' . implode(',', $difference));
}
}
}