Skip to content

Reference

dsg_lib.fastapi_functions.http_codes

http_codes.py

This module provides a dictionary of HTTP status codes and their descriptions.

The dictionary ALL_HTTP_CODES contains the HTTP status codes as keys. Each key maps to another dictionary that contains a description of the status code, an extended description, and a link to its documentation on the Mozilla Developer Network (MDN).

Example:

from dsg_lib.fastapi_functions import http_codes

# Get the description, extended description, and link for HTTP status code 200
status_200 = http_codes.ALL_HTTP_CODES[200]
print(status_200)
# {'description': 'OK', 'extended_description': 'The request has succeeded', 'link': 'https://developer.mozilla.org/en-US/docs/Web/HTTP/Status/200'}

Attributes:

Name Type Description
ALL_HTTP_CODES dict

A dictionary of HTTP status codes. Each key is an

DELETE_CODES = generate_code_dict(common_codes + [202, 204, 205, 409]) module-attribute

DELETE_CODES is a dictionary of HTTP status codes for DELETE requests. It includes all the common codes, plus some additional codes that are specific to DELETE requests.

Example:

from dsg_lib.fastapi_functions import http_codes

# Print the dictionary of HTTP status codes for DELETE requests
print(http_codes.DELETE_CODES)

GET_CODES = generate_code_dict(common_codes + [206, 304, 307, 410, 502]) module-attribute

GET_CODES is a dictionary of HTTP status codes for GET requests. It includes all the common codes, plus some additional codes that are specific to GET requests.

Example:

from dsg_lib.fastapi_functions import http_codes

# Print the dictionary of HTTP status codes for GET requests
print(http_codes.GET_CODES)

PATCH_CODES = generate_code_dict(common_codes + [202, 204, 206, 409, 412, 413]) module-attribute

PATCH_CODES is a dictionary of HTTP status codes for PATCH requests. It includes all the common codes, plus some additional codes that are specific to PATCH requests.

Example:

from dsg_lib.fastapi_functions import http_codes

# Print the dictionary of HTTP status codes for PATCH requests
print(http_codes.PATCH_CODES)

POST_CODES = generate_code_dict(common_codes + [201, 202, 205, 307, 409, 413, 415]) module-attribute

POST_CODES is a dictionary of HTTP status codes for POST requests. It includes all the common codes, plus some additional codes that are specific to POST requests.

Example:

from dsg_lib.fastapi_functions import http_codes

# Print the dictionary of HTTP status codes for POST requests
print(http_codes.POST_CODES)

PUT_CODES = generate_code_dict(common_codes + [202, 204, 206, 409, 412, 413]) module-attribute

PUT_CODES is a dictionary of HTTP status codes for PUT requests. It includes all the common codes, plus some additional codes that are specific to PUT requests.

Example:

from dsg_lib.fastapi_functions import http_codes

# Print the dictionary of HTTP status codes for PUT requests
print(http_codes.PUT_CODES)

generate_code_dict(codes, description_only=False)

Generate a dictionary of specific HTTP error codes from the http_codes dictionary.

This function takes a list of HTTP status codes and an optional boolean flag. If the flag is True, the function returns a dictionary where each key is an HTTP status code from the input list and each value is the corresponding description from the ALL_HTTP_CODES dictionary. If the flag is False, the function returns a dictionary where each key is an HTTP status code from the input list and each value is the corresponding dictionary from the ALL_HTTP_CODES dictionary.

Parameters:

Name Type Description Default
codes list

A list of HTTP status codes.

required
description_only bool

If True, only the description of the codes will be returned.

False

Returns:

Name Type Description
dict

A dictionary where each key is an HTTP error code from the input

list and each value depends on the description_only parameter. If

description_only is True, the value is the description string. If

description_only is False, the value is a dictionary with keys

'description', 'extended_description', and 'link'.

Example:

from dsg_lib.fastapi_functions import http_codes

# Generate a dictionary for HTTP status codes 200 and 404
status_dict = http_codes.generate_code_dict([200, 404])
print(status_dict)
# {200: {'description': 'OK', 'extended_description': 'The request has succeeded', 'link': 'https://developer.mozilla.org/en-US/docs/Web/HTTP/Status/200'},
#  404: {'description': 'Not Found', 'extended_description': 'The requested resource could not be found', 'link': 'https://developer.mozilla.org/en-US/docs/Web/HTTP/Status/404'}}

# Generate a dictionary for HTTP status codes 200 and 404 with only descriptions
status_dict = http_codes.generate_code_dict([200, 404], description_only=True)
print(status_dict)  # {200: 'OK', 404: 'Not Found'}

Source code in dsg_lib/fastapi_functions/http_codes.py
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
def generate_code_dict(codes, description_only=False):
    """
    Generate a dictionary of specific HTTP error codes from the http_codes
    dictionary.

    This function takes a list of HTTP status codes and an optional boolean
    flag. If the flag is True, the function returns a dictionary where each key
    is an HTTP status code from the input list and each value is the
    corresponding description from the ALL_HTTP_CODES dictionary. If the flag is
    False, the function returns a dictionary where each key is an HTTP status
    code from the input list and each value is the corresponding dictionary from
    the ALL_HTTP_CODES dictionary.

    Args:
        codes (list): A list of HTTP status codes.
        description_only (bool, optional): If True, only the description of the codes will be returned.
        Defaults to False.

    Returns:
        dict: A dictionary where each key is an HTTP error code from the input
        list and each value depends on the description_only parameter. If
        description_only is True, the value is the description string. If
        description_only is False, the value is a dictionary with keys
        'description', 'extended_description', and 'link'.

    Example:
    ```python

    from dsg_lib.fastapi_functions import http_codes

    # Generate a dictionary for HTTP status codes 200 and 404
    status_dict = http_codes.generate_code_dict([200, 404])
    print(status_dict)
    # {200: {'description': 'OK', 'extended_description': 'The request has succeeded', 'link': 'https://developer.mozilla.org/en-US/docs/Web/HTTP/Status/200'},
    #  404: {'description': 'Not Found', 'extended_description': 'The requested resource could not be found', 'link': 'https://developer.mozilla.org/en-US/docs/Web/HTTP/Status/404'}}

    # Generate a dictionary for HTTP status codes 200 and 404 with only descriptions
    status_dict = http_codes.generate_code_dict([200, 404], description_only=True)
    print(status_dict)  # {200: 'OK', 404: 'Not Found'}
    ```
    """

    if description_only:
        # Log the operation
        logger.debug(f'description_only is True and returning HTTP codes: {codes}')

        # If description_only is True, return a dictionary where each key is an
        # HTTP error code from the input list and each value is the
        # corresponding description from the ALL_HTTP_CODES dictionary.
        return {
            code: ALL_HTTP_CODES[code]['description'] for code in codes if code in ALL_HTTP_CODES
        }
    else:
        # Log the operation
        logger.debug(f'returning HTTP codes: {codes}')

        # If description_only is False, return a dictionary where each key is an
        # HTTP error code from the input list and each value is the
        # corresponding dictionary from the ALL_HTTP_CODES dictionary.
        return {code: ALL_HTTP_CODES[code] for code in codes if code in ALL_HTTP_CODES}

dsg_lib.fastapi_functions._all_codes

This module contains a dictionary mapping HTTP status codes to their descriptions, extended descriptions, and links to their documentation.

Each key in this dictionary is an HTTP status code, and each value is another dictionary with keys 'description', 'extended_description', and 'link'.

The 'description' key maps to a brief string that describes the HTTP status code. The 'extended_description' key maps to a more detailed explanation of the status code. The 'link' key maps to a string that is a link to the documentation for the HTTP status code.

Example
from dsg_lib.fastapi_functions.http_codes import ALL_HTTP_CODES

# Get the dictionary for HTTP status code 200
status_200 = ALL_HTTP_CODES[200]
print(status_200)
# Output: {'description': 'OK', 'extended_description': 'The request has succeeded.', 'link': 'https://developer.mozilla.org/en-US/docs/Web/HTTP/Status/200'}

# Get the description for HTTP status code 404
description_404 = ALL_HTTP_CODES[404]['description']
print(description_404)  # Output: 'Not Found'

# Get the extended description for HTTP status code 200
extended_description_200 = ALL_HTTP_CODES[200]['extended_description']
print(extended_description_200)  # Output: 'The request has succeeded.'

# Get the link to the documentation for HTTP status code 500
link_500 = ALL_HTTP_CODES[500]['link']
print(link_500)  # Output: 'https://developer.mozilla.org/en-US/docs/Web/HTTP/Status/500'