summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorGravatar Chris Lovering <[email protected]>2023-12-14 18:16:40 +0000
committerGravatar Chris Lovering <[email protected]>2023-12-14 18:16:40 +0000
commit8877fa154439f0ef44366d5808769a50f0c2915f (patch)
treed1b741c5fdaa79a2494f187c973b48881790a8fb
parentChangelog 10.5.0 (diff)
Do not attempt to read response body if the HTTP response code is 204.v10.5.1
-rw-r--r--docs/changelog.rst3
-rw-r--r--pydis_core/site_api.py34
-rw-r--r--pyproject.toml2
3 files changed, 15 insertions, 24 deletions
diff --git a/docs/changelog.rst b/docs/changelog.rst
index 2bf3d8f2..e59cd890 100644
--- a/docs/changelog.rst
+++ b/docs/changelog.rst
@@ -4,6 +4,9 @@
Changelog
=========
+- :release:`10.5.1 <14th December 2023>`
+- :bug:`200` Do not attempt to read response body if the HTTP response code is 204. Previously only :obj:`pydis_core.site_api.APIClient.delete` did this.
+
- :release:`10.5.0 <10th December 2023>`
- :support:`197` Mark dependencies using tilde version specifiers. This is to allow user of pydis core to use newer versions of these libraries without us having to cut a new release.
diff --git a/pydis_core/site_api.py b/pydis_core/site_api.py
index 80eeff2b..69e0ceba 100644
--- a/pydis_core/site_api.py
+++ b/pydis_core/site_api.py
@@ -96,7 +96,7 @@ class APIClient:
response_text = await response.text()
raise ResponseCodeError(response=response, response_text=response_text)
- async def request(self, method: str, endpoint: str, *, raise_for_status: bool = True, **kwargs) -> dict:
+ async def request(self, method: str, endpoint: str, *, raise_for_status: bool = True, **kwargs) -> dict | None:
"""
Send an HTTP request to the site API and return the JSON response.
@@ -107,50 +107,38 @@ class APIClient:
**kwargs: Any extra keyword arguments to pass to :func:`aiohttp.request`.
Returns:
- The JSON response the API returns.
+ The JSON response the API returns, or :obj:`None` if the response code is 204.
Raises:
:exc:`ResponseCodeError`:
If the response is not OK and ``raise_for_status`` is True.
"""
async with self.session.request(method.upper(), self._url_for(endpoint), **kwargs) as resp:
+ if resp.status == 204:
+ return None
+
await self.maybe_raise_for_status(resp, raise_for_status)
return await resp.json()
- async def get(self, endpoint: str, *, raise_for_status: bool = True, **kwargs) -> dict:
+ async def get(self, endpoint: str, *, raise_for_status: bool = True, **kwargs) -> dict | None:
"""Equivalent to :meth:`APIClient.request` with GET passed as the method."""
return await self.request("GET", endpoint, raise_for_status=raise_for_status, **kwargs)
- async def patch(self, endpoint: str, *, raise_for_status: bool = True, **kwargs) -> dict:
+ async def patch(self, endpoint: str, *, raise_for_status: bool = True, **kwargs) -> dict | None:
"""Equivalent to :meth:`APIClient.request` with PATCH passed as the method."""
return await self.request("PATCH", endpoint, raise_for_status=raise_for_status, **kwargs)
- async def post(self, endpoint: str, *, raise_for_status: bool = True, **kwargs) -> dict:
+ async def post(self, endpoint: str, *, raise_for_status: bool = True, **kwargs) -> dict | None:
"""Equivalent to :meth:`APIClient.request` with POST passed as the method."""
return await self.request("POST", endpoint, raise_for_status=raise_for_status, **kwargs)
- async def put(self, endpoint: str, *, raise_for_status: bool = True, **kwargs) -> dict:
+ async def put(self, endpoint: str, *, raise_for_status: bool = True, **kwargs) -> dict | None:
"""Equivalent to :meth:`APIClient.request` with PUT passed as the method."""
return await self.request("PUT", endpoint, raise_for_status=raise_for_status, **kwargs)
async def delete(self, endpoint: str, *, raise_for_status: bool = True, **kwargs) -> dict | None:
- """
- Send a DELETE request to the site API and return the JSON response.
-
- Args:
- endpoint: The endpoint to send the request to.
- raise_for_status: Whether or not to raise an exception if the response is not OK.
- **kwargs: Any extra keyword arguments to pass to :func:`aiohttp.request`.
-
- Returns:
- The JSON response the API returns, or None if the response is 204 No Content.
- """
- async with self.session.delete(self._url_for(endpoint), **kwargs) as resp:
- if resp.status == 204:
- return None
-
- await self.maybe_raise_for_status(resp, raise_for_status)
- return await resp.json()
+ """Equivalent to :meth:`APIClient.request` with DELETE passed as the method."""
+ return await self.request("DELETE", endpoint, raise_for_status=raise_for_status, **kwargs)
__all__ = ["APIClient", "ResponseCodeError"]
diff --git a/pyproject.toml b/pyproject.toml
index b388f8d5..3aaa2747 100644
--- a/pyproject.toml
+++ b/pyproject.toml
@@ -1,6 +1,6 @@
[tool.poetry]
name = "pydis_core"
-version = "10.5.0"
+version = "10.5.1"
description = "PyDis core provides core functionality and utility to the bots of the Python Discord community."
authors = ["Python Discord <[email protected]>"]
license = "MIT"