-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathrequest_handler.py
90 lines (72 loc) · 2.57 KB
/
request_handler.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
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
89
90
from http.server import BaseHTTPRequestHandler, HTTPServer
import json
from views.user import create_user, login_user
class HandleRequests(BaseHTTPRequestHandler):
"""Handles the requests to this server"""
def parse_url(self):
"""Parse the url into the resource and id"""
path_params = self.path.split('/')
resource = path_params[1]
if '?' in resource:
param = resource.split('?')[1]
resource = resource.split('?')[0]
pair = param.split('=')
key = pair[0]
value = pair[1]
return (resource, key, value)
else:
id = None
try:
id = int(path_params[2])
except (IndexError, ValueError):
pass
return (resource, id)
def _set_headers(self, status):
"""Sets the status code, Content-Type and Access-Control-Allow-Origin
headers on the response
Args:
status (number): the status code to return to the front end
"""
self.send_response(status)
self.send_header('Content-type', 'application/json')
self.send_header('Access-Control-Allow-Origin', '*')
self.end_headers()
def do_OPTIONS(self):
"""Sets the OPTIONS headers
"""
self.send_response(200)
self.send_header('Access-Control-Allow-Origin', '*')
self.send_header('Access-Control-Allow-Methods',
'GET, POST, PUT, DELETE')
self.send_header('Access-Control-Allow-Headers',
'X-Requested-With, Content-Type, Accept')
self.end_headers()
def do_GET(self):
"""Handle Get requests to the server"""
pass
def do_POST(self):
"""Make a post request to the server"""
self._set_headers(201)
content_len = int(self.headers.get('content-length', 0))
post_body = json.loads(self.rfile.read(content_len))
response = ''
resource, _ = self.parse_url()
if resource == 'login':
response = login_user(post_body)
if resource == 'register':
response = create_user(post_body)
self.wfile.write(response.encode())
def do_PUT(self):
"""Handles PUT requests to the server"""
pass
def do_DELETE(self):
"""Handle DELETE Requests"""
pass
def main():
"""Starts the server on port 8088 using the HandleRequests class
"""
host = ''
port = 8088
HTTPServer((host, port), HandleRequests).serve_forever()
if __name__ == "__main__":
main()