Skip to content
Myr

status_codes.py

This file

Path

http3/status_codes.py

126 lines · 4.7 KiB · Python

Last changed by

Int status codes (#92)Tom Christiebefore adoption56a7943

History of this file

Take it

http3 (#86)Tom Christiebefore adoptionc9747aa

lines 1–123why, one step back
1from enum import IntEnum
234class StatusCode(IntEnum):5 """HTTP status codes and reason phrases6 Status codes from the following RFCs are all observed:7 * RFC 7231: Hypertext Transfer Protocol (HTTP/1.1), obsoletes 26168 * RFC 6585: Additional HTTP Status Codes9 * RFC 3229: Delta encoding in HTTP10 * RFC 4918: HTTP Extensions for WebDAV, obsoletes 251811 * RFC 5842: Binding Extensions to WebDAV12 * RFC 7238: Permanent Redirect13 * RFC 2295: Transparent Content Negotiation in HTTP14 * RFC 2774: An HTTP Extension Framework15 * RFC 7540: Hypertext Transfer Protocol Version 2 (HTTP/2)16 """1718 def __new__(cls, value: int, phrase: str = "") -> "StatusCode":19 obj = int.__new__(cls, value) # type: ignore20 obj._value_ = value2122 obj.phrase = phrase23 return obj2425 def __str__(self) -> str:26 return str(self.value)2728 @classmethod29 def get_reason_phrase(cls, value: int) -> str:30 try:31 return StatusCode(value).phrase # type: ignore32 except ValueError:33 return ""3435 @classmethod36 def is_redirect(cls, value: int) -> bool:37 return value in (38 StatusCode.MOVED_PERMANENTLY, # 301 (Cacheable redirect. Method may change to GET.)39 StatusCode.FOUND, # 302 (Uncacheable redirect. Method may change to GET.)40 StatusCode.SEE_OTHER, # 303 (Client should make a GET or HEAD request.)41 StatusCode.TEMPORARY_REDIRECT, # 307 (Equiv. 302, but retain method)42 StatusCode.PERMANENT_REDIRECT, # 308 (Equiv. 301, but retain method)43 )4445 @classmethod46 def is_client_error(cls, value: int) -> bool:47 return value >= 400 and value <= 4994849 @classmethod50 def is_server_error(cls, value: int) -> bool:51 return value >= 500 and value <= 5995253 # informational54 CONTINUE = 100, "Continue"55 SWITCHING_PROTOCOLS = 101, "Switching Protocols"56 PROCESSING = 102, "Processing"5758 # success59 OK = 200, "OK"60 CREATED = 201, "Created"61 ACCEPTED = 202, "Accepted"62 NON_AUTHORITATIVE_INFORMATION = 203, "Non-Authoritative Information"63 NO_CONTENT = 204, "No Content"64 RESET_CONTENT = 205, "Reset Content"65 PARTIAL_CONTENT = 206, "Partial Content"66 MULTI_STATUS = 207, "Multi-Status"67 ALREADY_REPORTED = 208, "Already Reported"68 IM_USED = 226, "IM Used"6970 # redirection71 MULTIPLE_CHOICES = 300, "Multiple Choices"72 MOVED_PERMANENTLY = 301, "Moved Permanently"73 FOUND = 302, "Found"74 SEE_OTHER = 303, "See Other"75 NOT_MODIFIED = 304, "Not Modified"76 USE_PROXY = 305, "Use Proxy"77 TEMPORARY_REDIRECT = 307, "Temporary Redirect"78 PERMANENT_REDIRECT = 308, "Permanent Redirect"7980 # client error81 BAD_REQUEST = 400, "Bad Request"82 UNAUTHORIZED = 401, "Unauthorized"83 PAYMENT_REQUIRED = 402, "Payment Required"84 FORBIDDEN = 403, "Forbidden"85 NOT_FOUND = 404, "Not Found"86 METHOD_NOT_ALLOWED = 405, "Method Not Allowed"87 NOT_ACCEPTABLE = 406, "Not Acceptable"88 PROXY_AUTHENTICATION_REQUIRED = 407, "Proxy Authentication Required"89 REQUEST_TIMEOUT = 408, "Request Timeout"90 CONFLICT = 409, "Conflict"91 GONE = 410, "Gone"92 LENGTH_REQUIRED = 411, "Length Required"93 PRECONDITION_FAILED = 412, "Precondition Failed"94 REQUEST_ENTITY_TOO_LARGE = 413, "Request Entity Too Large"95 REQUEST_URI_TOO_LONG = 414, "Request-URI Too Long"96 UNSUPPORTED_MEDIA_TYPE = 415, "Unsupported Media Type"97 REQUESTED_RANGE_NOT_SATISFIABLE = 416, "Requested Range Not Satisfiable"98 EXPECTATION_FAILED = 417, "Expectation Failed"99 MISDIRECTED_REQUEST = 421, "Misdirected Request"100 UNPROCESSABLE_ENTITY = 422, "Unprocessable Entity"101 LOCKED = 423, "Locked"102 FAILED_DEPENDENCY = 424, "Failed Dependency"103 UPGRADE_REQUIRED = 426, "Upgrade Required"104 PRECONDITION_REQUIRED = 428, "Precondition Required"105 TOO_MANY_REQUESTS = 429, "Too Many Requests"106 REQUEST_HEADER_FIELDS_TOO_LARGE = 431, "Request Header Fields Too Large"107108 # server errors109 INTERNAL_SERVER_ERROR = 500, "Internal Server Error"110 NOT_IMPLEMENTED = 501, "Not Implemented"111 BAD_GATEWAY = 502, "Bad Gateway"112 SERVICE_UNAVAILABLE = 503, "Service Unavailable"113 GATEWAY_TIMEOUT = 504, "Gateway Timeout"114 HTTP_VERSION_NOT_SUPPORTED = 505, "HTTP Version Not Supported"115 VARIANT_ALSO_NEGOTIATES = 506, "Variant Also Negotiates"116 INSUFFICIENT_STORAGE = 507, "Insufficient Storage"117 LOOP_DETECTED = 508, "Loop Detected"118 NOT_EXTENDED = 510, "Not Extended"119 NETWORK_AUTHENTICATION_REQUIRED = 511, "Network Authentication Required"120121122codes = StatusCode123

Multipart support (#90)Tom Christiebefore adoption6a44b05

line 124why, one step back
124#  Include lower-case styles for `requests` compatability.

http3 (#86)Tom Christiebefore adoptionc9747aa

lines 125–126why, one step back
125for code in codes:
126 setattr(codes, code._name_.lower(), int(code))