Restructure API so the user registration endpoint is /api/user/new

This commit is contained in:
Anthony Wang 2022-05-23 22:39:12 -05:00
parent 471cc8d254
commit cf559d1e2e
Signed by: a
GPG key ID: BC96B00AEC5F2D76

32
api.py
View file

@ -19,6 +19,22 @@ class api(BaseHTTPRequestHandler):
self.end_headers()
self.wfile.write(body.encode('utf-8'))
def usernew(self, data):
"""Register a new user"""
if data['code'] != code():
self.send(403, 'Nice try, but that secret code is totally wrong')
return
if not all(c.isdigit() or c.islower() for c in data['username']) or data['username'][0].isdigit():
self.send(403, 'I don\'t like that username')
return
# Add the user
adduser(data['username'], data['firstname'].capitalize(), data['lastname'].capitalize(),
data['email'], data['password'])
self.send(200, 'Well I think it worked...')
def do_GET(self):
"""Handle GET requests"""
@ -39,20 +55,8 @@ class api(BaseHTTPRequestHandler):
debug.pop('confirmpassword')
print(debug)
if self.path == '/api/join':
# New user
if data['code'] != code():
self.send(403, 'Nice try, but that secret code is totally wrong')
return
if not all(c.isdigit() or c.islower() for c in data['username']) or data['username'][0].isdigit():
self.send(403, 'I don\'t like that username')
return
# Add the user
adduser(data['username'], data['firstname'].capitalize(), data['lastname'].capitalize(),
data['email'], data['password'])
self.send(200, 'Well I think it worked...')
if self.path == '/api/user/new':
self.usernew(data)
return
self.send(501, 'We don\'t know how to do that yet')