Skip to content

Commit 1254d13

Browse files
committed
Factor out argument checking into a helper.
1 parent 91c8968 commit 1254d13

File tree

3 files changed

+41
-31
lines changed

3 files changed

+41
-31
lines changed

src/akismet/_async_client.py

Lines changed: 11 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -313,19 +313,17 @@ async def _post_request(
313313
optional argument names.
314314
315315
"""
316-
unknown_args = [k for k in kwargs if k not in _common._OPTIONAL_KEYS]
317-
if unknown_args:
318-
raise _exceptions.UnknownArgumentError(
319-
f"Received unknown argument(s) for Akismet operation {endpoint}: "
320-
f"{', '.join(unknown_args)}"
321-
)
322-
data = {
323-
"api_key": self._config.key,
324-
"blog": self._config.url,
325-
"user_ip": user_ip,
326-
**kwargs,
327-
}
328-
return await self._request("POST", version, endpoint, data)
316+
return await self._request(
317+
"POST",
318+
version,
319+
endpoint,
320+
data={
321+
"api_key": self._config.key,
322+
"blog": self._config.url,
323+
"user_ip": user_ip,
324+
**_common._check_post_kwargs(kwargs, endpoint),
325+
},
326+
)
329327

330328
async def _submit(
331329
self, endpoint: str, user_ip: str, **kwargs: Unpack[_common.AkismetArguments]

src/akismet/_common.py

Lines changed: 19 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414

1515
import httpx
1616

17-
from ._exceptions import APIKeyError, ConfigurationError, ProtocolError
17+
from . import _exceptions
1818

1919
# Private constants.
2020
# -------------------------------------------------------------------------------
@@ -131,7 +131,7 @@ def _configuration_error(config: Config) -> NoReturn:
131131
Raise an appropriate exception for invalid configuration.
132132
133133
"""
134-
raise APIKeyError(
134+
raise _exceptions.APIKeyError(
135135
textwrap.dedent(
136136
f"""
137137
Akismet API key and/or blog URL were invalid.
@@ -164,7 +164,7 @@ def _protocol_error(operation: str, response: httpx.Response) -> NoReturn:
164164
Raise an appropriate exception for unexpected API responses.
165165
166166
"""
167-
raise ProtocolError(
167+
raise _exceptions.ProtocolError(
168168
textwrap.dedent(
169169
f"""
170170
Received unexpected or non-standard response from Akismet API.
@@ -190,7 +190,7 @@ def _try_discover_config() -> Config:
190190
url = os.getenv(_URL_ENV_VAR, None)
191191

192192
if key is None or url is None:
193-
raise ConfigurationError(
193+
raise _exceptions.ConfigurationError(
194194
textwrap.dedent(
195195
f"""
196196
Could not find full Akismet configuration.
@@ -202,7 +202,7 @@ def _try_discover_config() -> Config:
202202
)
203203

204204
if not url.startswith(("http://", "https://")):
205-
raise ConfigurationError(
205+
raise _exceptions.ConfigurationError(
206206
textwrap.dedent(
207207
f"""
208208
Invalid Akismet site URL specified: {url}
@@ -227,3 +227,17 @@ def _handle_check_response(response: httpx.Response) -> CheckResponse:
227227
if response.text == "false":
228228
return CheckResponse.HAM
229229
_protocol_error(_COMMENT_CHECK, response)
230+
231+
232+
def _check_post_kwargs(kwargs: dict, endpoint: str) -> AkismetArguments:
233+
"""
234+
Verify that the provided set of keyword arguments is valid for an Akismet POST
235+
request, returning them if they are or raising UnknownArgumentError if they aren't.
236+
237+
"""
238+
if unknown_args := [k for k in kwargs if k not in _OPTIONAL_KEYS]:
239+
raise _exceptions.UnknownArgumentError(
240+
f"Received unknown argument(s) for Akismet operation {endpoint}: "
241+
f"{', '.join(unknown_args)}"
242+
)
243+
return kwargs

src/akismet/_sync_client.py

Lines changed: 11 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -312,19 +312,17 @@ def _post_request(
312312
optional argument names.
313313
314314
"""
315-
unknown_args = [k for k in kwargs if k not in _common._OPTIONAL_KEYS]
316-
if unknown_args:
317-
raise _exceptions.UnknownArgumentError(
318-
f"Received unknown argument(s) for Akismet operation {endpoint}: "
319-
f"{', '.join(unknown_args)}"
320-
)
321-
data = {
322-
"api_key": self._config.key,
323-
"blog": self._config.url,
324-
"user_ip": user_ip,
325-
**kwargs,
326-
}
327-
return self._request("POST", version, endpoint, data)
315+
return self._request(
316+
"POST",
317+
version,
318+
endpoint,
319+
data={
320+
"api_key": self._config.key,
321+
"blog": self._config.url,
322+
"user_ip": user_ip,
323+
**_common._check_post_kwargs(kwargs, endpoint),
324+
},
325+
)
328326

329327
def _submit(
330328
self, endpoint: str, user_ip: str, **kwargs: Unpack[_common.AkismetArguments]

0 commit comments

Comments
 (0)