Skip to content

Commit

Permalink
Do not attempt to read response body if the HTTP response code is 204.
Browse files Browse the repository at this point in the history
  • Loading branch information
ChrisLovering committed Dec 14, 2023
1 parent 3ed1d4b commit 8877fa1
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 24 deletions.
3 changes: 3 additions & 0 deletions docs/changelog.rst
Original file line number Diff line number Diff line change
Expand Up @@ -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.

Expand Down
34 changes: 11 additions & 23 deletions pydis_core/site_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,7 @@ async def maybe_raise_for_status(response: aiohttp.ClientResponse, should_raise:
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.
Expand All @@ -107,50 +107,38 @@ async def request(self, method: str, endpoint: str, *, raise_for_status: bool =
**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"]
2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
@@ -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 <info@pythondiscord.com>"]
license = "MIT"
Expand Down

0 comments on commit 8877fa1

Please sign in to comment.