9
9
from typing import TYPE_CHECKING , Literal , Optional , Type , Union
10
10
11
11
import httpx
12
-
13
- from . import _common , _exceptions
12
+ from typing_extensions import Self , Unpack
13
+
14
+ from . import _exceptions
15
+ from ._common import (
16
+ _API_URL ,
17
+ _API_V11 ,
18
+ _API_V12 ,
19
+ _COMMENT_CHECK ,
20
+ _KEY_SITES ,
21
+ _OPTIONAL_KEYS ,
22
+ _REQUEST_METHODS ,
23
+ _SUBMISSION_RESPONSE ,
24
+ _SUBMIT_HAM ,
25
+ _SUBMIT_SPAM ,
26
+ _USAGE_LIMIT ,
27
+ _VERIFY_KEY ,
28
+ AkismetArguments ,
29
+ CheckResponse ,
30
+ _configuration_error ,
31
+ _get_async_http_client ,
32
+ _protocol_error ,
33
+ _try_discover_config ,
34
+ )
14
35
15
36
if TYPE_CHECKING : # pragma: no cover
16
37
import akismet
@@ -139,15 +160,15 @@ def __init__(
139
160
You will almost always want to use :meth:`validated_client` instead.
140
161
141
162
"""
142
- self ._config = config if config is not None else _common . _try_discover_config ()
143
- self ._http_client = http_client or _common . _get_async_http_client ()
163
+ self ._config = config if config is not None else _try_discover_config ()
164
+ self ._http_client = http_client or _get_async_http_client ()
144
165
145
166
@classmethod
146
167
async def validated_client (
147
168
cls ,
148
169
config : Optional ["akismet.Config" ] = None ,
149
170
http_client : Optional [httpx .AsyncClient ] = None ,
150
- ) -> "AsyncClient" :
171
+ ) -> Self :
151
172
"""
152
173
Constructor of :class:`AsyncClient`.
153
174
@@ -186,7 +207,7 @@ async def validated_client(
186
207
# alternative constructor in order to achieve API consistency.
187
208
instance = cls (config = config , http_client = http_client )
188
209
if not await instance .verify_key ():
189
- _common . _configuration_error (instance ._config )
210
+ _configuration_error (instance ._config )
190
211
return instance
191
212
192
213
# Async context-manager protocol.
@@ -198,7 +219,7 @@ async def __aenter__(self) -> "AsyncClient":
198
219
199
220
"""
200
221
if not await self .verify_key ():
201
- _common . _configuration_error (self ._config )
222
+ _configuration_error (self ._config )
202
223
return self
203
224
204
225
async def __aexit__ (
@@ -215,7 +236,7 @@ async def __aexit__(
215
236
216
237
async def _request (
217
238
self ,
218
- method : _common . _REQUEST_METHODS ,
239
+ method : _REQUEST_METHODS ,
219
240
version : str ,
220
241
endpoint : str ,
221
242
data : dict ,
@@ -244,7 +265,7 @@ async def _request(
244
265
request_kwarg = "data" if method == "POST" else "params"
245
266
try :
246
267
response = await handler (
247
- f"{ _common . _API_URL } /{ version } /{ endpoint } " , ** {request_kwarg : data }
268
+ f"{ _API_URL } /{ version } /{ endpoint } " , ** {request_kwarg : data }
248
269
)
249
270
response .raise_for_status ()
250
271
except httpx .HTTPStatusError as exc :
@@ -260,7 +281,7 @@ async def _request(
260
281
# Since it's possible to construct a client without performing up-front API key
261
282
# validation, we have to watch out here for the possibility that we're making
262
283
# requests with an invalid key, and raise the appropriate exception.
263
- if endpoint != _common . _VERIFY_KEY and response .text == "invalid" :
284
+ if endpoint != _VERIFY_KEY and response .text == "invalid" :
264
285
raise _exceptions .APIKeyError (
265
286
"Akismet API key and/or site URL are invalid."
266
287
)
@@ -309,7 +330,7 @@ async def _post_request(
309
330
optional argument names.
310
331
311
332
"""
312
- unknown_args = [k for k in kwargs if k not in _common . _OPTIONAL_KEYS ]
333
+ unknown_args = [k for k in kwargs if k not in _OPTIONAL_KEYS ]
313
334
if unknown_args :
314
335
raise _exceptions .UnknownArgumentError (
315
336
f"Received unknown argument(s) for Akismet operation { endpoint } : "
@@ -337,17 +358,17 @@ async def _submit(self, endpoint: str, user_ip: str, **kwargs: str) -> bool:
337
358
338
359
"""
339
360
response = await self ._post_request (
340
- _common . _API_V11 , endpoint , user_ip = user_ip , ** kwargs
361
+ _API_V11 , endpoint , user_ip = user_ip , ** kwargs
341
362
)
342
- if response .text == _common . _SUBMISSION_RESPONSE :
363
+ if response .text == _SUBMISSION_RESPONSE :
343
364
return True
344
- _common . _protocol_error (endpoint , response )
365
+ _protocol_error (endpoint , response )
345
366
346
367
# Public methods corresponding to the methods of the Akismet API.
347
368
# ----------------------------------------------------------------------------
348
369
349
370
async def comment_check (
350
- self , user_ip : str , ** kwargs : str
371
+ self , user_ip : str , ** kwargs : Unpack [ AkismetArguments ]
351
372
) -> "akismet.CheckResponse" :
352
373
"""
353
374
Check a piece of user-submitted content to determine whether it is spam.
@@ -392,17 +413,19 @@ async def comment_check(
392
413
393
414
"""
394
415
response = await self ._post_request (
395
- _common . _API_V11 , _common . _COMMENT_CHECK , user_ip = user_ip , ** kwargs
416
+ _API_V11 , _COMMENT_CHECK , user_ip = user_ip , ** kwargs
396
417
)
397
418
if response .text == "true" :
398
419
if response .headers .get ("X-akismet-pro-tip" , "" ) == "discard" :
399
- return _common . CheckResponse .DISCARD
400
- return _common . CheckResponse .SPAM
420
+ return CheckResponse .DISCARD
421
+ return CheckResponse .SPAM
401
422
if response .text == "false" :
402
- return _common . CheckResponse .HAM
403
- _common . _protocol_error (_common . _COMMENT_CHECK , response )
423
+ return CheckResponse .HAM
424
+ _protocol_error (_COMMENT_CHECK , response )
404
425
405
- async def submit_ham (self , user_ip : str , ** kwargs : str ) -> bool :
426
+ async def submit_ham (
427
+ self , user_ip : str , ** kwargs : Unpack [AkismetArguments ]
428
+ ) -> bool :
406
429
"""
407
430
Inform Akismet that a piece of user-submitted comment is not spam.
408
431
@@ -440,9 +463,11 @@ async def submit_ham(self, user_ip: str, **kwargs: str) -> bool:
440
463
received from the Akismet API.
441
464
442
465
"""
443
- return await self ._submit (_common . _SUBMIT_HAM , user_ip , ** kwargs )
466
+ return await self ._submit (_SUBMIT_HAM , user_ip , ** kwargs )
444
467
445
- async def submit_spam (self , user_ip : str , ** kwargs : str ) -> bool :
468
+ async def submit_spam (
469
+ self , user_ip : str , ** kwargs : Unpack [AkismetArguments ]
470
+ ) -> bool :
446
471
"""
447
472
Inform Akismet that a piece of user-submitted comment is spam.
448
473
@@ -480,9 +505,10 @@ async def submit_spam(self, user_ip: str, **kwargs: str) -> bool:
480
505
received from the Akismet API.
481
506
482
507
"""
483
- return await self ._submit (_common . _SUBMIT_SPAM , user_ip , ** kwargs )
508
+ return await self ._submit (_SUBMIT_SPAM , user_ip , ** kwargs )
484
509
485
- async def key_sites ( # pylint: disable=too-many-positional-arguments,too-many-arguments
510
+ async def key_sites (
511
+ # pylint: disable=too-many-positional-arguments,too-many-arguments
486
512
self ,
487
513
month : Optional [str ] = None ,
488
514
url_filter : Optional [str ] = None ,
@@ -531,7 +557,7 @@ async def key_sites( # pylint: disable=too-many-positional-arguments,too-many-a
531
557
):
532
558
if value is not None :
533
559
params [argument ] = value
534
- response = await self ._get_request (_common . _API_V12 , _common . _KEY_SITES , params )
560
+ response = await self ._get_request (_API_V12 , _KEY_SITES , params )
535
561
if result_format == "csv" :
536
562
return response .text
537
563
return response .json ()
@@ -546,7 +572,7 @@ async def usage_limit(self) -> dict:
546
572
547
573
"""
548
574
response = await self ._get_request (
549
- _common . _API_V12 , _common . _USAGE_LIMIT , params = {"api_key" : self ._config .key }
575
+ _API_V12 , _USAGE_LIMIT , params = {"api_key" : self ._config .key }
550
576
)
551
577
return response .json ()
552
578
@@ -575,10 +601,10 @@ async def verify_key(
575
601
if not all ([key , url ]):
576
602
key , url = self ._config
577
603
response = await self ._request (
578
- "POST" , _common . _API_V11 , _common . _VERIFY_KEY , {"key" : key , "blog" : url }
604
+ "POST" , _API_V11 , _VERIFY_KEY , {"key" : key , "blog" : url }
579
605
)
580
606
if response .text == "valid" :
581
607
return True
582
608
if response .text == "invalid" :
583
609
return False
584
- _common . _protocol_error (_common . _VERIFY_KEY , response )
610
+ _protocol_error (_VERIFY_KEY , response )
0 commit comments