Skip to content

Commit

Permalink
Add timeout to requests. (#49)
Browse files Browse the repository at this point in the history
  • Loading branch information
vremes authored Mar 13, 2021
1 parent 9f247b7 commit fb75925
Showing 1 changed file with 7 additions and 5 deletions.
12 changes: 7 additions & 5 deletions discord_webhook/webhook.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ def __init__(self, url, **kwargs):
:keyword embeds: list of embedded rich content
:keyword allowed_mentions: allowed mentions for the message
:keyword proxies: dict of proxies
:keyword timeout: (optional) amount of seconds to wait for a response from Discord
"""
self.url = url
self.content = kwargs.get("content")
Expand All @@ -37,6 +38,7 @@ def __init__(self, url, **kwargs):
self.embeds = kwargs.get("embeds", [])
self.proxies = kwargs.get("proxies")
self.allowed_mentions = kwargs.get("allowed_mentions")
self.timeout = kwargs.get("timeout")

def add_file(self, file, filename):
"""
Expand Down Expand Up @@ -139,10 +141,10 @@ def execute(self, remove_embeds=False, remove_files=False):
responses = []
for i, url in enumerate(webhook_urls):
if bool(self.files) is False:
response = requests.post(url, json=self.json, proxies=self.proxies, params={'wait': True})
response = requests.post(url, json=self.json, proxies=self.proxies, params={'wait': True}, timeout=self.timeout)
else:
self.files["payload_json"] = (None, json.dumps(self.json))
response = requests.post(url, files=self.files, proxies=self.proxies)
response = requests.post(url, files=self.files, proxies=self.proxies, timeout=self.timeout)
if response.status_code in [200, 204]:
logger.debug(
"[{index}/{length}] Webhook executed".format(
Expand Down Expand Up @@ -178,10 +180,10 @@ def edit(self, sent_webhook):
url = webhook.url.split('?')[0] # removes any query params
previous_sent_message_id = json.loads(webhook.content.decode('utf-8'))['id']
if bool(self.files) is False:
response = requests.patch(url+'/messages/'+str(previous_sent_message_id), json=self.json, proxies=self.proxies, params={'wait': True})
response = requests.patch(url+'/messages/'+str(previous_sent_message_id), json=self.json, proxies=self.proxies, params={'wait': True}, timeout=self.timeout)
else:
self.files["payload_json"] = (None, json.dumps(self.json))
response = requests.patch(url+'/messages/'+str(previous_sent_message_id), files=self.files, proxies=self.proxies)
response = requests.patch(url+'/messages/'+str(previous_sent_message_id), files=self.files, proxies=self.proxies, timeout=self.timeout)
if response.status_code in [200, 204]:
logger.debug(
"[{index}/{length}] Webhook edited".format(
Expand Down Expand Up @@ -213,7 +215,7 @@ def delete(self, sent_webhook):
for i, webhook in enumerate(sent_webhook):
url = webhook.url.split('?')[0] # removes any query params
previous_sent_message_id = json.loads(webhook.content.decode('utf-8'))['id']
response = requests.delete(url+'/messages/'+str(previous_sent_message_id), proxies=self.proxies)
response = requests.delete(url+'/messages/'+str(previous_sent_message_id), proxies=self.proxies, timeout=self.timeout)
if response.status_code in [200, 204]:
logger.debug(
"[{index}/{length}] Webhook deleted".format(
Expand Down

0 comments on commit fb75925

Please sign in to comment.