From 87d67eb63b337240fd67baf9c571c9ac4b281b60 Mon Sep 17 00:00:00 2001 From: Slipstream Date: Sun, 15 Jun 2025 18:50:06 -0600 Subject: [PATCH] Add invite fetching support (#95) --- disagreement/client.py | 15 ++++++++++++++- disagreement/http.py | 13 +++++++++---- tests/test_invite.py | 31 +++++++++++++++++++++++++++++++ 3 files changed, 54 insertions(+), 5 deletions(-) create mode 100644 tests/test_invite.py diff --git a/disagreement/client.py b/disagreement/client.py index 420f9a9..b335d8a 100644 --- a/disagreement/client.py +++ b/disagreement/client.py @@ -1658,7 +1658,20 @@ class Client: if self._closed: raise DisagreementException("Client is closed.") - await self._http.delete_invite(code) + await self._http.delete_invite(code) + + async def fetch_invite(self, code: Snowflake) -> Optional["Invite"]: + """|coro| Fetch a single invite by code.""" + + if self._closed: + raise DisagreementException("Client is closed.") + + try: + data = await self._http.get_invite(code) + return self.parse_invite(data) + except DisagreementException as e: + print(f"Failed to fetch invite {code}: {e}") + return None async def fetch_invites(self, channel_id: Snowflake) -> List["Invite"]: """|coro| Fetch all invites for a channel.""" diff --git a/disagreement/http.py b/disagreement/http.py index 722ffe7..3751585 100644 --- a/disagreement/http.py +++ b/disagreement/http.py @@ -766,10 +766,15 @@ class HTTPClient: return Invite.from_dict(data) - async def delete_invite(self, code: str) -> None: - """Deletes an invite by code.""" - - await self.request("DELETE", f"/invites/{code}") + async def delete_invite(self, code: str) -> None: + """Deletes an invite by code.""" + + await self.request("DELETE", f"/invites/{code}") + + async def get_invite(self, code: "Snowflake") -> Dict[str, Any]: + """Fetches a single invite by its code.""" + + return await self.request("GET", f"/invites/{code}") async def create_webhook( self, channel_id: "Snowflake", payload: Dict[str, Any] diff --git a/tests/test_invite.py b/tests/test_invite.py new file mode 100644 index 0000000..3abe0fb --- /dev/null +++ b/tests/test_invite.py @@ -0,0 +1,31 @@ +import pytest +from types import SimpleNamespace +from unittest.mock import AsyncMock + +from disagreement.http import HTTPClient +from disagreement.client import Client +from disagreement.models import Invite + + +@pytest.mark.asyncio +async def test_http_get_invite_calls_request(): + http = HTTPClient(token="t") + http.request = AsyncMock(return_value={"code": "abc"}) + + result = await http.get_invite("abc") + + http.request.assert_called_once_with("GET", "/invites/abc") + assert result == {"code": "abc"} + + +@pytest.mark.asyncio +async def test_client_fetch_invite_returns_invite(): + http = SimpleNamespace(get_invite=AsyncMock(return_value={"code": "abc"})) + client = Client.__new__(Client) + client._http = http + client._closed = False + + invite = await client.fetch_invite("abc") + + http.get_invite.assert_awaited_once_with("abc") + assert isinstance(invite, Invite)