diff --git a/discord/__init__.py b/discord/__init__.py index 17edda9011..702a2becf6 100644 --- a/discord/__init__.py +++ b/discord/__init__.py @@ -39,6 +39,7 @@ from .file import * from .flags import * from .guild import * +from .http import * from .integrations import * from .interactions import * from .invite import * diff --git a/discord/http.py b/discord/http.py index 1d137a52f6..2a01363206 100644 --- a/discord/http.py +++ b/discord/http.py @@ -98,6 +98,8 @@ MU = TypeVar("MU", bound="MaybeUnlock") Response = Coroutine[Any, Any, T] +API_VERSION: int = 10 + async def json_or_text(response: aiohttp.ClientResponse) -> Union[Dict[str, Any], str]: text = await response.text(encoding="utf-8") @@ -112,12 +114,10 @@ async def json_or_text(response: aiohttp.ClientResponse) -> Union[Dict[str, Any] class Route: - BASE: ClassVar[str] = "https://discord.com/api/v10" - def __init__(self, method: str, path: str, **parameters: Any) -> None: self.path: str = path self.method: str = method - url = self.BASE + self.path + url = self.base + self.path if parameters: url = url.format_map({k: _uriquote(v) if isinstance(v, str) else v for k, v in parameters.items()}) self.url: str = url @@ -128,6 +128,10 @@ def __init__(self, method: str, path: str, **parameters: Any) -> None: self.webhook_id: Optional[Snowflake] = parameters.get("webhook_id") self.webhook_token: Optional[str] = parameters.get("webhook_token") + @property + def base(self) -> str: + return f"https://discord.com/api/v{API_VERSION}" + @property def bucket(self) -> str: # the bucket is just method + path w/ major parameters @@ -2454,10 +2458,10 @@ async def get_gateway(self, *, encoding: str = "json", zlib: bool = True) -> str except HTTPException as exc: raise GatewayNotFound() from exc if zlib: - value = "{0}?encoding={1}&v=10&compress=zlib-stream" + value = "{0}?encoding={1}&v={2}&compress=zlib-stream" else: - value = "{0}?encoding={1}&v=10" - return value.format(data["url"], encoding) + value = "{0}?encoding={1}&v={2}" + return value.format(data["url"], encoding, API_VERSION) async def get_bot_gateway(self, *, encoding: str = "json", zlib: bool = True) -> Tuple[int, str]: try: @@ -2466,10 +2470,10 @@ async def get_bot_gateway(self, *, encoding: str = "json", zlib: bool = True) -> raise GatewayNotFound() from exc if zlib: - value = "{0}?encoding={1}&v=10&compress=zlib-stream" + value = "{0}?encoding={1}&v={2}&compress=zlib-stream" else: - value = "{0}?encoding={1}&v=10" - return data["shards"], value.format(data["url"], encoding) + value = "{0}?encoding={1}&v={2}" + return data["shards"], value.format(data["url"], encoding, API_VERSION) def get_user(self, user_id: Snowflake) -> Response[user.User]: return self.request(Route("GET", "/users/{user_id}", user_id=user_id))