Reference¶
The HTTP status code helpers are framework-neutral and live in
dsg_lib.web_common.http_codes. dsg_lib.robyn_functions.http_codes re-exports
them. For Robyn's responses= route argument, use the description-only form:
from robyn import Robyn
from dsg_lib.robyn_functions import http_codes
app = Robyn(__file__)
@app.get("/items/:item_id", responses=http_codes.generate_code_dict([404, 422], description_only=True))
def get_item(request):
...
dsg_lib.web_common.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.web_common 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 |
Author: Mike Ryan Date: 2024/05/16 License: MIT
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.web_common 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.web_common 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.web_common 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.web_common 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.web_common 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 |
Dict[int, Union[str, Dict[str, str]]]
|
A dictionary where each key is an HTTP error code from the input |
Dict[int, Union[str, Dict[str, str]]]
|
list and each value depends on the description_only parameter. If |
|
Dict[int, Union[str, Dict[str, str]]]
|
description_only is True, the value is the description string. If |
|
Dict[int, Union[str, Dict[str, str]]]
|
description_only is False, the value is a dictionary with keys |
|
Dict[int, Union[str, Dict[str, str]]]
|
'description', 'extended_description', and 'link'. |
Example:
from dsg_lib.web_common 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/web_common/http_codes.py
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 102 103 104 105 106 107 108 109 110 111 112 | |