status_codes.py
This file
Path
httpx/status_codes.py
131 lines · 4.7 KiB · Python
Last changed by
Renaming -> httpx (#129)Tom Christiebefore adoption2d09d5b
History of this fileTake it
1from enum import IntEnum234class 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 # 301 (Cacheable redirect. Method may change to GET.)39 StatusCode.MOVED_PERMANENTLY,40 # 302 (Uncacheable redirect. Method may change to GET.)41 StatusCode.FOUND,42 # 303 (Client should make a GET or HEAD request.)43 StatusCode.SEE_OTHER,44 # 307 (Equiv. 302, but retain method)45 StatusCode.TEMPORARY_REDIRECT,46 # 308 (Equiv. 301, but retain method)47 StatusCode.PERMANENT_REDIRECT,48 )4950 @classmethod51 def is_client_error(cls, value: int) -> bool:52 return value >= 400 and value <= 4995354 @classmethod55 def is_server_error(cls, value: int) -> bool:56 return value >= 500 and value <= 5995758 # informational59 CONTINUE = 100, "Continue"60 SWITCHING_PROTOCOLS = 101, "Switching Protocols"61 PROCESSING = 102, "Processing"6263 # success64 OK = 200, "OK"65 CREATED = 201, "Created"66 ACCEPTED = 202, "Accepted"67 NON_AUTHORITATIVE_INFORMATION = 203, "Non-Authoritative Information"68 NO_CONTENT = 204, "No Content"69 RESET_CONTENT = 205, "Reset Content"70 PARTIAL_CONTENT = 206, "Partial Content"71 MULTI_STATUS = 207, "Multi-Status"72 ALREADY_REPORTED = 208, "Already Reported"73 IM_USED = 226, "IM Used"7475 # redirection76 MULTIPLE_CHOICES = 300, "Multiple Choices"77 MOVED_PERMANENTLY = 301, "Moved Permanently"78 FOUND = 302, "Found"79 SEE_OTHER = 303, "See Other"80 NOT_MODIFIED = 304, "Not Modified"81 USE_PROXY = 305, "Use Proxy"82 TEMPORARY_REDIRECT = 307, "Temporary Redirect"83 PERMANENT_REDIRECT = 308, "Permanent Redirect"8485 # client error86 BAD_REQUEST = 400, "Bad Request"87 UNAUTHORIZED = 401, "Unauthorized"88 PAYMENT_REQUIRED = 402, "Payment Required"89 FORBIDDEN = 403, "Forbidden"90 NOT_FOUND = 404, "Not Found"91 METHOD_NOT_ALLOWED = 405, "Method Not Allowed"92 NOT_ACCEPTABLE = 406, "Not Acceptable"93 PROXY_AUTHENTICATION_REQUIRED = 407, "Proxy Authentication Required"94 REQUEST_TIMEOUT = 408, "Request Timeout"95 CONFLICT = 409, "Conflict"96 GONE = 410, "Gone"97 LENGTH_REQUIRED = 411, "Length Required"98 PRECONDITION_FAILED = 412, "Precondition Failed"99 REQUEST_ENTITY_TOO_LARGE = 413, "Request Entity Too Large"100 REQUEST_URI_TOO_LONG = 414, "Request-URI Too Long"101 UNSUPPORTED_MEDIA_TYPE = 415, "Unsupported Media Type"102 REQUESTED_RANGE_NOT_SATISFIABLE = 416, "Requested Range Not Satisfiable"103 EXPECTATION_FAILED = 417, "Expectation Failed"104 MISDIRECTED_REQUEST = 421, "Misdirected Request"105 UNPROCESSABLE_ENTITY = 422, "Unprocessable Entity"106 LOCKED = 423, "Locked"107 FAILED_DEPENDENCY = 424, "Failed Dependency"108 UPGRADE_REQUIRED = 426, "Upgrade Required"109 PRECONDITION_REQUIRED = 428, "Precondition Required"110 TOO_MANY_REQUESTS = 429, "Too Many Requests"111 REQUEST_HEADER_FIELDS_TOO_LARGE = 431, "Request Header Fields Too Large"112113 # server errors114 INTERNAL_SERVER_ERROR = 500, "Internal Server Error"115 NOT_IMPLEMENTED = 501, "Not Implemented"116 BAD_GATEWAY = 502, "Bad Gateway"117 SERVICE_UNAVAILABLE = 503, "Service Unavailable"118 GATEWAY_TIMEOUT = 504, "Gateway Timeout"119 HTTP_VERSION_NOT_SUPPORTED = 505, "HTTP Version Not Supported"120 VARIANT_ALSO_NEGOTIATES = 506, "Variant Also Negotiates"121 INSUFFICIENT_STORAGE = 507, "Insufficient Storage"122 LOOP_DETECTED = 508, "Loop Detected"123 NOT_EXTENDED = 510, "Not Extended"124 NETWORK_AUTHENTICATION_REQUIRED = 511, "Network Authentication Required"125126127codes = StatusCode128129# Include lower-case styles for `requests` compatability.130for code in codes:131 setattr(codes, code._name_.lower(), int(code))