Skip to content

Response helper

create_success(data=None, message=CREATED_MESSAGE)

Generate a JSON response with a 201 status code (Created).

Parameters:

Name Type Description Default
data dict[Any, Any] | None

Optional data to include in the response body.

None
message str

Optional message to include in the response.

CREATED_MESSAGE

Returns:

Type Description
Response

The JSON response.

Source code in hestia/web/response_helper.py
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
def create_success(
    data: dict[Any, Any] | None = None, message: str = CREATED_MESSAGE
) -> Response:
    """
    Generate a JSON response with a 201 status code (Created).

    :param data: Optional data to include in the response body.
    :param message: Optional message to include in the response.
    :return: The JSON response.
    """
    if data is None:
        return JSONResponse(
            content={"message": message}, status_code=status.HTTP_201_CREATED
        )
    else:
        return JSONResponse(
            content={"data": data, "message": message},
            status_code=status.HTTP_201_CREATED,
        )

forbidden_error(message=FORBIDDEN_MESSAGE)

Generate a JSON response with a 403 status code (Forbidden).

Parameters:

Name Type Description Default
message str

Optional message to include in the response.

FORBIDDEN_MESSAGE

Returns:

Type Description
Response

The JSON response.

Source code in hestia/web/response_helper.py
68
69
70
71
72
73
74
75
76
77
def forbidden_error(message: str = FORBIDDEN_MESSAGE) -> Response:
    """
    Generate a JSON response with a 403 status code (Forbidden).

    :param message: Optional message to include in the response.
    :return: The JSON response.
    """
    return JSONResponse(
        content={"message": message}, status_code=status.HTTP_403_FORBIDDEN
    )

found(location)

Generate a redirect response with a 302 status code (Found).

Parameters:

Name Type Description Default
location str

The URL to redirect to.

required

Returns:

Type Description
RedirectResponse

The RedirectResponse.

Source code in hestia/web/response_helper.py
102
103
104
105
106
107
108
109
def found(location: str) -> RedirectResponse:
    """
    Generate a redirect response with a 302 status code (Found).

    :param location: The URL to redirect to.
    :return: The RedirectResponse.
    """
    return RedirectResponse(url=location, status_code=status.HTTP_302_FOUND)

moved_permanently(location)

Generate a redirect response with a 301 status code (Moved Permanently).

Parameters:

Name Type Description Default
location str

The URL to redirect to.

required

Returns:

Type Description
RedirectResponse

The RedirectResponse.

Source code in hestia/web/response_helper.py
92
93
94
95
96
97
98
99
def moved_permanently(location: str) -> RedirectResponse:
    """
    Generate a redirect response with a 301 status code (Moved Permanently).

    :param location: The URL to redirect to.
    :return: The RedirectResponse.
    """
    return RedirectResponse(url=location, status_code=status.HTTP_301_MOVED_PERMANENTLY)

not_found_error(message=NOT_FOUND_MESSAGE)

Generate a JSON response with a 404 status code (Not Found).

Parameters:

Name Type Description Default
message str

Optional message to include in the response.

NOT_FOUND_MESSAGE

Returns:

Type Description
Response

The JSON response.

Source code in hestia/web/response_helper.py
56
57
58
59
60
61
62
63
64
65
def not_found_error(message: str = NOT_FOUND_MESSAGE) -> Response:
    """
    Generate a JSON response with a 404 status code (Not Found).

    :param message: Optional message to include in the response.
    :return: The JSON response.
    """
    return JSONResponse(
        content={"message": message}, status_code=status.HTTP_404_NOT_FOUND
    )

success(data=None, message=SUCCESS_MESSAGE)

Generate a JSON response with a 200 status code (OK).

Parameters:

Name Type Description Default
data dict[Any, Any] | None

Optional data to include in the response body.

None
message str

Optional message to include in the response.

SUCCESS_MESSAGE

Returns:

Type Description
Response

The JSON response.

Source code in hestia/web/response_helper.py
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
def success(
    data: dict[Any, Any] | None = None, message: str = SUCCESS_MESSAGE
) -> Response:
    """
    Generate a JSON response with a 200 status code (OK).

    :param data: Optional data to include in the response body.
    :param message: Optional message to include in the response.
    :return: The JSON response.
    """
    if data is None:
        return JSONResponse(
            content={"message": message}, status_code=status.HTTP_200_OK
        )
    else:
        return JSONResponse(
            content={"data": data, "message": message}, status_code=status.HTTP_200_OK
        )

unauthorized_error(message=UNAUTHORIZED_MESSAGE)

Generate a JSON response with a 401 status code (Unauthorized).

Parameters:

Name Type Description Default
message str

Optional message to include in the response.

UNAUTHORIZED_MESSAGE

Returns:

Type Description
Response

The JSON response.

Source code in hestia/web/response_helper.py
80
81
82
83
84
85
86
87
88
89
def unauthorized_error(message: str = UNAUTHORIZED_MESSAGE) -> Response:
    """
    Generate a JSON response with a 401 status code (Unauthorized).

    :param message: Optional message to include in the response.
    :return: The JSON response.
    """
    return JSONResponse(
        content={"message": message}, status_code=status.HTTP_401_UNAUTHORIZED
    )