-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrest.py
57 lines (44 loc) · 1.38 KB
/
rest.py
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
from http.client import HTTPConnection
class Rest:
def __init__(self, host, port, uri, method):
self.m_host = host
self.m_port = port
self.m_method = method
self.m_uri = uri
self.m_headers = {}
self.m_body = None
def __setitem__(self, key, value):
self.m_headers[key] = value
def __getitem__(self, key):
return self.m_headers[key]
def __delitem__(self, key):
del self.m_headers[key]
def body(self, body):
self.m_body = body
def authorize(self, sessionId):
self.m_headers["Authorization"] = "Bearer " + sessionId
def perform(self):
conn = HTTPConnection(self.m_host, self.m_port, timeout = 10)
conn.request(self.m_method, self.m_uri,
headers = self.m_headers, body = self.m_body)
response = conn.getresponse()
content = str(response.read(), "utf-8")
conn.close()
return response, content
def __call__(self):
return self.perform()
class GET(Rest):
def __init__(self, host, port, uri):
Rest.__init__(self, host, port, uri, "GET")
class POST(Rest):
def __init__(self, host, port, uri):
Rest.__init__(self, host, port, uri, "POST")
class PUT(Rest):
def __init__(self, host, port, uri):
Rest.__init__(self, host, port, uri, "PUT")
class DELETE(Rest):
def __init__(self, host, port, uri):
Rest.__init__(self, host, port, uri, "DELETE")
class OPTIONS(Rest):
def __init__(self, host, port, uri):
Rest.__init__(self, host, port, uri, "OPTIONS")