Add invite fetching support (#95)

This commit is contained in:
Slipstream 2025-06-15 18:50:06 -06:00 committed by GitHub
parent 9c10ab0f70
commit 87d67eb63b
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
3 changed files with 54 additions and 5 deletions

View File

@ -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."""

View File

@ -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]

31
tests/test_invite.py Normal file
View File

@ -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)