File size: 2,135 Bytes
ef1ad9e
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
from typing import Any, Dict


def to_dict(model_instance) -> dict[str, Any]:
    """

    Returns a dictionary representation of a model instance.



    Includes all attributes defined on the model class.



    Args:

        model_instance: The model instance to convert to a dictionary.



    Returns

    -------

        A dictionary containing all attributes and their values.

    """
    return {c.name: getattr(model_instance, c.name) for c in model_instance.__table__.columns}


def create_swagger_response(success_data, error_response_list=None):
    """

    Create a Swagger response example for both success and error responses.



    - **success_data**: Example data to include in the success response.

    - **error_response_list**: List of error responses to include in the Swagger documentation.



    Returns a dictionary representing the success and error responses.

    """
    # Initialize with the success response
    responses = {200: {"content": {"application/json": {"example": success_data}}}}

    # Add each error response from the list
    if error_response_list:
        for error_response in error_response_list:
            responses.update(error_response)

    return responses


def create_swagger_redirect_response(description: str, example_url: str) -> Dict[str, Any]:
    """

    Creates a Swagger response dictionary for redirect responses.



    Args:

        description (str): A description of the redirect response.

        example_url (str): An example URL that demonstrates the redirect.



    Returns

    -------

        Dict[str, Any]: A dictionary for Swagger response documentation.

    """
    return {
        "307": {
            "description": description,
            "headers": {
                "Location": {
                    "description": "The URL to which the client is redirected.",
                    "schema": {"type": "string", "example": example_url},
                }
            },
            "content": {"text/plain": {"schema": {"type": "string", "example": f"Redirecting to {example_url}"}}},
        }
    }