Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions discord/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -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 *
Expand Down
22 changes: 13 additions & 9 deletions discord/http.py
Original file line number Diff line number Diff line change
Expand Up @@ -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")
Expand All @@ -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
Expand All @@ -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
Expand Down Expand Up @@ -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:
Expand All @@ -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))