Skip to content

fl_server_api.openapi

Classes:

Name Description
BasicAuthAllowingTokenAuthInUrlScheme

A class that extends the BasicScheme to allow token authentication in the URL.

CustomAutoSchema

A custom AutoSchema that includes the documented examples from the Docstrings in the description.

Functions:

Name Description
create_error_response

Create an OpenAPI error response.

custom_preprocessing_hook

Hide the "/api/dummy/" endpoint from the OpenAPI schema.

Attributes:

Name Type Description
error_response_403

Generic OpenAPI 403 response.

error_response_404

Generic OpenAPI 404 response.

Attributes

error_response_403 module-attribute

error_response_403 = create_error_response('Unauthorized', 'Unauthorized', 'Authentication credentials were not provided.', 'Do not forget to authorize first!')

Generic OpenAPI 403 response.

error_response_404 module-attribute

error_response_404 = create_error_response('Not found', 'Not found', 'The server cannot find the requested resource.', 'Provide valid request data.')

Generic OpenAPI 404 response.

Classes

BasicAuthAllowingTokenAuthInUrlScheme

Bases: BasicScheme


              flowchart TD
              fl_server_api.openapi.BasicAuthAllowingTokenAuthInUrlScheme[BasicAuthAllowingTokenAuthInUrlScheme]

              

              click fl_server_api.openapi.BasicAuthAllowingTokenAuthInUrlScheme href "" "fl_server_api.openapi.BasicAuthAllowingTokenAuthInUrlScheme"
            

A class that extends the BasicScheme to allow token authentication in the URL.

Attributes:

Name Type Description
priority
target_class
Source code in fl_server_api/openapi.py
class BasicAuthAllowingTokenAuthInUrlScheme(BasicScheme):
    """
    A class that extends the BasicScheme to allow token authentication in the URL.
    """

    target_class = "fl_server_api.views.base.BasicAuthAllowingTokenAuthInUrl"
    priority = 0

Attributes

priority class-attribute instance-attribute
priority = 0
target_class class-attribute instance-attribute
target_class = 'fl_server_api.views.base.BasicAuthAllowingTokenAuthInUrl'

CustomAutoSchema

Bases: AutoSchema


              flowchart TD
              fl_server_api.openapi.CustomAutoSchema[CustomAutoSchema]

              

              click fl_server_api.openapi.CustomAutoSchema href "" "fl_server_api.openapi.CustomAutoSchema"
            

A custom AutoSchema that includes the documented examples from the Docstrings in the description.

Methods:

Name Description
get_description

Get the description of the view including its examples (if desired) formatted as markdown.

get_operation_id

Get the operation ID which is the fully qualified name of the corresponding view/action/method.

Attributes:

Name Type Description
rendering_style

Docstring rendering style.

show_examples

Flag to include examples in the description.

Source code in fl_server_api/openapi.py
class CustomAutoSchema(AutoSchema):
    """
    A custom AutoSchema that includes the documented examples from the Docstrings in the description.
    """

    show_examples = True
    """Flag to include examples in the description."""
    rendering_style = RenderingStyle.CLEAN
    """Docstring rendering style."""

    def _get_docstring(self):
        """
        Get the docstring of the view.

        This method parses the description of the view.

        Returns:
            Docstring: The parsed docstring.
        """
        return parse(super().get_description())

    def _get_param_docstring(self, docstring: Docstring, argument: str) -> Optional[str]:
        """
        Get the docstring of a parameter.

        This method finds the parameter in the docstring and returns its description.

        Args:
            docstring (Docstring): The docstring.
            argument (str): The name of the argument.

        Returns:
            Optional[str]: The description of the argument, or `None` if the argument is not found.
        """
        params = [p for p in docstring.params if p.arg_name == argument]
        if not params:
            return None
        return params[0].description

    def get_description(self):
        """
        Get the description of the view including its examples (if desired) formatted as markdown.

        Returns:
            str: The description of the view as markdown.
        """
        docstring = self._get_docstring()
        tmp_docstring = Docstring(style=docstring.style)
        tmp_docstring.short_description = docstring.short_description
        tmp_docstring.long_description = docstring.long_description
        if self.show_examples:
            tmp_docstring.meta.extend(docstring.examples)
        desc = compose(tmp_docstring, self.rendering_style, indent="")
        if self.show_examples and desc.__contains__("Examples:"):
            # customize examples section:
            # - examples should be in a new paragraph (not concatenated with the description)
            # - the examples header should be a h3 title
            desc = desc.replace("\nExamples:\n", "\n\n### Examples:\n\n")
        desc = cleandoc(desc)
        return desc

    def _resolve_path_parameters(self, variables: List[str]):
        """
        Resolve the path parameters of the view and set their descriptions if they are missing.

        Args:
            variables (List[str]): The list of variables in the path.

        Returns:
            list: The list of path parameters.
        """
        parameters = super()._resolve_path_parameters(variables)
        docstring = self._get_docstring()
        for parameter in parameters:
            if "description" not in parameter:
                description = self._get_param_docstring(docstring, parameter["name"])
                if description:
                    parameter["description"] = description
        return parameters

    def get_operation_id(self):
        """
        Get the operation ID which is the fully qualified name of the corresponding view/action/method.

        Returns:
            str: The operation ID.
        """
        action_or_method = getattr(self.view, getattr(self.view, 'action', self.method.lower()), None)
        return fullname(action_or_method or self.view.__class__)

Attributes

rendering_style class-attribute instance-attribute
rendering_style = CLEAN

Docstring rendering style.

show_examples class-attribute instance-attribute
show_examples = True

Flag to include examples in the description.

Functions

get_description
get_description()

Get the description of the view including its examples (if desired) formatted as markdown.

Returns:

Name Type Description
str

The description of the view as markdown.

Source code in fl_server_api/openapi.py
def get_description(self):
    """
    Get the description of the view including its examples (if desired) formatted as markdown.

    Returns:
        str: The description of the view as markdown.
    """
    docstring = self._get_docstring()
    tmp_docstring = Docstring(style=docstring.style)
    tmp_docstring.short_description = docstring.short_description
    tmp_docstring.long_description = docstring.long_description
    if self.show_examples:
        tmp_docstring.meta.extend(docstring.examples)
    desc = compose(tmp_docstring, self.rendering_style, indent="")
    if self.show_examples and desc.__contains__("Examples:"):
        # customize examples section:
        # - examples should be in a new paragraph (not concatenated with the description)
        # - the examples header should be a h3 title
        desc = desc.replace("\nExamples:\n", "\n\n### Examples:\n\n")
    desc = cleandoc(desc)
    return desc
get_operation_id
get_operation_id()

Get the operation ID which is the fully qualified name of the corresponding view/action/method.

Returns:

Name Type Description
str

The operation ID.

Source code in fl_server_api/openapi.py
def get_operation_id(self):
    """
    Get the operation ID which is the fully qualified name of the corresponding view/action/method.

    Returns:
        str: The operation ID.
    """
    action_or_method = getattr(self.view, getattr(self.view, 'action', self.method.lower()), None)
    return fullname(action_or_method or self.view.__class__)

Functions

create_error_response

create_error_response(response_description: str | None, example_name: str, example_details: str, example_description: str | None, **example_kwargs) -> OpenApiResponse

Create an OpenAPI error response.

Parameters:

Name Type Description Default

response_description

str | None

The description of the response.

required

example_name

str

The name of the example.

required

example_details

str

The details of the example.

required

example_description

str | None

The description of the example.

required

**example_kwargs

Additional keyword arguments for the example.

{}

Returns:

Name Type Description
OpenApiResponse OpenApiResponse

The created OpenAPI response.

Source code in fl_server_api/openapi.py
def create_error_response(
    response_description: Optional[str],
    example_name: str,
    example_details: str,
    example_description: Optional[str],
    **example_kwargs
) -> OpenApiResponse:
    """
    Create an OpenAPI error response.

    Args:
        response_description (Optional[str]): The description of the response.
        example_name (str): The name of the example.
        example_details (str): The details of the example.
        example_description (Optional[str]): The description of the example.
        **example_kwargs: Additional keyword arguments for the example.

    Returns:
        OpenApiResponse: The created OpenAPI response.
    """
    return OpenApiResponse(
        response=ErrorSerializer,
        description=response_description,
        examples=[
            OpenApiExample(
                example_name,
                value={"details": example_details},
                description=example_description,
                **example_kwargs,
            )
        ]
    )

custom_preprocessing_hook

custom_preprocessing_hook(endpoints: list[tuple[str, str, str, Callable]])

Hide the "/api/dummy/" endpoint from the OpenAPI schema.

Parameters:

Name Type Description Default

endpoints

list[tuple[str, str, str, Callable]]

The list of endpoints.

required

Returns:

Name Type Description
Iterator

The filtered list of endpoints.

Source code in fl_server_api/openapi.py
def custom_preprocessing_hook(endpoints: List[Tuple[str, str, str, Callable]]):
    """
    Hide the "/api/dummy/" endpoint from the OpenAPI schema.

    Args:
        endpoints (List[Tuple[str, str, str, Callable]]): The list of endpoints.

    Returns:
        Iterator: The filtered list of endpoints.
    """
    # your modifications to the list of operations that are exposed in the schema
    # for (path, path_regex, method, callback) in endpoints:
    #     pass
    return filter(lambda endpoint: endpoint[0] != "/api/dummy/", endpoints)