-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathInoreader.php
468 lines (408 loc) · 12.2 KB
/
Inoreader.php
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
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
<?php
declare(strict_types=1);
namespace ExileeD\Inoreader;
use ExileeD\Inoreader\HttpClient\HttpClient;
use ExileeD\Inoreader\Exception\InoreaderException;
use ExileeD\Inoreader\Objects\ActiveSearch;
use ExileeD\Inoreader\Objects\AddSubscription;
use ExileeD\Inoreader\Objects\ItemIds;
use ExileeD\Inoreader\Objects\StreamContents;
use ExileeD\Inoreader\Objects\StreamPreferenceList;
use ExileeD\Inoreader\Objects\Subscriptions;
use ExileeD\Inoreader\Objects\Tag;
use ExileeD\Inoreader\Objects\Token;
use ExileeD\Inoreader\Objects\UnreadCount;
use ExileeD\Inoreader\Objects\UserInfo;
class Inoreader
{
/**
* The default base URL.
*
* @var string
*/
private const API_OAUTH = 'https://www.inoreader.com/oauth2/';
/**
* Api key
*
* @var string
*/
private $apiKey;
/**
* @var string
*/
private $apiSecret;
/**
* @var Client
*/
private $client;
public function __construct(int $apiId, string $apiKey, HttpClient $httpClient = null)
{
$this->apiKey = $apiId;
$this->apiSecret = $apiKey;
$client = new Client($httpClient);
$this->setClient($client);
}
/**
* @return string
*/
public function getAccessToken(): ?string
{
return $this->getClient()->getAccessToken();
}
/**
* @access public
* @return Client
*/
public function getClient(): Client
{
return $this->client;
}
/**
* @access public
*
* @param Client $client
*
* @return void
*/
public function setClient(Client $client): void
{
$this->client = $client;
}
/**
* @param string|null $accessToken
*/
public function setAccessToken(string $accessToken = null): void
{
$this->getClient()->setAccessToken($accessToken);
}
/**
* @param string $redirect_uri This is the address that the user will be redirected to when he authorizes
* your application from the consent page.
* @param string $scope You can pass read or read write
* @param string $state Up to 500 bytes of arbitrary data that will be passed back to your redirect URI.
*
* @return string
* @see https://www.inoreader.com/developers/oauth
*/
public function getLoginUrl(string $redirect_uri, string $state, string $scope = ''): string
{
$query = [
'client_id' => $this->apiKey,
'redirect_uri' => $redirect_uri,
'response_type' => 'code',
'scope' => $scope,
'state' => $state,
];
return self::API_OAUTH . 'auth' . '?' . http_build_query($query);
}
/**
* Refreshing an access token
*
* @param string $code
* @param string $redirect_uri
*
* @return Token
* @throws InoreaderException
* @see http://www.inoreader.com/developers/oauth
*/
public function accessTokenFromCode(string $code, string $redirect_uri): Token
{
$params = [
'code' => $code,
'redirect_uri' => $redirect_uri,
'client_id' => $this->apiKey,
'client_secret' => $this->apiSecret,
'scope' => '',
'grant_type' => 'authorization_code',
];
$response = $this->getClient()->post(self::API_OAUTH . 'token', [], $params);
return new Token($response);
}
/**
* Refreshing an access token
*
* @param string $refresh_token
*
* @return Token
* @throws InoreaderException
* @see http://www.inoreader.com/developers/oauth
*/
public function accessTokenFromRefresh(string $refreshToken): Token
{
$params = [
'client_id' => $this->apiKey,
'client_secret' => $this->apiSecret,
'refresh_token' => $refreshToken,
'grant_type' => 'refresh_token',
];
$response = $this->getClient()->post(self::API_OAUTH . 'token', [], $params);
return new Token($response);
}
/**
* Basic information about the logged in user.
*
* @see https://www.inoreader.com/developers/user-info
* @throws InoreaderException
* @return UserInfo
*/
public function userInfo(): UserInfo
{
$response = $this->getClient()->get('user-info');
return new UserInfo($response);
}
/**
* This method is used to subscribe to feeds.
*
* @param string $url feedId to subscribe to
*
* @return AddSubscription
* @throws InoreaderException
* @see https://www.inoreader.com/developers/add-subscription
*/
public function addSubscription(string $url): AddSubscription
{
$params = [
'quickadd' => $url,
];
$response = $this->getClient()->post('subscription/quickadd', $params);
return new AddSubscription($response);
}
/**
* This method is used to rename the subscription, add it to a folder, remove it from folder or unsubscribe from it.
*
* @param string $params['ac'] Action. Can be edit, subscribe, or unsubscribe.
* @param string $params['s'] Stream ID
* @param string $params['t'] Subscription title.
* @param string $params['a'] Add subscription from folder.
* @param string $params['r'] Remove subscription from folder.
*
* @return bool
* @throws InoreaderException
* @see http://www.inoreader.com/developers/edit-subscription
*/
public function editSubscription(array $params): bool
{
$this->getClient()->post('subscription/edit', $params);
return true;
}
/**
* Fetch the unread counters for folders, tags and feeds.
*
* @see https://www.inoreader.com/developers/unread-counts
* @throws InoreaderException
* @return UnreadCount
*/
public function unreadCount(): UnreadCount
{
$response = $this->getClient()->get('unread-count');
return new UnreadCount($response);
}
/**
* Fetches the current subscriptions for the logged user
*
* @see http://www.inoreader.com/developers/subscription-list
* @throws InoreaderException
* @return Subscriptions
*/
public function subscriptionList(): Subscriptions
{
$response = $this->getClient()->get('subscription/list');
return new Subscriptions($response);
}
/**
* Folders and tags list
*
* @param int|string $types Set to 1 to get the item type. Can be tag, folder or active_search
* @param int $counts Set to 1 to get unread counts for tags and active searches.
*
* @return Tag[]
* @throws InoreaderException
* @see http://www.inoreader.com/developers/tag-list
*/
public function tagsList($types = 1, $counts = 1): array
{
$response = $this->getClient()->get('tag/list', ['types' => $types, 'counts' => $counts]);
$result = [];
foreach ($response->tags as $tag) {
$result[] = new Tag($tag);
}
return $result;
}
/**
* Returns the articles for a given collection.
*
* @param string $streamId Streams can be feeds, tags (folders) or system types
* @param array $params
*
* @return StreamContents
* @throws InoreaderException
* @see http://www.inoreader.com/developers/stream-contents
*/
public function streamContents(string $streamId, array $params = []): StreamContents
{
$response = $this->getClient()->get(sprintf('stream/contents/%s', $streamId), $params);
return new StreamContents($response);
}
/**
* This method is used to return only the article ids for a given stream.
*
* @param array $params
*
* @return ItemIds
* @throws InoreaderException
* @see http://www.inoreader.com/developers/stream-contents
*/
public function itemsIds(array $params = []): ItemIds
{
$response = $this->getClient()->get('stream/items/ids', $params);
return new ItemIds($response);
}
/**
* List of folders and the system.
*
*
* @see http://www.inoreader.com/developers/preference-list
* @throws InoreaderException
* @return StreamPreferenceList
*/
public function streamPreferenceList(): StreamPreferenceList
{
$response = $this->getClient()->get('preference/stream/list');
return new StreamPreferenceList($response);
}
/**
* List of folders and the system.
*
* @param string $streamId Stream ID
* @param string|null $key Key Only accepted is subscription-ordering
* @param string|null $value Value.
*
* @return bool
* @throws InoreaderException
* @see http://www.inoreader.com/developers/preference-set
*/
public function streamPreferenceSet(string $streamId, $key = null, $value = null): bool
{
$this->getClient()->post(
'preference/stream/set',
[
's' => $streamId,
'k' => $key,
'v' => $value,
]
);
return true;
}
/**
* This method is used to rename tags and folders
*
* @param string $source Source name
* @param string $target Target name
*
* @return bool
* @throws InoreaderException
* @see http://www.inoreader.com/developers/rename-tag
*/
public function renameTag(string $source, string $target): bool
{
$this->getClient()->post(
'rename-tag',
[
's' => $source,
'dest' => $target,
]
);
return true;
}
/**
* This method is used to delete tags and folders.
*
* @param string $source Full tag name
*
* @return bool
* @throws InoreaderException
* @see http://www.inoreader.com/developers/delete-tag
*/
public function deleteTag(string $source): bool
{
$this->getClient()->post(
'disable-tag',
[
's' => $source,
]
);
return true;
}
/**
* This method is used to mark articles as read, or to star them.
*
* @param array $items Item IDs
* @param string|null $add Tag to add
* @param string|null $remove Tag to remove
*
* @return bool
* @throws InoreaderException
* @see http://www.inoreader.com/developers/edit-tag
*/
public function editTag(array $items, string $add = null, string $remove = null): bool
{
$params = [
'i' => implode(',', $items),
'a' => $add,
'r' => $remove,
];
$this->getClient()->post('edit-tag', $params);
return true;
}
/**
* This method marks all items in a given stream as read.
*
* @param int $timestamp Unix Timestamp in seconds or microseconds.
* @param string $streamId Stream ID
*
* @return bool
* @throws InoreaderException
* @see https://www.inoreader.com/developers/mark-all-as-read
*/
public function markAllAsRead(int $timestamp, string $streamId): bool
{
$this->getClient()->get(
'mark-all-as-read',
[
'ts' => $timestamp,
's' => $streamId,
]
);
return true;
}
/**
* This method create an active search.
*
* @param array $params
*
* @return ActiveSearch
* @throws InoreaderException
* @see https://www.inoreader.com/developers/active-search-create
*/
public function createActiveSearch(array $params): ActiveSearch
{
$response = $this->getClient()->post('active_search/create', [], $params);
return new ActiveSearch($response);
}
/**
* This method delete an active search.
*
* @param string $id Mandatory parameter
*
* @return bool
* @throws InoreaderException
* @see https://www.inoreader.com/developers/active-search-delete
*/
public function deleteActiveSearch(string $id): bool
{
$this->getClient()->get('active_search/delete', [
'id' => $id,
]);
return true;
}
}