Skip to content

Commit 8877fa1

Browse files
committed
Do not attempt to read response body if the HTTP response code is 204.
1 parent 3ed1d4b commit 8877fa1

File tree

3 files changed

+15
-24
lines changed

3 files changed

+15
-24
lines changed

docs/changelog.rst

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,9 @@
44
Changelog
55
=========
66

7+
- :release:`10.5.1 <14th December 2023>`
8+
- :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.
9+
710
- :release:`10.5.0 <10th December 2023>`
811
- :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.
912

pydis_core/site_api.py

Lines changed: 11 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -96,7 +96,7 @@ async def maybe_raise_for_status(response: aiohttp.ClientResponse, should_raise:
9696
response_text = await response.text()
9797
raise ResponseCodeError(response=response, response_text=response_text)
9898

99-
async def request(self, method: str, endpoint: str, *, raise_for_status: bool = True, **kwargs) -> dict:
99+
async def request(self, method: str, endpoint: str, *, raise_for_status: bool = True, **kwargs) -> dict | None:
100100
"""
101101
Send an HTTP request to the site API and return the JSON response.
102102
@@ -107,50 +107,38 @@ async def request(self, method: str, endpoint: str, *, raise_for_status: bool =
107107
**kwargs: Any extra keyword arguments to pass to :func:`aiohttp.request`.
108108
109109
Returns:
110-
The JSON response the API returns.
110+
The JSON response the API returns, or :obj:`None` if the response code is 204.
111111
112112
Raises:
113113
:exc:`ResponseCodeError`:
114114
If the response is not OK and ``raise_for_status`` is True.
115115
"""
116116
async with self.session.request(method.upper(), self._url_for(endpoint), **kwargs) as resp:
117+
if resp.status == 204:
118+
return None
119+
117120
await self.maybe_raise_for_status(resp, raise_for_status)
118121
return await resp.json()
119122

120-
async def get(self, endpoint: str, *, raise_for_status: bool = True, **kwargs) -> dict:
123+
async def get(self, endpoint: str, *, raise_for_status: bool = True, **kwargs) -> dict | None:
121124
"""Equivalent to :meth:`APIClient.request` with GET passed as the method."""
122125
return await self.request("GET", endpoint, raise_for_status=raise_for_status, **kwargs)
123126

124-
async def patch(self, endpoint: str, *, raise_for_status: bool = True, **kwargs) -> dict:
127+
async def patch(self, endpoint: str, *, raise_for_status: bool = True, **kwargs) -> dict | None:
125128
"""Equivalent to :meth:`APIClient.request` with PATCH passed as the method."""
126129
return await self.request("PATCH", endpoint, raise_for_status=raise_for_status, **kwargs)
127130

128-
async def post(self, endpoint: str, *, raise_for_status: bool = True, **kwargs) -> dict:
131+
async def post(self, endpoint: str, *, raise_for_status: bool = True, **kwargs) -> dict | None:
129132
"""Equivalent to :meth:`APIClient.request` with POST passed as the method."""
130133
return await self.request("POST", endpoint, raise_for_status=raise_for_status, **kwargs)
131134

132-
async def put(self, endpoint: str, *, raise_for_status: bool = True, **kwargs) -> dict:
135+
async def put(self, endpoint: str, *, raise_for_status: bool = True, **kwargs) -> dict | None:
133136
"""Equivalent to :meth:`APIClient.request` with PUT passed as the method."""
134137
return await self.request("PUT", endpoint, raise_for_status=raise_for_status, **kwargs)
135138

136139
async def delete(self, endpoint: str, *, raise_for_status: bool = True, **kwargs) -> dict | None:
137-
"""
138-
Send a DELETE request to the site API and return the JSON response.
139-
140-
Args:
141-
endpoint: The endpoint to send the request to.
142-
raise_for_status: Whether or not to raise an exception if the response is not OK.
143-
**kwargs: Any extra keyword arguments to pass to :func:`aiohttp.request`.
144-
145-
Returns:
146-
The JSON response the API returns, or None if the response is 204 No Content.
147-
"""
148-
async with self.session.delete(self._url_for(endpoint), **kwargs) as resp:
149-
if resp.status == 204:
150-
return None
151-
152-
await self.maybe_raise_for_status(resp, raise_for_status)
153-
return await resp.json()
140+
"""Equivalent to :meth:`APIClient.request` with DELETE passed as the method."""
141+
return await self.request("DELETE", endpoint, raise_for_status=raise_for_status, **kwargs)
154142

155143

156144
__all__ = ["APIClient", "ResponseCodeError"]

pyproject.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
[tool.poetry]
22
name = "pydis_core"
3-
version = "10.5.0"
3+
version = "10.5.1"
44
description = "PyDis core provides core functionality and utility to the bots of the Python Discord community."
55
authors = ["Python Discord <[email protected]>"]
66
license = "MIT"

0 commit comments

Comments
 (0)