8
8
9
9
from .base import BaseApi
10
10
11
+
11
12
class NoNonPaginatedVersionError (Exception ):
12
13
"""Raised when a non-paginated of a paginated-only method is called."""
13
14
15
+
14
16
def endpoint (endpoint_url ):
15
17
"""Decorator for API methods. Denotes the URL endpoint."""
18
+
16
19
@wrapt .decorator
17
20
def wrapper (method , instance , args , kwargs ):
18
21
return instance .request (endpoint_url , * method (* args , ** kwargs ))
22
+
19
23
return wrapper
20
24
25
+
21
26
def get_default_data (** kwargs ):
22
27
"""Returns a POST data dict using default values."""
23
28
limit = kwargs .get ('limit' , 20 )
@@ -29,7 +34,8 @@ def get_default_data(**kwargs):
29
34
raise TypeError ("`offset` must be int" )
30
35
if not direction in ('asc' , 'desc' ):
31
36
raise ValueError ("`direction` must be one of 'asc', 'desc'" )
32
- return { 'limit' : limit , 'offset' : offset , 'direction' : direction }
37
+ return {'limit' : limit , 'offset' : offset , 'direction' : direction }
38
+
33
39
34
40
class PaginatedMethod :
35
41
"""Object representing a method that has a POST paginated version (with
@@ -38,6 +44,7 @@ class PaginatedMethod:
38
44
if it were a method. The POST version is accessed by calling the `verbose`
39
45
attribute.
40
46
"""
47
+
41
48
def __init__ (self , method : Callable , verbose_only : bool = False ):
42
49
self ._method = method
43
50
self ._verbose_only = verbose_only
@@ -55,6 +62,7 @@ def verbose(self, *args, **kwargs):
55
62
data = get_default_data (** kwargs )
56
63
return self .__call__ (* args , data = data , __verbose = True )
57
64
65
+
58
66
class Api (BaseApi ):
59
67
"""API version 1"""
60
68
@@ -64,14 +72,18 @@ def __init__(self, *args):
64
72
super ().__init__ (* args )
65
73
self .pages_since = PaginatedMethod (self ._pages_since )
66
74
self .page_revisions = PaginatedMethod (self ._page_revisions )
67
- self .forum_threads_since = PaginatedMethod (self ._forum_threads_since ,
68
- True )
75
+ self .forum_threads_since = PaginatedMethod (
76
+ self ._forum_threads_since , True
77
+ )
69
78
self .thread_posts = PaginatedMethod (self ._thread_posts )
70
- self .thread_posts_since = PaginatedMethod (self ._thread_posts_since ,
71
- True )
79
+ self .thread_posts_since = PaginatedMethod (
80
+ self ._thread_posts_since , True
81
+ )
72
82
self .wikidotuser_pages = PaginatedMethod (self ._wikidotuser_pages )
73
83
self .wikidotuser_posts = PaginatedMethod (self ._wikidotuser_posts )
74
- self .wikidotuser_revisions = PaginatedMethod (self ._wikidotuser_revisions )
84
+ self .wikidotuser_revisions = PaginatedMethod (
85
+ self ._wikidotuser_revisions
86
+ )
75
87
self .tags_pages = PaginatedMethod (self ._tags_pages , True )
76
88
77
89
def verbose (self , method : Callable , * args , ** kwargs ):
@@ -107,8 +119,9 @@ def paginated_generator():
107
119
offset : int = data ['offset' ]
108
120
direction : str = data ['direction' ]
109
121
while True :
110
- result = method .verbose (* args , limit = limit , offset = offset ,
111
- direction = direction )
122
+ result = method .verbose (
123
+ * args , limit = limit , offset = offset , direction = direction
124
+ )
112
125
yield result
113
126
if length < data ['limit' ]:
114
127
return
@@ -262,14 +275,18 @@ def tag_pages(self, tag: str):
262
275
return tag , None
263
276
264
277
@endpoint ("tag/pages" )
265
- def _tags_pages (self , tags : Union [List [int ], List [str ]], operator : str = 'and' , * , data ):
278
+ def _tags_pages (
279
+ self , tags : Union [List [int ], List [str ]], operator : str = 'and' , * , data
280
+ ):
266
281
"""
267
282
str[] `tags`: A list of tag names.
268
283
int[] `tags`: A list of SCUTTLE tag IDs.
269
284
str `operator`: 'and' or 'or'; defines how tags are combined.
270
285
"""
271
286
if isinstance (tags , str ):
272
- raise TypeError ("`tags` must be a list of at least one tag; use tag_pages() for a single tag name" )
287
+ raise TypeError (
288
+ "`tags` must be a list of at least one tag; use tag_pages() for a single tag name"
289
+ )
273
290
data ['operator' ] = operator
274
291
if all (isinstance (tag , str ) for tag in tags ):
275
292
data ['names' ] = tags
0 commit comments