Add invite fetching support (#95)
This commit is contained in:
parent
9c10ab0f70
commit
87d67eb63b
@ -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."""
|
||||
|
@ -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
31
tests/test_invite.py
Normal 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)
|
Loading…
x
Reference in New Issue
Block a user