def query(conn: socket, request: str, trie: Trie): query_request = request keys = query_request[len(QUERY):].strip() key = keys.split(".")[0] subkeys = keys.split(".")[1:] # get the top level key value = trie.get(key=key) if not value: not_found_response(s=conn, request=request) return # if user only gave first level key return it if len(keys.split(".")) == 1: # value = json.dumps(value).replace(",", ";") log(code=SUCCESS, request=request) construct_response(s=conn, data={key: value}, success=True) return result = trie.query(value=value, subkeys=subkeys, level=0) if not result: not_found_response(s=conn, request=request) return # to string for print method based on return type log(code=SUCCESS, request=request) construct_response(s=conn, data={keys: json.loads(result)}, success=True)
def get(conn: socket, request: str, trie: Trie): get_request = request key = get_request[len(GET):].strip() value = trie.get(key=key) if not value: not_found_response(s=conn, request=request) return # value = json.dumps(value).replace(",", ";") log(code=SUCCESS, request=request) construct_response(s=conn, data={key: value}, success=True)
def delete(conn: socket, request: str, trie: Trie): delete_request = request key = delete_request[len(DELETE):].strip() # check if top level key exists value = trie.get(key=key) if not value: not_found_response(s=conn, request=request) return trie.delete(key=key) log(code=DELETE, request=request) construct_response(s=conn, data=OK, success=True)
def show_model_response(): try: token = get_token_from_request() current_quotas = users.get_user_quotas(token=token) input_image=request.files.get("image") image = parse_info_as_image(input_image) gender= request.values.get("gender").encode('utf-8') except errors.UserAuthenticationError as bad_token_error: return errors.unauthorized_response(message=str(bad_token_error)) except errors.UserNotFoundError: return errors.not_found_response("No user associated with your token.") except errors.ImageNotFoundError: return errors.bad_request_response("Please include an image in your request body.") if current_quotas["quota_left"] <= 0: return errors.unauthorized_response(message="No more request quota.", dict={"quotas": current_quotas}) try: new_quotas = users.decrement_user_quota(token=token) predictions = classifier.predict(img=image, gender=gender) except Exception: print('error from classification') traceback.print_exc() return response_body = { "status": "ok", "quotas": new_quotas, "results": predictions } return jsonify(response_body)
def show_single_user(name): try: user_info = users.get_user_info(name=name) except errors.UserNotFoundError: return errors.not_found_response("No user associated with that name.") response_data = { "status": "ok", "user": user_info } return jsonify(response_data)
def show_echo(): try: token = get_token_from_request() users.get_user_by_token(token=token) image = parse_info_as_image(request.data) except errors.UserAuthenticationError as bad_token_error: return errors.unauthorized_response(message=str(bad_token_error)) except errors.UserNotFoundError: return errors.not_found_response("No user associated with your token.") except errors.ImageNotFoundError: return errors.bad_request_response("Please include an image in your request body.") return image
def show_delete_user_response(name): try: deleted_user_info = users.delete_user_by_name(name=name) except errors.UserNotFoundError: return errors.not_found_response("No user associated with that name.") except errors.UserConflictError as user_conflict_error: return errors.conflict_response(message=str(user_conflict_error)) response_data = { "status": "ok", "user": deleted_user_info } return jsonify(response_data)
def show_user_quota(): try: token = get_token_from_request() current_quotas = users.get_user_quotas(token=token) except errors.UserAuthenticationError as bad_token_error: return errors.unauthorized_response(message=str(bad_token_error)) except errors.UserNotFoundError: return errors.not_found_response("No user associated with your token.") if current_quotas["quota_left"] <= 0: return errors.unauthorized_response(message="No more request quota.", dict={"quotas": current_quotas}) response_body = { "status": "ok", "quotas": current_quotas } return jsonify(response_body)
def show_update_user_response(name): try: clean_info = parse_info_as_json(request.data) old_user_info = users.get_user_info(name) updated_user_info = users.update_user_from_info( name=name, info=clean_info) except errors.UserNotFoundError: return errors.not_found_response("No user associated with that name.") except errors.JSONNotFoundError: return errors.bad_request_response("Updated user details are required in a proper JSON request body.") except errors.UserInfoError as user_info_error: return errors.bad_request_response(message=str(user_info_error)) except errors.UserConflictError as user_conflict_error: return errors.conflict_response(message=str(user_conflict_error)) response_data = { "status": "ok", "user": updated_user_info, "old_info": old_user_info } return jsonify(response_data)
def send_error_not_found(error): return errors.not_found_response()