def HandleCarUpdate(self, id): if "userId" not in self.session: self.Handle401() return length = int(self.headers["Content-length"]) body = self.rfile.read(length).decode("utf-8") parsed_body = parse_qs(body) db = CarsDB() if db.getCar(id) == None: self.Handle404() return owner_name = parsed_body['owner_name'][0] year = parsed_body['year'][0] make = parsed_body['make'][0] model = parsed_body['model'][0] color = parsed_body['color'][0] platenumber = parsed_body['platenumber'][0] db.getUpdate(id, owner_name, year, make, model, color, platenumber) self.send_response(200) self.end_headers() self.wfile.write(bytes("Updated", "utf-8")) return
def HandleCarList(self): if "userId" not in self.session: self.Handle401() return db = CarsDB() cars = db.getCars() json_string = json.dumps(cars) print("JSON String", json_string) self.send_response(200) self.send_header("Content-Type", "application/json") self.end_headers() self.wfile.write(bytes(json_string, "utf-8")) return
def HandleCarDeletion(self, id): if "userId" not in self.session: self.Handle401() return db = CarsDB() car = db.getCar(id) if car == None: self.Handle404() return deleted = db.getDelete(id) json_string = json.dumps(deleted) print("JSON String", json_string) self.send_response(200) self.send_header("Content-Type", "application/json") self.end_headers() self.wfile.write(bytes(json_string, "utf-8")) return
def HandleUserCreation(self): length = int(self.headers["Content-length"]) body = self.rfile.read(length).decode("utf-8") parsed_body = parse_qs(body) db = CarsDB() fname = parsed_body['fname'][0] lname = parsed_body['lname'][0] email = parsed_body['email'][0] password = parsed_body['password'][0] user = db.getUserbyEmail(email) if user == None: db.createUser(fname, lname, email, password) self.send_response(201) self.end_headers() self.wfile.write(bytes("Created", "utf-8")) else: self.Handle422()
def HandleSessionCreation(self): length = int(self.headers["Content-length"]) body = self.rfile.read(length).decode("utf-8") parsed_body = parse_qs(body) db = CarsDB() email = parsed_body['email'][0] password = parsed_body['password'][0] user = db.getUserbyEmail(email) if user != None: print(user) if bcrypt.verify(password, user["encrypted_password"]) == True: self.session["userId"] = user["id"] self.send_response(201) self.send_cookie() self.end_headers() self.wfile.write(bytes(json.dumps(user), "utf-8")) else: self.Handle401() else: self.Handle401() return
def main(): db = CarsDB() db.createCarTable() db.createUserTable() db = None port = 8080 if len(sys.argv) > 1: port = int(sys.argv[1]) listen = ("0.0.0.0", port) server = HTTPServer(listen, MyHandler) print("Server Listening on...", "{}:{}".format(*listen)) server.serve_forever()