-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathminiflux.py
1080 lines (949 loc) · 33.7 KB
/
miniflux.py
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
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
# The MIT License (MIT)
#
# Copyright (c) 2018-2024 Frederic Guillot
#
# Permission is hereby granted, free of charge, to any person obtaining a copy
# of this software and associated documentation files (the "Software"), to deal
# in the Software without restriction, including without limitation the rights
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
# copies of the Software, and to permit persons to whom the Software is
# furnished to do so, subject to the following conditions:
#
# The above copyright notice and this permission notice shall be included in
# all copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
# THE SOFTWARE.
import json
from typing import List, Optional, Union
import requests
DEFAULT_USER_AGENT = "Miniflux Python Client Library"
class ClientError(Exception):
"""
Exception raised when the API client receives an error response from the server.
Attributes:
status_code (int): The HTTP status code of the error response.
"""
def __init__(self, response: requests.Response):
self.status_code = response.status_code
self._response = response
def get_error_reason(self) -> str:
"""
Returns the error message from the response body, or a default message if not available.
Returns:
str: The error message from the response body, or a default message if not available.
"""
result = self._response.json()
default_reason = f"status_code={self.status_code}"
if isinstance(result, dict):
return result.get("error_message", default_reason)
return default_reason
class ResourceNotFound(ClientError):
"""
Exception raised when the API client receives a 404 response from the server.
"""
pass
class AccessForbidden(ClientError):
"""
Exception raised when the API client receives a 403 response from the server.
"""
pass
class AccessUnauthorized(ClientError):
"""
Exception raised when the API client receives a 401 response from the server.
"""
pass
class BadRequest(ClientError):
"""
Exception raised when the API client receives a 400 response from the server.
"""
pass
class ServerError(ClientError):
"""
Exception raised when the API client receives a 500 response from the server.
"""
pass
class Client:
"""
Miniflux API client.
"""
API_VERSION = 1
def __init__(
self,
base_url: str,
username: Optional[str] = None,
password: Optional[str] = None,
timeout: float = 30.0,
api_key: Optional[str] = None,
user_agent: str = DEFAULT_USER_AGENT,
):
self._base_url = base_url
self._api_key = api_key
self._username = username
self._password = password
self._timeout = timeout
self._auth: Optional[tuple] = (
(self._username, self._password) if not api_key else None
)
self._headers = {"User-Agent": user_agent}
if api_key:
self._headers["X-Auth-Token"] = api_key
def _get_endpoint(self, path: str) -> str:
if len(self._base_url) > 0 and self._base_url[-1:] == "/":
self._base_url = self._base_url[:-1]
return f"{self._base_url}/v{self.API_VERSION}{path}"
def _get_params(self, **kwargs) -> Optional[dict]:
params = {k: v for k, v in kwargs.items() if v}
return params if len(params) > 0 else None
def _get_modification_params(self, **kwargs) -> dict:
return {k: v for k, v in kwargs.items() if v is not None}
def _handle_error_response(self, response: requests.Response):
if response.status_code == 404:
raise ResourceNotFound(response)
if response.status_code == 403:
raise AccessForbidden(response)
if response.status_code == 401:
raise AccessUnauthorized(response)
if response.status_code == 400:
raise BadRequest(response)
if response.status_code >= 500:
raise ServerError(response)
raise ClientError(response)
def flush_history(self) -> bool:
"""
Mark all read entries as removed excepted the starred ones.
Returns:
bool: True if the operation was successfully scheduled, False otherwise.
"""
endpoint = self._get_endpoint("/flush-history")
response = requests.delete(
endpoint, headers=self._headers, auth=self._auth, timeout=self._timeout
)
if response.status_code == 202:
return True
self._handle_error_response(response)
def get_version(self) -> dict:
"""
Get the version information of the Miniflux instance.
Returns:
A dictionary containing the version information.
Raises:
ClientError: If the request fails.
"""
endpoint = self._get_endpoint("/version")
response = requests.get(
endpoint, headers=self._headers, auth=self._auth, timeout=self._timeout
)
if response.status_code == 200:
return response.json()
self._handle_error_response(response)
def me(self) -> dict:
"""
Get the authenticated user's information.
Returns:
A dictionary containing the user's information.
Raises:
ClientError: If the request fails.
"""
endpoint = self._get_endpoint("/me")
response = requests.get(
endpoint, headers=self._headers, auth=self._auth, timeout=self._timeout
)
if response.status_code == 200:
return response.json()
self._handle_error_response(response)
def export(self) -> str:
"""
Export the user's feeds in OPML format.
Returns:
str: The OPML data.
Raises:
ClientError: If the request fails.
"""
return self.export_feeds()
def export_feeds(self) -> str:
"""
Export the user's feeds in OPML format.
Returns:
str: The OPML data.
Raises:
ClientError: If the request fails.
"""
endpoint = self._get_endpoint("/export")
response = requests.get(
endpoint, headers=self._headers, auth=self._auth, timeout=self._timeout
)
if response.status_code == 200:
return response.text
self._handle_error_response(response)
def import_feeds(self, opml: str) -> dict:
"""
Import feeds from an OPML file.
Args:
opml (str): The OPML data.
Returns:
A dictionary containing the import result.
Raises:
ClientError: If the request fails.
"""
endpoint = self._get_endpoint("/import")
response = requests.post(
endpoint,
headers=self._headers,
data=opml,
auth=self._auth,
timeout=self._timeout,
)
if response.status_code == 201:
return response.json()
self._handle_error_response(response)
def discover(self, website_url: str, **kwargs) -> List[dict]:
"""
Discover feeds from a website.
Args:
website_url (str): The website URL.
Returns:
List of feeds.
Raises:
ClientError: If the request fails.
"""
endpoint = self._get_endpoint("/discover")
data = dict(url=website_url)
data.update(kwargs)
response = requests.post(
endpoint,
headers=self._headers,
auth=self._auth,
data=json.dumps(data),
timeout=self._timeout,
)
if response.status_code == 200:
return response.json()
self._handle_error_response(response)
def get_category_feeds(self, category_id: int) -> List[dict]:
"""
Retrieves a list of feeds for a given category.
Args:
category_id (int): The category ID.
Returns:
A list of dictionaries representing the feeds.
Raises:
ClientError: If the request fails.
"""
endpoint = self._get_endpoint(f"/categories/{category_id}/feeds")
response = requests.get(
endpoint, headers=self._headers, auth=self._auth, timeout=self._timeout
)
if response.status_code == 200:
return response.json()
self._handle_error_response(response)
def get_feeds(self) -> List[dict]:
"""
Retrieves a list of all feeds.
Returns:
A list of dictionaries representing the feeds.
Raises:
ClientError: If the request fails.
"""
endpoint = self._get_endpoint("/feeds")
response = requests.get(
endpoint, headers=self._headers, auth=self._auth, timeout=self._timeout
)
if response.status_code == 200:
return response.json()
self._handle_error_response(response)
def get_feed(self, feed_id: int) -> dict:
"""
Retrieves a feed.
Args:
feed_id (int): The feed ID.
Returns:
A dictionary representing the feed.
Raises:
ClientError: If the request fails.
"""
endpoint = self._get_endpoint(f"/feeds/{feed_id}")
response = requests.get(
endpoint, headers=self._headers, auth=self._auth, timeout=self._timeout
)
if response.status_code == 200:
return response.json()
self._handle_error_response(response)
def get_feed_icon(self, feed_id: int) -> dict:
"""
Retrieves a feed icon.
Args:
feed_id (int): The feed ID.
Returns:
A dictionary representing the feed icon.
Raises:
ClientError: If the request fails.
"""
endpoint = self._get_endpoint(f"/feeds/{feed_id}/icon")
response = requests.get(
endpoint, headers=self._headers, auth=self._auth, timeout=self._timeout
)
if response.status_code == 200:
return response.json()
self._handle_error_response(response)
def get_icon(self, icon_id: int) -> dict:
"""
Retrieves a feed icon.
Args:
icon_id (int): The icon ID.
Returns:
A dictionary representing the feed icon.
Raises:
ClientError: If the request fails.
"""
endpoint = self._get_endpoint(f"/icons/{icon_id}")
response = requests.get(
endpoint, headers=self._headers, auth=self._auth, timeout=self._timeout
)
if response.status_code == 200:
return response.json()
self._handle_error_response(response)
def get_icon_by_feed_id(self, feed_id: int) -> dict:
"""
Retrieves a feed icon.
Args:
feed_id (int): The feed ID.
Returns:
A dictionary representing the feed icon.
Raises:
ClientError: If the request fails.
"""
return self.get_feed_icon(feed_id)
def create_feed(
self, feed_url: str, category_id: Optional[int] = None, **kwargs
) -> int:
"""
Create a new feed.
Args:
feed_url (str): The feed URL.
category_id (int): The category ID.
Returns:
int: The feed ID.
Raises:
ClientError: If the request fails.
"""
endpoint = self._get_endpoint("/feeds")
data = dict(feed_url=feed_url, category_id=category_id)
data.update(kwargs)
response = requests.post(
endpoint,
headers=self._headers,
auth=self._auth,
data=json.dumps(data),
timeout=self._timeout,
)
if response.status_code == 201:
return response.json()["feed_id"]
self._handle_error_response(response)
def update_feed(self, feed_id: int, **kwargs) -> dict:
"""
Update a feed.
Args:
feed_id (int): The feed ID.
Returns:
A dictionary representing the updated feed.
Raises:
ClientError: If the request fails.
"""
endpoint = self._get_endpoint(f"/feeds/{feed_id}")
data = self._get_modification_params(**kwargs)
response = requests.put(
endpoint,
headers=self._headers,
auth=self._auth,
data=json.dumps(data),
timeout=self._timeout,
)
if response.status_code == 201:
return response.json()
self._handle_error_response(response)
def refresh_all_feeds(self) -> bool:
"""
Refresh all feeds.
Returns:
bool: True if the operation was successfully scheduled, False otherwise.
Raises:
ClientError: If the request fails.
"""
endpoint = self._get_endpoint("/feeds/refresh")
response = requests.put(
endpoint, headers=self._headers, auth=self._auth, timeout=self._timeout
)
if response.status_code >= 400:
self._handle_error_response(response)
return True
def refresh_feed(self, feed_id: int) -> bool:
"""
Refreshes a single feed.
Args:
feed_id (int): The feed ID.
Returns:
bool: True if the operation was successfully scheduled, False otherwise.
Raises:
ClientError: If the request fails.
"""
endpoint = self._get_endpoint(f"/feeds/{feed_id}/refresh")
response = requests.put(
endpoint, headers=self._headers, auth=self._auth, timeout=self._timeout
)
if response.status_code >= 400:
self._handle_error_response(response)
return True
def refresh_category(self, category_id: int) -> bool:
"""
Refreshes all feeds that belongs to the given category.
Args:
category_id (int): The category ID.
Returns:
bool: True if the operation was successfully scheduled, False otherwise.
Raises:
ClientError: If the request fails.
"""
endpoint = self._get_endpoint(f"/categories/{category_id}/refresh")
response = requests.put(
endpoint, headers=self._headers, auth=self._auth, timeout=self._timeout
)
if response.status_code >= 400:
self._handle_error_response(response)
return True
def delete_feed(self, feed_id: int) -> None:
"""
Delete a feed.
Args:
feed_id (int): The feed ID.
Raises:
ClientError: If the request fails.
"""
endpoint = self._get_endpoint(f"/feeds/{feed_id}")
response = requests.delete(
endpoint, headers=self._headers, auth=self._auth, timeout=self._timeout
)
if response.status_code != 204:
self._handle_error_response(response)
def get_feed_entry(self, feed_id: int, entry_id: int) -> dict:
"""
Fetch a single entry for a given feed.
Args:
feed_id (int): The feed ID.
entry_id (int): The entry ID.
Returns:
A dictionary representing the entry.
Raises:
ClientError: If the request fails.
"""
endpoint = self._get_endpoint(f"/feeds/{feed_id}/entries/{entry_id}")
response = requests.get(
endpoint, headers=self._headers, auth=self._auth, timeout=self._timeout
)
if response.status_code == 200:
return response.json()
self._handle_error_response(response)
def get_feed_entries(self, feed_id: int, **kwargs) -> dict:
"""
Fetch all entries that belongs to the given feed.
Args:
feed_id (int): The feed ID.
Returns:
A list of dictionaries representing the entries.
Raises:
ClientError: If the request fails.
"""
endpoint = self._get_endpoint(f"/feeds/{feed_id}/entries")
params = self._get_params(**kwargs)
response = requests.get(
endpoint,
headers=self._headers,
auth=self._auth,
params=params,
timeout=self._timeout,
)
if response.status_code == 200:
return response.json()
self._handle_error_response(response)
def mark_feed_entries_as_read(self, feed_id: int) -> None:
"""
Mark all entries as read in the given feed.
Args:
feed_id (int): The feed ID.
Returns:
A list of dictionaries representing the entries.
Raises:
ClientError: If the request fails.
"""
endpoint = self._get_endpoint(f"/feeds/{feed_id}/mark-all-as-read")
response = requests.put(
endpoint, headers=self._headers, auth=self._auth, timeout=self._timeout
)
if response.status_code != 204:
self._handle_error_response(response)
def get_entry(self, entry_id: int) -> dict:
"""
Fetch a single entry.
Args:
entry_id (int): The entry ID.
Returns:
A dictionary representing the entry.
Raises:
ClientError: If the request fails.
"""
endpoint = self._get_endpoint(f"/entries/{entry_id}")
response = requests.get(
endpoint, headers=self._headers, auth=self._auth, timeout=self._timeout
)
if response.status_code == 200:
return response.json()
self._handle_error_response(response)
def get_entries(self, **kwargs) -> dict:
"""
Fetch all entries.
Returns:
A list of dictionaries representing the entries.
Raises:
ClientError: If the request fails.
"""
endpoint = self._get_endpoint("/entries")
params = self._get_params(**kwargs)
response = requests.get(
endpoint,
headers=self._headers,
auth=self._auth,
params=params,
timeout=self._timeout,
)
if response.status_code == 200:
return response.json()
self._handle_error_response(response)
def update_entry(
self, entry_id: int, title: Optional[str] = None, content: Optional[str] = None
) -> dict:
"""
Update an entry.
Args:
entry_id (int): The entry ID.
title (str): The entry title.
content (str): The entry content.
Returns:
A dictionary representing the updated entry.
Raises:
ClientError: If the request fails.
"""
endpoint = self._get_endpoint(f"/entries/{entry_id}")
data = self._get_modification_params(
**{
"title": title,
"content": content,
}
)
response = requests.put(
endpoint,
headers=self._headers,
auth=self._auth,
data=json.dumps(data),
timeout=self._timeout,
)
if response.status_code == 201:
return response.json()
self._handle_error_response(response)
def update_entries(self, entry_ids: List[int], status: str) -> bool:
"""
Change the status of multiple entries.
Args:
entry_ids (list[int]): The entry IDs.
status (str): The new status.
Returns:
bool: True if the operation was successful, False otherwise.
Raises:
ClientError: If the request fails.
"""
endpoint = self._get_endpoint("/entries")
data = {"entry_ids": entry_ids, "status": status}
response = requests.put(
endpoint,
headers=self._headers,
auth=self._auth,
data=json.dumps(data),
timeout=self._timeout,
)
if response.status_code >= 400:
self._handle_error_response(response)
return True
def fetch_entry_content(self, entry_id: int) -> dict:
"""
Scrape the entry original URL and returns the content.
Args:
entry_id (int): The entry ID.
Returns:
A dictionary representing the entry content.
Raises:
ClientError: If the request fails.
"""
endpoint = self._get_endpoint(f"/entries/{entry_id}/fetch-content")
response = requests.get(
endpoint, headers=self._headers, auth=self._auth, timeout=self._timeout
)
if response.status_code == 200:
return response.json()
self._handle_error_response(response)
def toggle_bookmark(self, entry_id: int) -> bool:
"""
Star or unstar an entry.
Args:
entry_id (int): The entry ID.
Returns:
bool: True if the operation was successful, False otherwise.
Raises:
ClientError: If the request fails.
"""
endpoint = self._get_endpoint(f"/entries/{entry_id}/bookmark")
response = requests.put(
endpoint, headers=self._headers, auth=self._auth, timeout=self._timeout
)
if response.status_code >= 400:
self._handle_error_response(response)
return True
def save_entry(self, entry_id: int) -> bool:
"""
Send an entry to a third-party service if enabled.
Args:
entry_id (int): The entry ID.
Returns:
bool: True if the operation was successfully queued, False otherwise.
Raises:
ClientError: If the request fails.
"""
endpoint = self._get_endpoint(f"/entries/{entry_id}/save")
response = requests.post(
endpoint, headers=self._headers, auth=self._auth, timeout=self._timeout
)
if response.status_code != 202:
self._handle_error_response(response)
return True
def get_enclosure(self, enclosure_id: int) -> dict:
"""
Fetch an enclosure.
Args:
enclosure_id (int): The enclosure ID.
Returns:
A dictionary representing the enclosure.
Raises:
ClientError: If the request fails.
"""
endpoint = self._get_endpoint(f"/enclosures/{enclosure_id}")
response = requests.get(
endpoint, headers=self._headers, auth=self._auth, timeout=self._timeout
)
if response.status_code == 200:
return response.json()
self._handle_error_response(response)
def update_enclosure(
self, enclosure_id: int, media_progression: Optional[int] = None
) -> bool:
"""
Update an enclosure.
Args:
enclosure_id (int): The enclosure ID.
media_progression (int): The progression of the media.
Returns:
bool: True if the operation was successful, False otherwise.
Raises:
ClientError: If the request fails.
"""
endpoint = self._get_endpoint(f"/enclosures/{enclosure_id}")
data = self._get_modification_params(media_progression=media_progression)
response = requests.put(
endpoint,
headers=self._headers,
auth=self._auth,
data=json.dumps(data),
timeout=self._timeout,
)
if response.status_code != 204:
self._handle_error_response(response)
return True
def get_categories(self) -> List[dict]:
"""
Fetch all categories.
Returns:
A list of dictionaries representing the categories.
Raises:
ClientError: If the request fails.
"""
endpoint = self._get_endpoint("/categories")
response = requests.get(
endpoint, headers=self._headers, auth=self._auth, timeout=self._timeout
)
if response.status_code == 200:
return response.json()
self._handle_error_response(response)
def get_category_entry(self, category_id: int, entry_id: int) -> dict:
"""
Fetch a single entry for a given category.
Args:
category_id (int): The category ID.
entry_id (int): The entry ID.
Returns:
A dictionary representing the entry.
Raises:
ClientError: If the request fails.
"""
endpoint = self._get_endpoint(f"/categories/{category_id}/entries/{entry_id}")
response = requests.get(
endpoint, headers=self._headers, auth=self._auth, timeout=self._timeout
)
if response.status_code == 200:
return response.json()
self._handle_error_response(response)
def get_category_entries(self, category_id: int, **kwargs) -> dict:
"""
Fetch all entries for a given category.
Args:
category_id (int): The category ID.
Returns:
A list of dictionaries representing the entries.
Raises:
ClientError: If the request fails.
"""
endpoint = self._get_endpoint(f"/categories/{category_id}/entries")
params = self._get_params(**kwargs)
response = requests.get(
endpoint,
headers=self._headers,
auth=self._auth,
params=params,
timeout=self._timeout,
)
if response.status_code == 200:
return response.json()
self._handle_error_response(response)
def create_category(self, title: str) -> dict:
"""
Create a new category.
Args:
title (str): The category title.
Returns:
A dictionary representing the created category.
Raises:
ClientError: If the request fails.
"""
endpoint = self._get_endpoint("/categories")
data = {"title": title}
response = requests.post(
endpoint,
headers=self._headers,
auth=self._auth,
data=json.dumps(data),
timeout=self._timeout,
)
if response.status_code == 201:
return response.json()
self._handle_error_response(response)
def update_category(self, category_id: int, title: str) -> dict:
"""
Update a category.
Args:
category_id (int): The category ID.
title (str): The category title.
Returns:
A dictionary representing the updated category.
Raises:
ClientError: If the request fails.
"""
endpoint = self._get_endpoint(f"/categories/{category_id}")
data = {"id": category_id, "title": title}
response = requests.put(
endpoint,
headers=self._headers,
auth=self._auth,
data=json.dumps(data),
timeout=self._timeout,
)
if response.status_code == 201:
return response.json()
self._handle_error_response(response)
def delete_category(self, category_id: int) -> None:
"""
Delete a category.
Args:
category_id (int): The category ID.
Raises:
ClientError: If the request fails.
"""
endpoint = self._get_endpoint(f"/categories/{category_id}")
response = requests.delete(
endpoint, headers=self._headers, auth=self._auth, timeout=self._timeout
)
if response.status_code != 204:
self._handle_error_response(response)
def mark_category_entries_as_read(self, category_id: int) -> None:
"""
Mark all entries as read in the given category.
Args:
category_id (int): The category ID.
Raises:
ClientError: If the request fails.
"""
endpoint = self._get_endpoint(f"/categories/{category_id}/mark-all-as-read")
response = requests.put(
endpoint, headers=self._headers, auth=self._auth, timeout=self._timeout
)
if response.status_code != 204:
self._handle_error_response(response)
def get_users(self) -> List[dict]:
"""
Fetch all users.
Returns:
A list of dictionaries representing users.
Raises:
ClientError: If the request fails.
"""
endpoint = self._get_endpoint("/users")
response = requests.get(
endpoint, headers=self._headers, auth=self._auth, timeout=self._timeout
)
if response.status_code == 200:
return response.json()
self._handle_error_response(response)
def get_user_by_id(self, user_id: int) -> dict:
"""
Fetch a user by its ID.
Args:
user_id (int): The user ID.
Returns:
A dictionary representing the user.
Raises:
ClientError: If the request fails.
"""
return self._get_user(user_id)
def get_user_by_username(self, username: str) -> dict:
"""
Fetch a user by its username.
Args:
username (str): The username.
Returns:
A dictionary representing the user.
Raises:
ClientError: If the request fails.
"""
return self._get_user(username)
def _get_user(self, user_id_or_username: Union[str, int]) -> dict:
endpoint = self._get_endpoint(f"/users/{user_id_or_username}")
response = requests.get(
endpoint, headers=self._headers, auth=self._auth, timeout=self._timeout
)
if response.status_code == 200:
return response.json()
self._handle_error_response(response)
def create_user(self, username: str, password: str, is_admin: bool = False) -> dict:
"""
Create a new user.
Args:
username (str): The username.
password (str): The password.
is_admin (bool): Whether the user should be an administrator.
Returns:
A dictionary representing the created user.
Raises:
ClientError: If the request fails.
"""
endpoint = self._get_endpoint("/users")
data = {"username": username, "password": password, "is_admin": is_admin}
response = requests.post(
endpoint,
headers=self._headers,
auth=self._auth,
data=json.dumps(data),
timeout=self._timeout,
)
if response.status_code == 201:
return response.json()
self._handle_error_response(response)
def update_user(self, user_id: int, **kwargs) -> dict:
"""
Update a user.
Args:
user_id (int): The user ID.
Returns:
A dictionary representing the updated user.
Raises: