def setup(self): self.logging_data_map = LoggingDataMap() self.ns_pizza = Namespace( subject="pizza", version="v1", ) self.ns_pizza_orders = Namespace(subject="pizza", version="v1", object_="order") self.retrieve_operation = Operation.Retrieve self.search_operation = Operation.Search self.retrieve_for_operation = Operation.RetrieveFor self.retrieve_function_name = "retrieve" self.search_function_name = "search" self.create_operation = Operation.Create self.create_for_operation = Operation.CreateFor self.create_function_name = "create" self.logging_data_map.add_entry( self.ns_pizza, self.retrieve_operation.value, self.retrieve_function_name ) self.logging_data_map.add_entry( self.ns_pizza_orders, self.retrieve_for_operation.value, self.retrieve_function_name ) self.logging_data_map.add_entry( self.ns_pizza, self.search_operation.value, self.search_function_name ) self.logging_data_map.add_entry( self.ns_pizza, self.create_operation.value, self.create_function_name ) self.logging_data_map.add_entry( self.ns_pizza_orders, self.create_for_operation.value, self.create_function_name )
def test_create_path_for_create_collection_operation(): pizza_namespace = Namespace( subject=Pizza, version="v1", ) operation = Operation.CreateCollection.value api_path = pizza_namespace.path_for_operation(operation) assert api_path == "/api/v1/pizza"
def test_create_path_for_delete_operation(): pizza_namespace = Namespace( subject=Pizza, version="v1", ) operation = Operation.Delete.value api_path = pizza_namespace.path_for_operation(operation) assert api_path == "/api/v1/pizza/{pizza_id}"
def test_create_path_for_search_for_operation(): pizza_namespace = Namespace( subject=Pizza, version="v1", object_=Waiter, ) operation = Operation.SearchFor.value api_path = pizza_namespace.path_for_operation(operation) assert api_path == "/api/v1/pizza/{pizza_id}/waiter"
def add_entry(self, namespace: Namespace, operation: OperationInfo, function_name: str): operation_name = namespace.generate_operation_name_for_logging( operation) key = self._generate_key_from_namespace_and_operation( namespace, operation) self.data_map[key] = LoggingInfo(operation_name, function_name)
def base_fixture(self): pizza_ns = Namespace( subject=Pizza, version="v1", ) sn = SimpleNamespace(ns=pizza_ns) return sn
def base_fixture(self, test_graph): person_ns = Namespace(subject=Person, version="v1") configure_crud(test_graph, person_ns, PERSON_MAPPINGS) sn = SimpleNamespace( person_id_1=PERSON_ID_1, person_id_2=PERSON_ID_2, base_url="/api/v1/person", ) return sn
def configure_crud( graph, namespace: Namespace, mappings: Dict[Operation, Callable], response_model_exclude_none: bool = False, ): """ Mounts the supported namespace operations into the FastAPI graph, following our conventions for setting up URL patterns. :param mappings: Dict[ Operation: function ] """ for op, fn in mappings.items(): operation = op.value # Configuration params for this swagger endpoint # Some are generated dynamically depending on the specific configurations # and user definitions, which we add next configuration = dict( operation_id=operation.name, status_code=operation.default_code, response_model_exclude_none=response_model_exclude_none, ) # Construct the unique path for this operation & object namespace url_path = namespace.path_for_operation(operation) method_mapping = { "GET": graph.app.get, "POST": graph.app.post, "PATCH": graph.app.patch, "DELETE": graph.app.delete, "OPTIONS": graph.app.options, "HEAD": graph.app.head, "TRACE": graph.app.trace, } try: graph.logging_data_map.add_entry(namespace, operation, fn.__name__) fn = graph.session_injection(fn) method_mapping[operation.method](url_path, **configuration)(fn) except FastAPIError as e: raise ValueError( f"Error configuring endpoint: {url_path} {operation.method}: {e}" )
def base_fixture(self, test_graph): test_graph.use("audit_middleware", ) options = AuditOptions( include_request_body_status=True, include_response_body_status=True, include_path=True, include_query_string=True, log_as_debug=False, ) person_ns = Namespace(subject=Person, version="v1") configure_crud(test_graph, person_ns, PERSON_MAPPINGS) sn = SimpleNamespace( person_id_1=PERSON_ID_1, person_id_2=PERSON_ID_2, base_url="/api/v1/person", options=options, ) return sn