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_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_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"
Esempio n. 4
0
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}"
            )