def delete(self, type_: str) -> Response: """Delete a non Collection class item.""" if get_authentication(): if request.authorization is None: return failed_authentication() else: try: auth = check_authorization(request, get_session()) if auth is False: return failed_authentication() except Exception as e: status_code, message = e.get_HTTP() # type: ignore return set_response_headers(jsonify(message), status_code=status_code) endpoint_ = checkEndpoint("DELETE", type_) if endpoint_['method']: # No Delete Operation for collections if type_ in get_doc( ).parsed_classes and type_ + "Collection" not in get_doc( ).collections: try: crud.delete_single(type_, session=get_session()) response = {"message": "Object successfully deleted"} return set_response_headers(jsonify(response)) except Exception as e: status_code, message = e.get_HTTP() # type: ignore return set_response_headers(jsonify(message), status_code=status_code) abort(endpoint_['status'])
def on_delete(self, req, resp, type_: str): """ Method executed for DELETE requests. Used to delete a non-collection class. :param type_ - Item type """ if get_authentication(resp): if req.auth is None: return failed_authentication(resp) else: try: auth = check_authorization(req, get_session(resp)) if auth is False: return failed_authentication(resp) except Exception as e: status_code, message = e.get_HTTP() # type: ignore resp.media = message return set_response_headers(resp, status_code=status_code) endpoint_ = checkEndpoint(resp, "DELETE", type_) if endpoint_['method']: # No Delete Operation for collections if type_ in get_doc(resp).parsed_classes and type_ + "Collection" not in get_doc(resp).collections: try: crud.delete_single(type_, session=get_session(resp)) response = {"message": "Object successfully deleted"} resp.media = response return set_response_headers(resp) except Exception as e: status_code, message = e.get_HTTP() resp.media = message return set_response_headers(resp, status_code=status_code)
def get(self, id_, type_): """GET object with id = id_ from the database.""" if get_authentication(): if request.authorization is None: return failed_authentication() else: auth = check_authorization(request, get_session()) if auth is False: return failed_authentication() class_type = get_doc().collections[type_]["collection"].class_.title if checkClassOp(class_type, "GET"): try: response = crud.get(id_, class_type, api_name=get_api_name(), session=get_session()) return set_response_headers(jsonify(hydrafy(response))) except Exception as e: status_code, message = e.get_HTTP() return set_response_headers(jsonify(message), status_code=status_code) abort(405)
def on_delete(self, req, resp, id_: int, type_: str): """Delete object with id=id_ from database.""" if get_authentication(resp): if req.auth is None: return failed_authentication(resp) else: try: auth = check_authorization(req, get_session(resp)) if auth is False: return failed_authentication(resp) except Exception as e: status_code, message = e.get_HTTP() # type: ignore resp.media = message return set_response_headers(resp, status_code=status_code) class_type = get_doc(resp).collections[type_]["collection"].class_.title if checkClassOp(resp, class_type, "DELETE"): # Check if class_type supports PUT operation try: # Delete the Item with ID == id_ crud.delete(id_, class_type, session=get_session(resp)) response = { "message": "Object with ID %s successfully deleted" % (id_)} resp.media = response return set_response_headers(resp) except Exception as e: status_code, message = e.get_HTTP() resp.media = message return set_response_headers(resp, status_code= status_code) resp.status = falcon.HTTP_405
def on_get(self, req, resp, id_, type_): """GET object with id = id_ from the database.""" if get_authentication(resp): if req.auth is None: return failed_authentication(resp) else: try: auth = check_authorization(req, get_session(resp)) if auth is False: return failed_authentication(resp) except Exception as e: status_code, message = e.get_HTTP() # type: ignore resp.media = message return set_response_headers(resp, status_code=status_code) class_type = get_doc(resp).collections[type_]["collection"].class_.title if checkClassOp(resp, class_type, "GET"): try: resp.media = hydrafy(resp, crud.get(id_, class_type, api_name=get_api_name(resp), session=get_session(resp))) return set_response_headers(resp) except Exception as e: status_code, message = e.get_HTTP() resp.media = message return set_response_headers(resp, status_code= status_code) resp.status = falcon.HTTP_405
def post(self, type_): """Update Non Collection class item.""" if get_authentication(): if request.authorization is None: return failed_authentication() else: try: auth = check_authorization(request, get_session()) if auth is False: return failed_authentication() except Exception as e: status_code, message = e.get_HTTP() return set_response_headers(jsonify(message), status_code=status_code) if checkEndpoint("POST", type_): object_ = json.loads(request.data.decode('utf-8')) if type_ in get_doc().parsed_classes and type_+"Collection" not in get_doc().collections: obj_type = getType(type_, "POST") if validObject(object_): if object_["@type"] == obj_type: # try: crud.update_single(object_=object_, session=get_session(), api_name=get_api_name()) headers_ = [{"Location": get_hydrus_server_url()+get_api_name()+"/"+type_+"/"}] response = {"message": "Object successfully updated"} return set_response_headers(jsonify(response), headers=headers_) # except Exception as e: # status_code, message = e.get_HTTP() # return set_response_headers(jsonify(message), status_code=status_code) return set_response_headers(jsonify({400: "Data is not valid"}), status_code=400) abort(405)
def delete(self, id_, type_): """Delete object with id=id_ from database.""" if get_authentication(): if request.authorization is None: return failed_authentication() else: auth = check_authorization(request, get_session()) if auth is False: return failed_authentication() class_type = get_doc().collections[type_]["collection"].class_.title if checkClassOp(class_type, "DELETE"): try: crud.delete(id_, class_type, session=get_session()) response = { "message": "Object with ID %s successfully deleted" % (id_) } return set_response_headers(jsonify(response)) except Exception as e: status_code, message = e.get_HTTP() return set_response_headers(jsonify(message), status_code=status_code) abort(405)
def on_put(self, req, resp, id_: int, type_: str): """Add new object_ optional <id_> parameter using HTTP PUT. :param id_ - ID of Item to be updated :param type_ - Type(Class name) of Item to be updated """ if get_authentication(resp): if req.auth is None: return failed_authentication(resp) else: try: auth = check_authorization(req, get_session(resp)) if auth is False: return failed_authentication(resp) except Exception as e: status_code, message = e.get_HTTP() # type: ignore resp.media = message return set_response_headers(resp, status_code=status_code) class_type = get_doc( resp).collections[type_]["collection"].class_.title if checkClassOp(resp, class_type, "PUT"): # Check if class_type supports PUT operation object_ = req.media obj_type = getType(resp, class_type, "PUT") # Load new object and type if validObject(object_): if object_["@type"] == obj_type: try: # Add the object with given ID object_id = crud.insert(object_=object_, id_=id_, session=get_session(resp)) headers_ = [{ "Location": get_hydrus_server_url(resp) + get_api_name(resp) + "/" + type_ + "/" + str(object_id) }] response = { "message": "Object with ID %s successfully added" % (object_id) } resp.media = response return set_response_headers( resp, headers=headers_[0], status_code=falcon.HTTP_201) except Exception as e: status_code, message = e.get_HTTP() resp.media = message return set_response_headers(resp, status_code=status_code) return set_response_headers(resp, status_code=falcon.HTTP_400) resp.status = falcon.HTTP_405
def put(self, type_): """Add item to ItemCollection.""" if get_authentication(): if request.authorization is None: return failed_authentication() else: try: auth = check_authorization(request, get_session()) if auth is False: return failed_authentication() except Exception as e: status_code, message = e.get_HTTP() return set_response_headers(jsonify(message), status_code=status_code) if checkEndpoint("PUT", type_): object_ = json.loads(request.data.decode('utf-8')) # Collections if type_ in get_doc().collections: collection = get_doc().collections[type_]["collection"] obj_type = collection.class_.title if validObject(object_): if object_["@type"] == obj_type: try: object_id = crud.insert(object_=object_, session=get_session()) headers_ = [{"Location": get_hydrus_server_url()+get_api_name()+"/"+type_+"/"+str(object_id)}] response = {"message": "Object with ID %s successfully deleted" % (object_id)} return set_response_headers(jsonify(response), headers=headers_, status_code=201) except Exception as e: status_code, message = e.get_HTTP() return set_response_headers(jsonify(message), status_code=status_code) return set_response_headers(jsonify({400: "Data is not valid"}), status_code=400) # Non Collection classes elif type_ in get_doc().parsed_classes and type_+"Collection" not in get_doc().collections: obj_type = getType(type_, "PUT") if object_["@type"] == obj_type: if validObject(object_): try: object_id = crud.insert(object_=object_, session=get_session()) headers_ = [{"Location": get_hydrus_server_url()+get_api_name()+"/"+type_+"/"}] response = {"message": "Object successfully added"} return set_response_headers(jsonify(response), headers=headers_, status_code=201) except Exception as e: status_code, message = e.get_HTTP() return set_response_headers(jsonify(message), status_code=status_code) return set_response_headers(jsonify({400: "Data is not valid"}), status_code=400) abort(405)
def post(self, id_: int, type_: str) -> Response: """Update object of type<type_> at ID<id_> with new object_ using HTTP POST.""" if get_authentication(): if request.authorization is None: return failed_authentication() else: try: auth = check_authorization(request, get_session()) if auth is False: return failed_authentication() except Exception as e: status_code, message = e.get_HTTP() # type: ignore return set_response_headers(jsonify(message), status_code=status_code) class_type = get_doc().collections[type_]["collection"].class_.title if checkClassOp(class_type, "POST"): object_ = json.loads(request.data.decode('utf-8')) obj_type = getType(class_type, "POST") if validObject(object_): if object_["@type"] == obj_type: try: object_id = crud.update(object_=object_, id_=id_, type_=object_["@type"], session=get_session(), api_name=get_api_name()) headers_ = [{ "Location": get_hydrus_server_url() + get_api_name() + "/" + type_ + "/" + str(object_id) }] response = { "message": "Object with ID %s successfully updated" % (object_id) } return set_response_headers(jsonify(response), headers=headers_) except Exception as e: status_code, message = e.get_HTTP() # type: ignore return set_response_headers(jsonify(message), status_code=status_code) return set_response_headers(jsonify({400: "Data is not valid"}), status_code=400) abort(405)
def on_post(self, req, resp, type_: str): """ Method executed for POST requests. Used to update a non-collection class. :param type_ - Item type """ if get_authentication(resp): if req.auth is None: return failed_authentication(resp) else: try: auth = check_authorization(req, get_session(resp)) if auth is False: return failed_authentication(resp) except Exception as e: status_code, message = e.get_HTTP() # type: ignore resp.media = message return set_response_headers(resp, status_code=status_code) endpoint_ = checkEndpoint(resp, "POST", type_) if endpoint_['method']: object_ = req.media if type_ in get_doc( resp ).parsed_classes and type_ + "Collection" not in get_doc( resp).collections: obj_type = getType(resp, type_, "POST") if validObject(object_): if object_["@type"] == obj_type: try: crud.update_single(object_=object_, session=get_session(resp), api_name=get_api_name(resp)) headers_ = [{ "Location": get_hydrus_server_url(resp) + get_api_name(resp) + "/" + type_ + "/" }] response = { "message": "Object successfully updated" } resp.media = response return set_response_headers(resp, headers=headers_[0]) except Exception as e: status_code, message = e.get_HTTP() resp.media = message return set_response_headers( resp, status_code=status_code) return set_response_headers(resp, status_code=falcon.HTTP_400)
def verify_user() -> Union[Response, None]: """ Verify the credentials of the user and assign token. """ try: auth = check_authorization(request, get_session()) if auth is False: return failed_authentication(True) elif get_token(): token = add_token(request, get_session()) return token_response(token) except Exception as e: error = e.get_HTTP() # type: HydraError return error_response(error) return None
def verify_user() -> Union[Response, None]: """ Verify the credentials of the user and assign token. """ try: auth = check_authorization(request, get_session()) if auth is False: return failed_authentication(True) elif get_token(): token = add_token(request, get_session()) return token_response(token) except Exception as e: status_code, message = e.get_HTTP() # type: ignore return set_response_headers(jsonify(message), status_code=status_code) return None
def on_get(self, req, resp, type_): """Retrieve a collection of items from the database.""" if get_authentication(resp): if req.auth is None: return failed_authentication(resp) else: try: auth = check_authorization(req, get_session(resp)) if auth is False: return failed_authentication(resp) except Exception as e: status_code, message = e.get_HTTP() # type: ignore resp.media = message return set_response_headers(resp, status_code=status_code) if checkEndpoint(resp, "GET", type_): # Collections if type_ in get_doc(resp).collections: collection = get_doc(resp).collections[type_]["collection"] try: resp.media = crud.get_collection(get_api_name(resp), collection.class_.title, session=get_session(resp)) return set_response_headers(resp) except Exception as e: status_code, message = e.get_HTTP() resp.media = message return set_response_headers(resp, status_code=status_code) # Non Collection classes elif type_ in get_doc( resp ).parsed_classes and type_ + "Collection" not in get_doc( resp).collections: try: resp.media = hydrafy( resp, crud.get_single(type_, api_name=get_api_name(resp), session=get_session(resp))) return set_response_headers(resp) except Exception as e: status_code, message = e.get_HTTP() resp.media = message return set_response_headers(resp, status_code=status_code)
def get(self, type_: str) -> Response: """Retrieve a collection of items from the database.""" if get_authentication(): if request.authorization is None: return failed_authentication() else: try: auth = check_authorization(request, get_session()) if auth is False: return failed_authentication() except Exception as e: status_code, message = e.get_HTTP() # type: ignore return set_response_headers(jsonify(message), status_code=status_code) endpoint_ = checkEndpoint("GET", type_) if endpoint_['method']: # Collections if type_ in get_doc().collections: collection = get_doc().collections[type_]["collection"] try: response = crud.get_collection(get_api_name(), collection.class_.title, session=get_session()) return set_response_headers(jsonify(hydrafy(response))) except Exception as e: status_code, message = e.get_HTTP() # type: ignore return set_response_headers(jsonify(message), status_code=status_code) # Non Collection classes elif type_ in get_doc( ).parsed_classes and type_ + "Collection" not in get_doc( ).collections: try: response = crud.get_single(type_, api_name=get_api_name(), session=get_session()) return set_response_headers(jsonify(hydrafy(response))) except Exception as e: status_code, message = e.get_HTTP() # type: ignore return set_response_headers(jsonify(message), status_code=status_code) abort(endpoint_['status'])
def put(self, id_, type_): """Add new object_ optional <id_> parameter using HTTP PUT.""" if get_authentication(): if request.authorization is None: return failed_authentication() else: auth = check_authorization(request, get_session()) if auth is False: return failed_authentication() class_type = get_doc().collections[type_]["collection"].class_.title if checkClassOp(class_type, "PUT"): object_ = json.loads(request.data.decode('utf-8')) obj_type = getType(class_type, "PUT") if validObject(object_): if object_["@type"] == obj_type: try: object_id = crud.insert(object_=object_, id_=id_, session=get_session()) headers_ = [{ "Location": get_hydrus_server_url() + get_api_name() + "/" + type_ + "/" + str(object_id) }] response = { "message": "Object with ID %s successfully added" % (object_id) } return set_response_headers(jsonify(response), headers=headers_, status_code=201) except Exception as e: status_code, message = e.get_HTTP() return set_response_headers(jsonify(message), status_code=status_code) return set_response_headers(jsonify({400: "Data is not valid"}), status_code=400) abort(405)
def on_put(self, req, resp, type_: str): """ Method executed for PUT requests. Used to add an item to a collection :param type_ - Item type """ if get_authentication(resp): if req.auth is None: return failed_authentication(resp) else: try: auth = check_authorization(req, get_session(resp)) if auth is False: return failed_authentication(resp) except Exception as e: status_code, message = e.get_HTTP() # type: ignore resp.media = message return set_response_headers(resp, status_code=status_code) endpoint_ = checkEndpoint(resp, "PUT", type_) if endpoint_['method']: # If endpoint and PUT method is supported in the API object_ = req.media if type_ in get_doc(resp).collections: # If collection name in document's collections collection = get_doc(resp).collections[type_]["collection"] # title of HydraClass object corresponding to collection obj_type = collection.class_.title if validObject(object_): # If Item in request's JSON is a valid object # ie. @type is one of the keys in object_ if object_["@type"] == obj_type: # If the right Item type is being added to the collection try: # Insert object and return location in Header object_id = crud.insert(object_=object_, session=get_session(resp)) headers_ = [{ "Location": get_hydrus_server_url(resp) + get_api_name(resp) + "/" + type_ + "/" + str(object_id) }] response = { "message": "Object with ID %s successfully added" % (object_id) } resp.media = response return set_response_headers( resp, headers=headers_[0], status_code=falcon.HTTP_201) except Exception as e: status_code, message = e.get_HTTP() resp.media = message return set_response_headers( resp, status_code=status_code) return set_response_headers(resp, status_code=falcon.HTTP_400) elif type_ in get_doc( resp ).parsed_classes and type_ + "Collection" not in get_doc( resp).collections: # If type_ is in parsed_classes but is not a collection obj_type = getType(resp, type_, "PUT") if object_["@type"] == obj_type: if validObject(object_): try: object_id = crud.insert(object_=object_, session=get_session(resp)) headers_ = [{ "Location": get_hydrus_server_url(resp) + get_api_name(resp) + "/" + type_ + "/" }] response = {"message": "Object successfully added"} resp.media = response return set_response_headers( resp, headers=headers_[0], status_code=falcon.HTTP_201) except Exception as e: status_code, message = e.get_HTTP() resp.media = message return set_response_headers( resp, status_code=status_code) return set_response_headers(resp, status_code=falcon.HTTP_400)