def get(self, id_: str, path: str) -> Response: """ GET object with id = id_ from the database. :param id_ : Item ID :param path : Path for Item ( Specified in APIDoc @id) """ id_ = str(id_) auth_response = check_authentication_response() if isinstance(auth_response, Response): return auth_response class_type = get_doc().collections[path]["collection"].class_.title if checkClassOp(class_type, "GET"): # Check if class_type supports GET operation try: # Try getting the Item based on ID and Class type response = crud.get(id_, class_type, api_name=get_api_name(), session=get_session()) return set_response_headers( jsonify(hydrafy(response, path=path))) except (ClassNotFound, InstanceNotFound) as e: status_code, message = e.get_HTTP() return set_response_headers(jsonify(message), status_code=status_code) abort(405)
def get(self, path: str) -> Response: """ Retrieve a collection of items from the database. """ auth_response = check_authentication_response() if isinstance(auth_response, Response): return auth_response endpoint_ = checkEndpoint("GET", path) if endpoint_['method']: # If endpoint and GET method is supported in the API if path in get_doc().collections: # If collection name in document's collections collection = get_doc().collections[path]["collection"] try: # Get collection details from the database response = crud.get_collection(get_api_name(), collection.class_.title, session=get_session(), path=path) return set_response_headers( jsonify(hydrafy(response, path=path))) except ClassNotFound as e: status_code, message = e.get_HTTP() return set_response_headers(jsonify(message), status_code=status_code) # If class is supported elif path in get_doc().parsed_classes and "{}Collection".format( path) not in get_doc().collections: try: class_type = get_doc().parsed_classes[path]['class'].title response = crud.get_single(class_type, api_name=get_api_name(), session=get_session(), path=path) return set_response_headers( jsonify(hydrafy(response, path=path))) except (ClassNotFound, InstanceNotFound) as e: status_code, message = e.get_HTTP() return set_response_headers(jsonify(message), status_code=status_code) abort(endpoint_['status'])
def item_collection_get_response(path: str) -> Response: """ Handles GET operation on item collection classes. :param path: Path for Item Collection :type path: str :return: Appropriate response for the GET operation. :rtype: Response """ search_params = request.args.to_dict() collections, parsed_classes = get_collections_and_parsed_classes() api_name = get_api_name() expanded_base_url = DocUrl.doc_url # If endpoint and GET method is supported in the API and class is supported if path in parsed_classes: abort(405) if path in collections: collection = collections[path]["collection"] class_name = collection.manages["object"].split(expanded_base_url)[1] collection_manages_class = parsed_classes[class_name]["class"] class_type = collection_manages_class.title class_path = collection_manages_class.path try: # Get collection details from the database # create partial function for crud operation crud_response = partial(crud.get_collection, api_name, class_type, session=get_session(), path=path, search_params=search_params, collection=False) if get_pagination(): # Get paginated response response = crud_response(paginate=True, page_size=get_page_size()) else: # Get whole collection response = crud_response(paginate=False) response["search"] = add_iri_template(path=class_path, API_NAME=api_name, collection_path=path) return set_response_headers(jsonify(hydrafy(response, path=path))) except (ClassNotFound, PageNotFound, InvalidSearchParameter, OffsetOutOfRange) as e: error = e.get_HTTP() return error_response(error)
def items_get_check_support(id_, class_type, class_path, path, is_collection=False): """Check if class_type supports GET operation""" try: # Try getting the Item based on ID and Class type response = crud.get( id_, class_type, api_name=get_api_name(), session=get_session(), path=path, collection=is_collection) response = finalize_response(class_path, response) return set_response_headers( jsonify(hydrafy(response, path=path))) except (ClassNotFound, InstanceNotFound) as e: error = e.get_HTTP() return error_response(error)