Coverage for /pythoncovmergedfiles/medio/medio/usr/local/lib/python3.8/site-packages/httpx/_status_codes.py: 89%
97 statements
« prev ^ index » next coverage.py v7.2.7, created at 2023-06-07 07:19 +0000
« prev ^ index » next coverage.py v7.2.7, created at 2023-06-07 07:19 +0000
1from enum import IntEnum
4class codes(IntEnum):
5 """HTTP status codes and reason phrases
7 Status codes from the following RFCs are all observed:
9 * RFC 7231: Hypertext Transfer Protocol (HTTP/1.1), obsoletes 2616
10 * RFC 6585: Additional HTTP Status Codes
11 * RFC 3229: Delta encoding in HTTP
12 * RFC 4918: HTTP Extensions for WebDAV, obsoletes 2518
13 * RFC 5842: Binding Extensions to WebDAV
14 * RFC 7238: Permanent Redirect
15 * RFC 2295: Transparent Content Negotiation in HTTP
16 * RFC 2774: An HTTP Extension Framework
17 * RFC 7540: Hypertext Transfer Protocol Version 2 (HTTP/2)
18 * RFC 2324: Hyper Text Coffee Pot Control Protocol (HTCPCP/1.0)
19 * RFC 7725: An HTTP Status Code to Report Legal Obstacles
20 * RFC 8297: An HTTP Status Code for Indicating Hints
21 * RFC 8470: Using Early Data in HTTP
22 """
24 def __new__(cls, value: int, phrase: str = "") -> "codes":
25 obj = int.__new__(cls, value)
26 obj._value_ = value
28 obj.phrase = phrase # type: ignore[attr-defined]
29 return obj
31 def __str__(self) -> str:
32 return str(self.value)
34 @classmethod
35 def get_reason_phrase(cls, value: int) -> str:
36 try:
37 return codes(value).phrase # type: ignore
38 except ValueError:
39 return ""
41 @classmethod
42 def is_informational(cls, value: int) -> bool:
43 """
44 Returns `True` for 1xx status codes, `False` otherwise.
45 """
46 return 100 <= value <= 199
48 @classmethod
49 def is_success(cls, value: int) -> bool:
50 """
51 Returns `True` for 2xx status codes, `False` otherwise.
52 """
53 return 200 <= value <= 299
55 @classmethod
56 def is_redirect(cls, value: int) -> bool:
57 """
58 Returns `True` for 3xx status codes, `False` otherwise.
59 """
60 return 300 <= value <= 399
62 @classmethod
63 def is_client_error(cls, value: int) -> bool:
64 """
65 Returns `True` for 4xx status codes, `False` otherwise.
66 """
67 return 400 <= value <= 499
69 @classmethod
70 def is_server_error(cls, value: int) -> bool:
71 """
72 Returns `True` for 5xx status codes, `False` otherwise.
73 """
74 return 500 <= value <= 599
76 @classmethod
77 def is_error(cls, value: int) -> bool:
78 """
79 Returns `True` for 4xx or 5xx status codes, `False` otherwise.
80 """
81 return 400 <= value <= 599
83 # informational
84 CONTINUE = 100, "Continue"
85 SWITCHING_PROTOCOLS = 101, "Switching Protocols"
86 PROCESSING = 102, "Processing"
87 EARLY_HINTS = 103, "Early Hints"
89 # success
90 OK = 200, "OK"
91 CREATED = 201, "Created"
92 ACCEPTED = 202, "Accepted"
93 NON_AUTHORITATIVE_INFORMATION = 203, "Non-Authoritative Information"
94 NO_CONTENT = 204, "No Content"
95 RESET_CONTENT = 205, "Reset Content"
96 PARTIAL_CONTENT = 206, "Partial Content"
97 MULTI_STATUS = 207, "Multi-Status"
98 ALREADY_REPORTED = 208, "Already Reported"
99 IM_USED = 226, "IM Used"
101 # redirection
102 MULTIPLE_CHOICES = 300, "Multiple Choices"
103 MOVED_PERMANENTLY = 301, "Moved Permanently"
104 FOUND = 302, "Found"
105 SEE_OTHER = 303, "See Other"
106 NOT_MODIFIED = 304, "Not Modified"
107 USE_PROXY = 305, "Use Proxy"
108 TEMPORARY_REDIRECT = 307, "Temporary Redirect"
109 PERMANENT_REDIRECT = 308, "Permanent Redirect"
111 # client error
112 BAD_REQUEST = 400, "Bad Request"
113 UNAUTHORIZED = 401, "Unauthorized"
114 PAYMENT_REQUIRED = 402, "Payment Required"
115 FORBIDDEN = 403, "Forbidden"
116 NOT_FOUND = 404, "Not Found"
117 METHOD_NOT_ALLOWED = 405, "Method Not Allowed"
118 NOT_ACCEPTABLE = 406, "Not Acceptable"
119 PROXY_AUTHENTICATION_REQUIRED = 407, "Proxy Authentication Required"
120 REQUEST_TIMEOUT = 408, "Request Timeout"
121 CONFLICT = 409, "Conflict"
122 GONE = 410, "Gone"
123 LENGTH_REQUIRED = 411, "Length Required"
124 PRECONDITION_FAILED = 412, "Precondition Failed"
125 REQUEST_ENTITY_TOO_LARGE = 413, "Request Entity Too Large"
126 REQUEST_URI_TOO_LONG = 414, "Request-URI Too Long"
127 UNSUPPORTED_MEDIA_TYPE = 415, "Unsupported Media Type"
128 REQUESTED_RANGE_NOT_SATISFIABLE = 416, "Requested Range Not Satisfiable"
129 EXPECTATION_FAILED = 417, "Expectation Failed"
130 IM_A_TEAPOT = 418, "I'm a teapot"
131 MISDIRECTED_REQUEST = 421, "Misdirected Request"
132 UNPROCESSABLE_ENTITY = 422, "Unprocessable Entity"
133 LOCKED = 423, "Locked"
134 FAILED_DEPENDENCY = 424, "Failed Dependency"
135 TOO_EARLY = 425, "Too Early"
136 UPGRADE_REQUIRED = 426, "Upgrade Required"
137 PRECONDITION_REQUIRED = 428, "Precondition Required"
138 TOO_MANY_REQUESTS = 429, "Too Many Requests"
139 REQUEST_HEADER_FIELDS_TOO_LARGE = 431, "Request Header Fields Too Large"
140 UNAVAILABLE_FOR_LEGAL_REASONS = 451, "Unavailable For Legal Reasons"
142 # server errors
143 INTERNAL_SERVER_ERROR = 500, "Internal Server Error"
144 NOT_IMPLEMENTED = 501, "Not Implemented"
145 BAD_GATEWAY = 502, "Bad Gateway"
146 SERVICE_UNAVAILABLE = 503, "Service Unavailable"
147 GATEWAY_TIMEOUT = 504, "Gateway Timeout"
148 HTTP_VERSION_NOT_SUPPORTED = 505, "HTTP Version Not Supported"
149 VARIANT_ALSO_NEGOTIATES = 506, "Variant Also Negotiates"
150 INSUFFICIENT_STORAGE = 507, "Insufficient Storage"
151 LOOP_DETECTED = 508, "Loop Detected"
152 NOT_EXTENDED = 510, "Not Extended"
153 NETWORK_AUTHENTICATION_REQUIRED = 511, "Network Authentication Required"
156# Include lower-case styles for `requests` compatibility.
157for code in codes:
158 setattr(codes, code._name_.lower(), int(code))