-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSODAPIRequest.py
More file actions
88 lines (69 loc) · 3.06 KB
/
Copy pathSODAPIRequest.py
File metadata and controls
88 lines (69 loc) · 3.06 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
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
from httplib import HTTPSConnection
import httplib
import urllib
import json
class SODAPIRequestFailedException(Exception):
def __init__(self, value):
self.value = value
def __str__(self):
return repr(self.value)
class SODAPIRequest:
"""
A convenience class for constructing API calls to Discord.
Also has templates for a few of the common API calls.
"""
SODDiscordServerAddress = "discordapp.com"
SODDiscordAPIPrefix = "/api/"
SODStandardFormHeaders = {"Content-type": "application/x-www-form-urlencoded",
"Accept": "*/*"}
def __init__(self, apiEndpoint, method, headers, body):
"""
General constructor.
apiEndpoint (string): the API endpoint you want to make a request to.
Should be the part of the url after "https://discordapp.com/api/"
method (string): the HTTP method you want to use ("POST" or "GET")
parameters (dict): a dictionary of parameters.
"""
self.endpoint = SODAPIRequest.SODDiscordAPIPrefix + apiEndpoint
self.method = method
self.headers = headers
self.body = body
def makeFormRequest(self):
self.body = urllib.urlencode(self.body)
return self.makeRequest()
def makeRequest(self):
"""
Makes the request you have specified and returns the response
as a dict.
If the request does not return 200 OK, it raises a SODAPIRequestFailedException instead
token - Authorization token
"""
try:
conn = HTTPSConnection(SODAPIRequest.SODDiscordServerAddress)
conn.request(self.method, self.endpoint, self.body, self.headers)
response = conn.getresponse()
if response.status >= 200 and response.status < 300:
jsonresponse = response.read()
print jsonresponse
return json.loads(jsonresponse) if jsonresponse is not None else None
else:
raise SODAPIRequestFailedException("Response was: " + str(response.status) + " " + response.reason)
except Exception as e:
#TODO: Logging.
raise e
# Template requests
@staticmethod
def LoginRequest(email, password):
return SODAPIRequest("auth/login", "POST", SODAPIRequest.SODStandardFormHeaders, {"email": email, "password": password})
@staticmethod
def LogoutRequest(token):
return SODAPIRequest("auth/logout", "POST", {"authorization": token, "Content-Type": "application/json"}, {})
@staticmethod
def SendMessageRequest(token, channelId, content):
return SODAPIRequest("channels/" + channelId + "/messages", "POST", {"authorization": token, "Content-Type": "application/json"}, json.dumps({"content": content}))
@staticmethod
def GuildsRequest(token):
return SODAPIRequest("users/@me/guilds", "GET", {"authorization": token}, {})
@staticmethod
def ChannelsRequest(token, guildId):
return SODAPIRequest("guilds/" + str(guildId) + "/channels", "GET", {"authorization": token}, {})