Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add timeout to requests calls #2

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
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
12 changes: 6 additions & 6 deletions src/Bot/nerimity/channel.py
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ def update_channel(self, server_id: int, permissions: int=None, name: str=None,

if icon == None: del data["icon"]

response = requests.post(api_endpoint, headers=headers, data=json.dumps(data))
response = requests.post(api_endpoint, headers=headers, data=json.dumps(data), timeout=60)
if response.status_code != 200:
print(f"{ConsoleShortcuts.error()} Failed to update a channel for {self}. Status code: {response.status_code}. Response Text: {response.text}")
raise requests.RequestException
Expand All @@ -66,13 +66,13 @@ def update_channel(self, server_id: int, permissions: int=None, name: str=None,
api_endpoint = f"https://nerimity.com/api/servers/{server_id}/channels/{self.id}/notice"

if content == "":
response = requests.delete(api_endpoint, headers=headers)
response = requests.delete(api_endpoint, headers=headers, timeout=60)

if response.status_code != 200:
print(f"{ConsoleShortcuts.error()} Failed to update a channel for {self}. Status code: {response.status_code}. Response Text: {response.text}")
raise requests.RequestException
else:
response = requests.put(api_endpoint, headers=headers, data=json.dumps({"content": content}))
response = requests.put(api_endpoint, headers=headers, data=json.dumps({"content": content}), timeout=60)

if response.status_code != 200:
print(f"{ConsoleShortcuts.error()} Failed to update a channel for {self}. Status code: {response.status_code}. Response Text: {response.text}")
Expand All @@ -98,7 +98,7 @@ def send_message(self, message_content: str, attachments: list[Attachment] | Non
'attachment': ('Unbenannt.PNG', attachment.data),
}

response = requests.post(api_endpoint, headers=headers, data=data, files=files)
response = requests.post(api_endpoint, headers=headers, data=data, files=files, timeout=60)
if response.status_code != 200:
print(f"{ConsoleShortcuts.error()} Failed to send message to {self}. Status code: {response.status_code}. Response Text: {response.text}")
raise requests.RequestException
Expand All @@ -117,7 +117,7 @@ def _get_messages_raw(self, amount: int) -> str:
"Content-Type": "application/json",
}

response = requests.get(api_endpoint, headers=headers)
response = requests.get(api_endpoint, headers=headers, timeout=60)
if response.status_code != 200:
print(f"Failed to get messages from {self}. Status code: {response.status_code}. Response Text: {response.text}")
raise requests.RequestException
Expand Down Expand Up @@ -166,4 +166,4 @@ def deserialize(json: dict) -> 'Channel':
new_channel.created_at = float(json["createdAt"])
new_channel.order = json["order"]

return new_channel
return new_channel
24 changes: 12 additions & 12 deletions src/Bot/nerimity/member.py
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ def follow(self) -> None:
"Content-Type": "application/json",
}

response = requests.post(api_endpoint, headers=headers)
response = requests.post(api_endpoint, headers=headers, timeout=60)
if response.status_code != 200:
print(f"{ConsoleShortcuts.error()} Failed to follow {self}. Status code: {response.status_code}. Response Text: {response.text}")
raise requests.RequestException
Expand All @@ -64,7 +64,7 @@ def unfollow(self) -> None:
"Content-Type": "application/json",
}

response = requests.delete(api_endpoint, headers=headers)
response = requests.delete(api_endpoint, headers=headers, timeout=60)
if response.status_code != 200:
print(f"{ConsoleShortcuts.error()} Failed to follow {self}. Status code: {response.status_code}. Response Text: {response.text}")
raise requests.RequestException
Expand All @@ -81,7 +81,7 @@ def add_friend(self) -> None:
"Content-Type": "application/json",
}

response = requests.post(api_endpoint, headers=headers)
response = requests.post(api_endpoint, headers=headers, timeout=60)
if response.status_code != 200:
print(f"{ConsoleShortcuts.error()} Failed to send a friend request to {self}. Status code: {response.status_code}. Response Text: {response.text}")
raise requests.RequestException
Expand All @@ -98,7 +98,7 @@ def remove_friend(self) -> None:
"Content-Type": "application/json",
}

response = requests.post(api_endpoint, headers=headers)
response = requests.post(api_endpoint, headers=headers, timeout=60)
if response.status_code != 200:
print(f"{ConsoleShortcuts.error()} Failed to send a friend request to {self}. Status code: {response.status_code}. Response Text: {response.text}")
raise requests.RequestException
Expand All @@ -118,7 +118,7 @@ def send_message(self, message_content: str) -> None:
"content": message_content,
}

response = requests.post(api_endpoint, headers=headers, data=json.dumps(data))
response = requests.post(api_endpoint, headers=headers, data=json.dumps(data), timeout=60)
if response.status_code != 200:
print(f"{ConsoleShortcuts.error()} Failed to send message to {self}. Status code: {response.status_code}. Response Text: {response.text}")
raise requests.RequestException
Expand Down Expand Up @@ -213,7 +213,7 @@ def kick(self) -> None:
"Content-Type": "application/json",
}

response = requests.delete(api_endpoint, headers=headers)
response = requests.delete(api_endpoint, headers=headers, timeout=60)
if response.status_code != 200:
print(f"{ConsoleShortcuts.error()} Failed to kick {self} from {self.server_id}. Status code: {response.status_code}. Response Text: {response.text}")
raise requests.RequestException
Expand All @@ -232,7 +232,7 @@ def ban(self, soft_ban: bool=False) -> None:
"Content-Type": "application/json",
}

response = requests.post(api_endpoint, headers=headers)
response = requests.post(api_endpoint, headers=headers, timeout=60)
if response.status_code != 200:
print(f"{ConsoleShortcuts.error()} Failed to ban {self} from {self.server_id}. Status code: {response.status_code}. Response Text: {response.text}")
raise requests.RequestException
Expand All @@ -248,7 +248,7 @@ def unban(self) -> None:
"Content-Type": "application/json",
}

response = requests.delete(api_endpoint, headers=headers)
response = requests.delete(api_endpoint, headers=headers, timeout=60)
if response.status_code != 200:
print(f"{ConsoleShortcuts.error()} Failed to ban {self} from {self.server_id}. Status code: {response.status_code}. Response Text: {response.text}")
raise requests.RequestException
Expand Down Expand Up @@ -306,7 +306,7 @@ def set_presence(self, value: int, custom_text: str) -> None:
"custom": custom_text
}

response = requests.post(api_endpoint, headers=headers, data=json.dumps(data))
response = requests.post(api_endpoint, headers=headers, data=json.dumps(data), timeout=60)
if response.status_code != 200:
print(f"Failed to set presence. Status code: {response.status_code}. Response Text: {response.text}")
raise requests.RequestException
Expand All @@ -327,7 +327,7 @@ def create_post(self, message_content: str) -> None:
"content": message_content,
}

response = requests.post(api_endpoint, headers=headers, data=data)
response = requests.post(api_endpoint, headers=headers, data=data, timeout=60)
pass
if response.status_code != 200:
print(f"{ConsoleShortcuts.error()} Failed to send message to {self}. Status code: {response.status_code}. Response Text: {response.text}")
Expand All @@ -345,7 +345,7 @@ async def get_feed(self) -> list[Post]:
"Content-Type": "application/json",
}

response = requests.get(api_endpoint, headers=headers)
response = requests.get(api_endpoint, headers=headers, timeout=60)
raw_feed = json.loads(response.content)

feed = []
Expand All @@ -369,4 +369,4 @@ def deserialize(json: dict) -> 'ClientMember':
new_member.badges = int(json["badges"])
new_member.friends = {}

return new_member
return new_member
10 changes: 5 additions & 5 deletions src/Bot/nerimity/message.py
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ def delete(self) -> None:
"Content-Type": "application/json",
}

response = requests.delete(api_endpoint, headers=headers)
response = requests.delete(api_endpoint, headers=headers, timeout=60)
if response.status_code != 200:
print(f"{ConsoleShortcuts.error()} Failed to delete {self}. Status code: {response.status_code}. Response Text: {response.text}")
raise requests.RequestException
Expand All @@ -76,7 +76,7 @@ def edit(self, edited_content: str):
"content": edited_content,
}

response = requests.patch(api_endpoint, headers=headers, data=json.dumps(data))
response = requests.patch(api_endpoint, headers=headers, data=json.dumps(data), timeout=60)
if response.status_code != 200:
print(f"{ConsoleShortcuts.error()} Failed to edit {self}. Status code: {response.status_code}. Response Text: {response.text}")
raise requests.RequestException
Expand All @@ -95,7 +95,7 @@ def react(self, emoji: str):
"name": emoji,
}

response = requests.post(api_endpoint, headers=headers, data=json.dumps(data))
response = requests.post(api_endpoint, headers=headers, data=json.dumps(data), timeout=60)
if response.status_code != 200:
print(f"{ConsoleShortcuts.error()} Failed to add '{emoji}' to {self}. Status code: {response.status_code}. Response Text: {response.text}")
raise requests.RequestException
Expand All @@ -114,7 +114,7 @@ def unreact(self, emoji: str):
"name": emoji,
}

response = requests.post(api_endpoint, headers=headers, data=json.dumps(data))
response = requests.post(api_endpoint, headers=headers, data=json.dumps(data), timeout=60)
if response.status_code != 200:
print(f"{ConsoleShortcuts.error()} Failed to add {emoji} to {self}. Status code: {response.status_code}. Response Text: {response.text}")
raise requests.RequestException
Expand All @@ -139,4 +139,4 @@ def deserialize(json: dict) -> 'Message':
new_message.attachments = list(Attachment.deserialize(i) for i in json["attachments"]) if json["attachments"] is not None else []
new_message.author = Member.deserialize(json["createdBy"])

return new_message
return new_message
12 changes: 6 additions & 6 deletions src/Bot/nerimity/post.py
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ def delete_post(self) -> None:
"Content-Type": "application/json",
}

response = requests.delete(api_endpoint, headers=headers)
response = requests.delete(api_endpoint, headers=headers, timeout=60)
if response.status_code != 200:
print(f"{ConsoleShortcuts.error()} Failed to delete {self}. Status code: {response.status_code}. Response Text: {response.text}")
raise requests.RequestException
Expand All @@ -59,7 +59,7 @@ def get_comments(self) -> list['Post']:
"Content-Type": "application/json",
}

response = requests.get(api_endpoint, headers=headers)
response = requests.get(api_endpoint, headers=headers, timeout=60)
if response.status_code != 200:
print(f"{ConsoleShortcuts.error()} Failed to get comments for {self}. Status code: {response.status_code}. Response Text: {response.text}")
raise requests.RequestException
Expand All @@ -78,7 +78,7 @@ def like(self) -> None:
}
json.dumps(self, default=vars)

response = requests.post(api_endpoint, headers=headers)
response = requests.post(api_endpoint, headers=headers, timeout=60)
if response.status_code != 200:
print(f"{ConsoleShortcuts.error()} Failed to like {self}. Status code: {response.status_code}. Response Text: {response.text}")
raise requests.RequestException
Expand All @@ -97,7 +97,7 @@ def unlike(self) -> None:
}
json.dumps(self, default=vars)

response = requests.post(api_endpoint, headers=headers)
response = requests.post(api_endpoint, headers=headers, timeout=60)
if response.status_code != 200:
print(f"{ConsoleShortcuts.error()} Failed to like {self}. Status code: {response.status_code}. Response Text: {response.text}")
raise requests.RequestException
Expand All @@ -118,7 +118,7 @@ def create_comment(self, message_content: str) -> None:
"postId": f"{self.id}",
}

response = requests.post(api_endpoint, headers=headers, data=json.dumps(data))
response = requests.post(api_endpoint, headers=headers, data=json.dumps(data), timeout=60)
if response.status_code != 200:
print(f"{ConsoleShortcuts.error()} Failed to create a post. Status code: {response.status_code}. Response Text: {response.text}")
raise requests.RequestException
Expand All @@ -143,4 +143,4 @@ def deserialize(json: dict) -> 'Post':
new_post.liked_by = [int(i["id"]) for i in json["likedBy"]]
new_post.attachments = json["attachments"]

return new_post
return new_post
4 changes: 2 additions & 2 deletions src/Bot/nerimity/roles.py
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ def update_role(self, server_id: int, name: str=None, hex_color: str=None, hide_
"permissions": permissions
}

response = requests.post(api_endpoint, headers=headers, data=json.dumps(data))
response = requests.post(api_endpoint, headers=headers, data=json.dumps(data), timeout=60)
if response.status_code != 200:
print(f"{ConsoleShortcuts.error()} Failed to update a role for {self}. Status code: {response.status_code}. Response Text: {response.text}")
raise requests.RequestException
Expand All @@ -67,4 +67,4 @@ def deserialize(json: dict) -> 'Role':
new_role.bot_role = bool(json["botRole"]) if json["botRole"] is not None else False
new_role.created_at = float(json["createdAt"])

return new_role
return new_role
Loading
Loading