from typing import TYPE_CHECKING, Any, List, Optional, Tuple
from .channel import ChannelRequest
from .emoji import EmojiRequest
from .guild import GuildRequest
from .interaction import InteractionRequest
from .invite import InviteRequest
from .member import MemberRequest
from .message import MessageRequest
from .reaction import ReactionRequest
from .request import _Request
from .route import Route
from .scheduledEvent import ScheduledEventRequest
from .sticker import StickerRequest
from .thread import ThreadRequest
from .user import UserRequest
from .webhook import WebhookRequest
if TYPE_CHECKING:
from ...api.cache import Cache
__all__ = ("HTTPClient",)
[docs]class HTTPClient(
ChannelRequest,
EmojiRequest,
GuildRequest,
InteractionRequest,
InviteRequest,
MemberRequest,
MessageRequest,
ReactionRequest,
ScheduledEventRequest,
StickerRequest,
ThreadRequest,
UserRequest,
WebhookRequest,
):
"""
The user-facing client of the Web API for individual endpoints.
:ivar str token: The token of the application.
:ivar Request _req: The requesting interface for endpoints.
:ivar Cache cache: The referenced cache.
"""
__slots__ = (
"token",
"_req",
"cache",
)
token: str
_req: _Request
cache: "Cache"
def __init__(self, token: str, cache: "Cache"): # noqa skip the no super imports
self.token = token
self._req = _Request(self.token)
self.cache = cache
# An ideology is that this client does every single HTTP call, which reduces multiple ClientSessions in theory
# because of how they are constructed/closed. This includes Gateway
[docs] async def get_gateway(self) -> str:
"""This calls the Gateway endpoint and returns a v9 gateway link with JSON encoding."""
url: Any = await self._req.request(
Route("GET", "/gateway")
) # typehinting Any because pycharm yells
try:
_url = f'{url["url"]}?v=10&encoding=json&compress=zlib-stream'
except TypeError: # seen a few times
_url = "wss://gateway.discord.gg?v=10&encoding=json&compress=zlib-stream"
return _url
[docs] async def get_bot_gateway(self) -> Tuple[int, str]:
"""
This calls the BOT Gateway endpoint.
:return: A tuple denoting (shard, gateway_url), url from API v9 and JSON encoding
"""
data: Any = await self._req.request(Route("GET", "/gateway/bot"))
try:
_url = f'{data["url"]}?v=10&encoding=json&compress=zlib-stream'
except TypeError: # seen a few times
_url = "wss://gateway.discord.gg?v=10&encoding=json&compress=zlib-stream"
return data["shards"], _url
[docs] async def login(self) -> Optional[dict]:
"""
This 'logins' to the gateway, which makes it available to use any other endpoint.
"""
return await self._req.request(
Route("GET", "/users/@me")
) # Internally raises any Exception.
[docs] async def logout(self) -> None:
"""This 'log outs' the session."""
await self._req.request(Route("POST", "/auth/logout"))
@property
def req(self) -> _Request:
return self._req
# ---- Oauth2 endpoint
# ---- Role connection metadata endpoints