-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathutil.py
More file actions
61 lines (50 loc) · 1.73 KB
/
util.py
File metadata and controls
61 lines (50 loc) · 1.73 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
import json
from typing import Set
from urllib import error, request
import discord
async def get_user_ids(r: discord.Reaction) -> Set[int]:
"""
Extracts user ids from a discord reaction.
This only exists because testing anything involving discord reactions
is a pain, and we want to mock it out.
"""
pids = set()
async for u in r.users():
pids.add(u.id)
return pids
def make_request(base_url,
additional_url,
params={},
data=None,
raise_exception_on_http_error=False,
method=None):
"""
Fetches resource at URL, converts JSON response to object.
The URL is just base_url + additional_url.
If data is set, it should be a dictionary, which will be encoded as JSON.
This will make the request a POST request instead of GET.
"""
url = base_url + additional_url
first_item = True
for param, value in params.items():
if first_item:
url += f'?{param}={value}'
first_item = False
continue
url += f'&{param}={value}'
headers = {}
if data is not None:
data = json.dumps(data).encode()
headers['Content-Type'] = 'application/json'
try:
r = request.Request(url, data, headers, method=method)
response = request.urlopen(r)
except error.HTTPError as e:
# Usually we want to return any data on an HTTP error,
# but sometimes we may wish to still treat it as an exception.
if raise_exception_on_http_error:
raise e
response = e
# Convert raw response to usable JSON object
response_as_string = response.read().decode('utf-8')
return json.loads(response_as_string)