Browse Source

Parse gateway URL as an actual URL using yarl

Discord has changed the URL format to make it infeasible to edit it
using basic string interpolation.
pull/10109/head
Rapptz 3 years ago
committed by dolfies
parent
commit
efab542aba
  1. 15
      discord/gateway.py

15
discord/gateway.py

@ -35,6 +35,7 @@ import zlib
from typing import Any, Callable, Coroutine, Dict, List, TYPE_CHECKING, NamedTuple, Optional, TypeVar from typing import Any, Callable, Coroutine, Dict, List, TYPE_CHECKING, NamedTuple, Optional, TypeVar
import aiohttp import aiohttp
import yarl
from . import utils from . import utils
from .activity import BaseActivity, Spotify from .activity import BaseActivity, Spotify
@ -286,14 +287,14 @@ class DiscordWebSocket:
_initial_identify: bool _initial_identify: bool
shard_id: Optional[int] shard_id: Optional[int]
shard_count: Optional[int] shard_count: Optional[int]
gateway: str gateway: yarl.URL
_max_heartbeat_timeout: float _max_heartbeat_timeout: float
_user_agent: str _user_agent: str
_super_properties: Dict[str, Any] _super_properties: Dict[str, Any]
_zlib_enabled: bool _zlib_enabled: bool
# fmt: off # fmt: off
DEFAULT_GATEWAY = 'wss://gateway.discord.gg/' DEFAULT_GATEWAY = yarl.URL('wss://gateway.discord.gg/')
DISPATCH = 0 DISPATCH = 0
HEARTBEAT = 1 HEARTBEAT = 1
IDENTIFY = 2 IDENTIFY = 2
@ -355,7 +356,7 @@ class DiscordWebSocket:
client: Client, client: Client,
*, *,
initial: bool = False, initial: bool = False,
gateway: Optional[str] = None, gateway: Optional[yarl.URL] = None,
session: Optional[str] = None, session: Optional[str] = None,
sequence: Optional[int] = None, sequence: Optional[int] = None,
resume: bool = False, resume: bool = False,
@ -372,11 +373,11 @@ class DiscordWebSocket:
gateway = gateway or cls.DEFAULT_GATEWAY gateway = gateway or cls.DEFAULT_GATEWAY
if zlib: if zlib:
url = f'{gateway}?v={INTERNAL_API_VERSION}&encoding={encoding}&compress=zlib-stream' url = gateway.with_query(v=INTERNAL_API_VERSION, encoding=encoding, compress='zlib-stream')
else: else:
url = f'{gateway}?v={INTERNAL_API_VERSION}&encoding={encoding}' url = gateway.with_query(v=INTERNAL_API_VERSION, encoding=encoding)
socket = await client.http.ws_connect(url) socket = await client.http.ws_connect(str(url))
ws = cls(socket, loop=client.loop) ws = cls(socket, loop=client.loop)
# Dynamically add attributes needed # Dynamically add attributes needed
@ -577,7 +578,7 @@ class DiscordWebSocket:
self._trace = trace = data.get('_trace', []) self._trace = trace = data.get('_trace', [])
self.sequence = msg['s'] self.sequence = msg['s']
self.session_id = data['session_id'] self.session_id = data['session_id']
self.gateway = data['resume_gateway_url'] self.gateway = yarl.URL(data['resume_gateway_url'])
_log.info('Connected to Gateway: %s (Session ID: %s).', ', '.join(trace), self.session_id) _log.info('Connected to Gateway: %s (Session ID: %s).', ', '.join(trace), self.session_id)
elif event == 'RESUMED': elif event == 'RESUMED':

Loading…
Cancel
Save