From 4a7220161774378e83f35ce2c115a13496304656 Mon Sep 17 00:00:00 2001 From: Rapptz Date: Sun, 22 Aug 2021 02:35:58 -0400 Subject: [PATCH] Make json conversion functions private --- discord/gateway.py | 12 ++++++------ discord/http.py | 8 ++++---- discord/utils.py | 8 ++++---- discord/webhook/async_.py | 4 ++-- discord/webhook/sync.py | 2 +- 5 files changed, 17 insertions(+), 17 deletions(-) diff --git a/discord/gateway.py b/discord/gateway.py index 8110361b1..871376952 100644 --- a/discord/gateway.py +++ b/discord/gateway.py @@ -430,7 +430,7 @@ class DiscordWebSocket: msg = self._zlib.decompress(self._buffer) msg = msg.decode('utf-8') self._buffer = bytearray() - msg = utils.from_json(msg) + msg = utils._from_json(msg) _log.debug('For Shard ID %s: WebSocket Event: %s', self.shard_id, msg) event = msg.get('t') @@ -595,7 +595,7 @@ class DiscordWebSocket: async def send_as_json(self, data): try: - await self.send(utils.to_json(data)) + await self.send(utils._to_json(data)) except RuntimeError as exc: if not self._can_handle_close(): raise ConnectionClosed(self.socket, shard_id=self.shard_id) from exc @@ -603,7 +603,7 @@ class DiscordWebSocket: async def send_heartbeat(self, data): # This bypasses the rate limit handling code since it has a higher priority try: - await self.socket.send_str(utils.to_json(data)) + await self.socket.send_str(utils._to_json(data)) except RuntimeError as exc: if not self._can_handle_close(): raise ConnectionClosed(self.socket, shard_id=self.shard_id) from exc @@ -629,7 +629,7 @@ class DiscordWebSocket: } } - sent = utils.to_json(payload) + sent = utils._to_json(payload) _log.debug('Sending "%s" to change status', sent) await self.send(sent) @@ -735,7 +735,7 @@ class DiscordVoiceWebSocket: async def send_as_json(self, data): _log.debug('Sending voice websocket frame: %s.', data) - await self.ws.send_str(utils.to_json(data)) + await self.ws.send_str(utils._to_json(data)) send_heartbeat = send_as_json @@ -895,7 +895,7 @@ class DiscordVoiceWebSocket: # This exception is handled up the chain msg = await asyncio.wait_for(self.ws.receive(), timeout=30.0) if msg.type is aiohttp.WSMsgType.TEXT: - await self.received_message(utils.from_json(msg.data)) + await self.received_message(utils._from_json(msg.data)) elif msg.type is aiohttp.WSMsgType.ERROR: _log.debug('Received %s', msg) raise ConnectionClosed(self.ws, shard_id=None) from msg.data diff --git a/discord/http.py b/discord/http.py index 7cc82880b..504852945 100644 --- a/discord/http.py +++ b/discord/http.py @@ -99,7 +99,7 @@ async def json_or_text(response: aiohttp.ClientResponse) -> Union[Dict[str, Any] text = await response.text(encoding='utf-8') try: if response.headers['content-type'] == 'application/json': - return utils.from_json(text) + return utils._from_json(text) except KeyError: # Thanks Cloudflare pass @@ -231,7 +231,7 @@ class HTTPClient: # some checking if it's a JSON request if 'json' in kwargs: headers['Content-Type'] = 'application/json' - kwargs['data'] = utils.to_json(kwargs.pop('json')) + kwargs['data'] = utils._to_json(kwargs.pop('json')) try: reason = kwargs.pop('reason') @@ -493,7 +493,7 @@ class HTTPClient: if stickers: payload['sticker_ids'] = stickers - form.append({'name': 'payload_json', 'value': utils.to_json(payload)}) + form.append({'name': 'payload_json', 'value': utils._to_json(payload)}) if len(files) == 1: file = files[0] form.append( @@ -1657,7 +1657,7 @@ class HTTPClient: form: List[Dict[str, Any]] = [ { 'name': 'payload_json', - 'value': utils.to_json(payload), + 'value': utils._to_json(payload), } ] diff --git a/discord/utils.py b/discord/utils.py index 920222a94..98108a0bb 100644 --- a/discord/utils.py +++ b/discord/utils.py @@ -479,17 +479,17 @@ def _bytes_to_base64_data(data: bytes) -> str: if HAS_ORJSON: - def to_json(obj: Any) -> str: # type: ignore + def _to_json(obj: Any) -> str: # type: ignore return orjson.dumps(obj).decode('utf-8') - from_json = orjson.loads # type: ignore + _from_json = orjson.loads # type: ignore else: - def to_json(obj: Any) -> str: + def _to_json(obj: Any) -> str: return json.dumps(obj, separators=(',', ':'), ensure_ascii=True) - from_json = json.loads + _from_json = json.loads def _parse_ratelimit_header(request: Any, *, use_clock: bool = False) -> float: diff --git a/discord/webhook/async_.py b/discord/webhook/async_.py index f78ddbc35..6ee4ce5c4 100644 --- a/discord/webhook/async_.py +++ b/discord/webhook/async_.py @@ -116,7 +116,7 @@ class AsyncWebhookAdapter: if payload is not None: headers['Content-Type'] = 'application/json' - to_send = utils.to_json(payload) + to_send = utils._to_json(payload) if auth_token is not None: headers['Authorization'] = f'Bot {auth_token}' @@ -481,7 +481,7 @@ def handle_message_parameters( files = [file] if files: - multipart.append({'name': 'payload_json', 'value': utils.to_json(payload)}) + multipart.append({'name': 'payload_json', 'value': utils._to_json(payload)}) payload = None if len(files) == 1: file = files[0] diff --git a/discord/webhook/sync.py b/discord/webhook/sync.py index a7efc7d0e..a3b2f156f 100644 --- a/discord/webhook/sync.py +++ b/discord/webhook/sync.py @@ -117,7 +117,7 @@ class WebhookAdapter: if payload is not None: headers['Content-Type'] = 'application/json' - to_send = utils.to_json(payload) + to_send = utils._to_json(payload) if auth_token is not None: headers['Authorization'] = f'Bot {auth_token}'