From cf559d1e2e48291ff8c9d0555d9dc16b86e644a2 Mon Sep 17 00:00:00 2001 From: Anthony Wang Date: Mon, 23 May 2022 22:39:12 -0500 Subject: [PATCH] Restructure API so the user registration endpoint is /api/user/new --- api.py | 32 ++++++++++++++++++-------------- 1 file changed, 18 insertions(+), 14 deletions(-) diff --git a/api.py b/api.py index 487ba07..834c01f 100644 --- a/api.py +++ b/api.py @@ -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')