Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Do not attempt to read response body if the HTTP response code is 204. #200

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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:`198` 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