Utils#
HTTP utilities for the core_https library.
This module provides HTTP constants and utilities that maintain backward compatibility with older Python versions. It includes comprehensive HTTP status codes and methods that match the standard library implementations introduced in Python 3.11.
Key features: - Complete HTTP status code enumeration with descriptions - Standard HTTP method definitions with documentation - Backward compatibility with Python < 3.11 - Utility methods for status code and method lookup - Type-safe enum implementations
Example
Basic usage of HTTP status codes:
from core_https.utils import HTTPStatus, HTTPMethod
# Access status codes
print(HTTPStatus.OK.code) # 200
print(HTTPStatus.OK.description) # "OK"
# Find status by code
status = HTTPStatus.by_code(404)
print(status.description) # "Not Found"
# Get all status codes as dictionary
status_map = HTTPStatus.as_dict()
# {200: "OK", 404: "Not Found", ...}
HTTP methods usage:
# Access HTTP methods
print(HTTPMethod.GET.value) # "GET"
print(HTTPMethod.POST.description) # "Perform resource-specific processing..."
# Find method by name
method = HTTPMethod.by_name("patch")
print(method.value) # "PATCH"
See also
HTTP/1.1 Status Codes: https://tools.ietf.org/html/rfc7231#section-6
HTTP Methods: https://tools.ietf.org/html/rfc7231#section-4
- class core_https.utils.HTTPStatus(*values)[source]#
Bases:
EnumHTTP status code enumeration with backward compatibility.
This enum provides comprehensive HTTP status codes with their standard descriptions. It maintains compatibility with Python versions prior to 3.11 where http.HTTPStatus was introduced.
Each status code includes both the numeric code and the standard description as defined in RFC specifications.
- `code`
The numeric HTTP status code (e.g., 200, 404, 500)
- `description`
The standard description for the status code
Example
Using HTTP status codes:
# Access specific status codes status = HTTPStatus.OK print(f"Code: {status.code}") # Code: 200 print(f"Description: {status.description}") # Description: OK # Find status by code not_found = HTTPStatus.by_code(404) print(not_found.description) # Not Found # Check if status indicates success if HTTPStatus.CREATED.is_success(): print("Request succeeded!") # Get all status codes all_codes = HTTPStatus.as_dict() # {100: 'Continue', 200: 'OK', 404: 'Not Found', ...}
- CONTINUE = 100#
- SWITCHING_PROTOCOLS = 101#
- PROCESSING = 102#
- EARLY_HINTS = 103#
- OK = 200#
- CREATED = 201#
- ACCEPTED = 202#
- NON_AUTHORITATIVE_INFORMATION = 203#
- NO_CONTENT = 204#
- RESET_CONTENT = 205#
- PARTIAL_CONTENT = 206#
- MULTI_STATUS = 207#
- ALREADY_REPORTED = 208#
- IM_USED = 226#
- MULTIPLE_CHOICES = 300#
- MOVED_PERMANENTLY = 301#
- FOUND = 302#
- SEE_OTHER = 303#
- NOT_MODIFIED = 304#
- USE_PROXY = 305#
- TEMPORARY_REDIRECT = 307#
- PERMANENT_REDIRECT = 308#
- BAD_REQUEST = 400#
- UNAUTHORIZED = 401#
- PAYMENT_REQUIRED = 402#
- FORBIDDEN = 403#
- NOT_FOUND = 404#
- METHOD_NOT_ALLOWED = 405#
- NOT_ACCEPTABLE = 406#
- PROXY_AUTHENTICATION_REQUIRED = 407#
- REQUEST_TIMEOUT = 408#
- CONFLICT = 409#
- GONE = 410#
- LENGTH_REQUIRED = 411#
- PRECONDITION_FAILED = 412#
- PAYLOAD_TOO_LARGE = 413#
- URI_TOO_LONG = 414#
- UNSUPPORTED_MEDIA_TYPE = 415#
- RANGE_NOT_SATISFIABLE = 416#
- EXPECTATION_FAILED = 417#
- IM_A_TEAPOT = 418#
- MISDIRECTED_REQUEST = 421#
- UNPROCESSABLE_ENTITY = 422#
- LOCKED = 423#
- FAILED_DEPENDENCY = 424#
- TOO_EARLY = 425#
- UPGRADE_REQUIRED = 426#
- PRECONDITION_REQUIRED = 428#
- TOO_MANY_REQUESTS = 429#
- REQUEST_HEADER_FIELDS_TOO_LARGE = 431#
- UNAVAILABLE_FOR_LEGAL_REASONS = 451#
- INTERNAL_SERVER_ERROR = 500#
- NOT_IMPLEMENTED = 501#
- BAD_GATEWAY = 502#
- SERVICE_UNAVAILABLE = 503#
- GATEWAY_TIMEOUT = 504#
- HTTP_VERSION_NOT_SUPPORTED = 505#
- VARIANT_ALSO_NEGOTIATES = 506#
- INSUFFICIENT_STORAGE = 507#
- LOOP_DETECTED = 508#
- NOT_EXTENDED = 510#
- NETWORK_AUTHENTICATION_REQUIRED = 511#
- property code#
Get the numeric HTTP status code.
- property description#
Get the standard description for this status code.
- classmethod by_code(code: int) HTTPStatus[source]#
Find HTTP status by numeric code.
- Parameters:
code – The numeric HTTP status code to look up
- Returns:
HTTPStatus instance for the given code
- Raises:
ValueError – If no status code matches the given code
Example
status = HTTPStatus.by_code(404) print(status.description) # “Not Found”
- class core_https.utils.HTTPMethod(*values)[source]#
Bases:
EnumHTTP method enumeration with backward compatibility.
This enum provides standard HTTP methods with their descriptions. It maintains compatibility with Python versions prior to 3.11 where http.HTTPMethod was introduced.
Each method includes the string name and a description of its intended purpose as defined in HTTP specifications.
Example
Using HTTP methods:
# Access methods method = HTTPMethod.GET print(method.value) # "GET" print(method.description) # "Retrieve the resource." # Find method by string name post = HTTPMethod.by_name("post") # Case insensitive print(post.value) # "POST" # Check method properties if HTTPMethod.POST.is_idempotent(): print("POST is idempotent") # This won't print if HTTPMethod.GET.is_safe(): print("GET is safe") # This will print
- CONNECT = 'CONNECT'#
- DELETE = 'DELETE'#
- GET = 'GET'#
- HEAD = 'HEAD'#
- OPTIONS = 'OPTIONS'#
- PATCH = 'PATCH'#
- POST = 'POST'#
- PUT = 'PUT'#
- TRACE = 'TRACE'#
- property description#
Get the description of this HTTP method.
- is_safe() bool[source]#
Check if method is considered safe (read-only).
Safe methods are those that do not modify server state.
- is_idempotent() bool[source]#
Check if method is idempotent.
Idempotent methods can be called multiple times with the same result.
- is_cacheable() bool[source]#
Check if responses to this method are typically cacheable.
Note: Cacheability also depends on response headers.
- classmethod by_name(name: str) HTTPMethod[source]#
Find HTTP method by name (case-insensitive).
- Parameters:
name – The HTTP method name to look up
- Returns:
HTTPMethod instance for the given name
- Raises:
ValueError – If no method matches the given name
Example
method = HTTPMethod.by_name(“get”) # Case insensitive print(method.value) # “GET”