Better response bodies
This commit is contained in:
parent
d598eb3f79
commit
4ac68b006e
22
api.py
22
api.py
|
@ -12,8 +12,8 @@ class api(BaseHTTPRequestHandler):
|
||||||
def send(self, code, body):
|
def send(self, code, body):
|
||||||
"""Send HTTP response and body"""
|
"""Send HTTP response and body"""
|
||||||
|
|
||||||
|
print(code, body)
|
||||||
self.send_response(code)
|
self.send_response(code)
|
||||||
self.send_response(200)
|
|
||||||
self.send_header('Content-Type', 'text/plain')
|
self.send_header('Content-Type', 'text/plain')
|
||||||
self.send_header('Content-Length', str(len(body)))
|
self.send_header('Content-Length', str(len(body)))
|
||||||
self.end_headers()
|
self.end_headers()
|
||||||
|
@ -22,13 +22,16 @@ class api(BaseHTTPRequestHandler):
|
||||||
def do_GET(self):
|
def do_GET(self):
|
||||||
"""Handle GET requests"""
|
"""Handle GET requests"""
|
||||||
|
|
||||||
self.send(200, 'Hello')
|
self.send(200, 'Hey there, you\'ve reached the top secret exozyme api')
|
||||||
|
|
||||||
def do_POST(self):
|
def do_POST(self):
|
||||||
"""Handle API POST requests"""
|
"""Handle API POST requests"""
|
||||||
|
|
||||||
content_length = int(self.headers['Content-Length'])
|
content_length = int(self.headers['Content-Length'])
|
||||||
data = dict(parse_qsl(self.rfile.read(content_length).decode('utf-8')))
|
try:
|
||||||
|
data = dict(parse_qsl(self.rfile.read(content_length).decode('utf-8')))
|
||||||
|
except:
|
||||||
|
self.send(400, 'Oops, that request doesn\'t look quite right')
|
||||||
|
|
||||||
# Print debug data
|
# Print debug data
|
||||||
debug = data.copy()
|
debug = data.copy()
|
||||||
|
@ -39,20 +42,17 @@ class api(BaseHTTPRequestHandler):
|
||||||
if self.path == '/api/join':
|
if self.path == '/api/join':
|
||||||
# New user
|
# New user
|
||||||
if data['code'] != code():
|
if data['code'] != code():
|
||||||
print('Incorrect code')
|
self.send(403, 'Nice try, but that secret code is totally wrong')
|
||||||
self.send(403, 'Incorrect code')
|
|
||||||
return
|
return
|
||||||
if not all(c.isdigit() or c.islower() for c in data['username']) or data['username'][0].isdigit():
|
if not all(c.isdigit() or c.islower() for c in data['username']) or data['username'][0].isdigit():
|
||||||
print('Bad username')
|
self.send(403, 'I don\'t like that username')
|
||||||
self.send(403, 'Bad username')
|
|
||||||
return
|
return
|
||||||
|
|
||||||
# Add the user
|
# Add the user
|
||||||
adduser(data['username'], data['firstname'].capitalize(), data['lastname'].capitalize(),
|
adduser(data['username'], data['firstname'].capitalize(), data['lastname'].capitalize(),
|
||||||
data['email'], data['password'])
|
data['email'], data['password'])
|
||||||
else:
|
|
||||||
print('Bad request')
|
self.send(200, 'Well I think it worked...')
|
||||||
self.send(400, 'Bad request')
|
|
||||||
return
|
return
|
||||||
|
|
||||||
self.send(200, 'It worked!')
|
self.send(501, 'We don\'t know how to do that yet')
|
||||||
|
|
Loading…
Reference in a new issue