Browse Source

Use json_or_text helper for parsing webhook responses

pull/10109/head
I. Ahmad 3 years ago
committed by dolfies
parent
commit
0bdc4d7281
  1. 5
      discord/webhook/async_.py
  2. 3
      discord/webhook/sync.py

5
discord/webhook/async_.py

@ -26,7 +26,6 @@ from __future__ import annotations
import logging import logging
import asyncio import asyncio
import json
import re import re
from urllib.parse import quote as urlquote from urllib.parse import quote as urlquote
@ -181,9 +180,7 @@ class AsyncWebhookAdapter:
url, url,
response.status, response.status,
) )
data = (await response.text(encoding='utf-8')) or None data = await json_or_text(response)
if data and response.headers['Content-Type'] == 'application/json':
data = json.loads(data)
remaining = response.headers.get('X-Ratelimit-Remaining') remaining = response.headers.get('X-Ratelimit-Remaining')
if remaining == '0' and response.status != 429: if remaining == '0' and response.status != 429:

3
discord/webhook/sync.py

@ -178,8 +178,11 @@ class WebhookAdapter:
response.status = response.status_code # type: ignore response.status = response.status_code # type: ignore
data = response.text or None data = response.text or None
try:
if data and response.headers['Content-Type'] == 'application/json': if data and response.headers['Content-Type'] == 'application/json':
data = json.loads(data) data = json.loads(data)
except KeyError:
pass
remaining = response.headers.get('X-Ratelimit-Remaining') remaining = response.headers.get('X-Ratelimit-Remaining')
if remaining == '0' and response.status_code != 429: if remaining == '0' and response.status_code != 429:

Loading…
Cancel
Save