def handleSessionCreate(self): length = self.headers["Content-length"] body = self.rfile.read(int(length)).decode("utf-8") parsed_body = parse_qs(body) #save the restaurant! email = parsed_body["email"][0] password = parsed_body["password"][0] #send these values to the DB! db = MemoryGameDB() user = db.getUserByEmail(email) if user == None: self.send_response(401) self.end_headers() else: if bcrypt.verify(password, user["password"]): # remember the user id in the session self.session["userId"] = user["uid"] self.send_response(201) self.end_headers() else: self.send_response(401) self.end_headers()
def handleMemoryUpdate(self, id): if self.isLoggedIn(): length = self.headers["Content-length"] body = self.rfile.read(int(length)).decode("utf-8") print("updated text body:", body) parsed_body = parse_qs(body) print("updated parsed body:", parsed_body) # save the quote quote = parsed_body["quote"][0] # send these values to the DB db = MemoryGameDB() memorygame = db.getMemory(id) if memorygame == None: self.handleNotFound() else: db.updateMemory(quote, id) self.send_response(200) # all headers go here: self.send_header("Content-type", "application/json") self.end_headers() self.wfile.write(bytes(json.dumps(memorygame), "utf-8")) else: self.handleUnauthorized()
def handleMemoryList(self, path): if self.isLoggedIn(): if self.path == "/quotes": self.send_response(200) # all headers go here: self.send_header("Content-type", "application/json") self.end_headers() db = MemoryGameDB() memorygame = db.getAllMemory() print("user_quote goes here") self.wfile.write(bytes(json.dumps(memorygame), "utf-8")) else: self.handleUnauthorized()
def handleMemoryRetrieve(self, id): if self.isLoggedIn(): db = MemoryGameDB() memorygame = db.getMemory(id) if memorygame == None: self.handleNotFound() else: self.send_response(200) # all headers go here: self.send_header("Content-type", "application/json") self.end_headers() self.wfile.write(bytes(json.dumps(memorygame), "utf-8")) else: self.handleUnauthorized()
def handleMemoryCreate(self): if self.isLoggedIn(): length = self.headers["Content-length"] body = self.rfile.read(int(length)).decode("utf-8") print("the text body:", body) parsed_body = parse_qs(body) print("the parsed body:", parsed_body) # save the quote name = parsed_body["name"][0] quote = parsed_body["quote"][0] score = parsed_body["score"][0] # send these values to the DB db = MemoryGameDB() db.createMemory(name, quote, score) self.send_response(201) self.end_headers() else: self.handleUnauthorized()
def handleUserCreate(self): length = self.headers["Content-length"] body = self.rfile.read(int(length)).decode("utf-8") print("the text body:", body) parsed_body = parse_qs(body) print("the parsed body:", parsed_body) # save the user fname = parsed_body["fname"][0] lname = parsed_body["lname"][0] email = parsed_body["email"][0] password = parsed_body["password"][0] encrypted_password = bcrypt.hash(password) # send these values to the DB db = MemoryGameDB() if db.getUserByEmail(email): self.send_response(422) self.end_headers() else: db.createUser(fname, lname, email, encrypted_password) self.send_response(201) self.end_headers()
def run(): db = MemoryGameDB() db.createMemoryGameTable() db.createUserTable() db = None # disconnect port = 8080 if len(sys.argv) > 1: port = int(sys.argv[1]) listen = ("0.0.0.0", port) server = HTTPServer(listen, MyRequestHandler) print("Server listening on", "{}:{}".format(*listen)) server.serve_forever()