1- from enum import IntEnum
1+ from enum import StrEnum , IntEnum , _simple_enum
22
3- __all__ = ['HTTPStatus' ]
3+ __all__ = ['HTTPStatus' , 'HTTPMethod' ]
44
55
6- class HTTPStatus (IntEnum ):
6+ @_simple_enum (IntEnum )
7+ class HTTPStatus :
78 """HTTP status codes and reason phrases
89
910 Status codes from the following RFCs are all observed:
1011
11- * RFC 7231: Hypertext Transfer Protocol (HTTP/1.1), obsoletes 2616
12+ * RFC 9110: HTTP Semantics, obsoletes 7231, which obsoleted 2616
1213 * RFC 6585: Additional HTTP Status Codes
1314 * RFC 3229: Delta encoding in HTTP
1415 * RFC 4918: HTTP Extensions for WebDAV, obsoletes 2518
@@ -25,11 +26,30 @@ class HTTPStatus(IntEnum):
2526 def __new__ (cls , value , phrase , description = '' ):
2627 obj = int .__new__ (cls , value )
2728 obj ._value_ = value
28-
2929 obj .phrase = phrase
3030 obj .description = description
3131 return obj
3232
33+ @property
34+ def is_informational (self ):
35+ return 100 <= self <= 199
36+
37+ @property
38+ def is_success (self ):
39+ return 200 <= self <= 299
40+
41+ @property
42+ def is_redirection (self ):
43+ return 300 <= self <= 399
44+
45+ @property
46+ def is_client_error (self ):
47+ return 400 <= self <= 499
48+
49+ @property
50+ def is_server_error (self ):
51+ return 500 <= self <= 599
52+
3353 # informational
3454 CONTINUE = 100 , 'Continue' , 'Request received, please continue'
3555 SWITCHING_PROTOCOLS = (101 , 'Switching Protocols' ,
@@ -94,22 +114,25 @@ def __new__(cls, value, phrase, description=''):
94114 'Client must specify Content-Length' )
95115 PRECONDITION_FAILED = (412 , 'Precondition Failed' ,
96116 'Precondition in headers is false' )
97- REQUEST_ENTITY_TOO_LARGE = (413 , 'Request Entity Too Large' ,
98- 'Entity is too large' )
99- REQUEST_URI_TOO_LONG = (414 , 'Request-URI Too Long' ,
117+ CONTENT_TOO_LARGE = (413 , 'Content Too Large' ,
118+ 'Content is too large' )
119+ REQUEST_ENTITY_TOO_LARGE = CONTENT_TOO_LARGE
120+ URI_TOO_LONG = (414 , 'URI Too Long' ,
100121 'URI is too long' )
122+ REQUEST_URI_TOO_LONG = URI_TOO_LONG
101123 UNSUPPORTED_MEDIA_TYPE = (415 , 'Unsupported Media Type' ,
102124 'Entity body in unsupported format' )
103- REQUESTED_RANGE_NOT_SATISFIABLE = (416 ,
104- 'Requested Range Not Satisfiable' ,
125+ RANGE_NOT_SATISFIABLE = (416 , 'Range Not Satisfiable' ,
105126 'Cannot satisfy request range' )
127+ REQUESTED_RANGE_NOT_SATISFIABLE = RANGE_NOT_SATISFIABLE
106128 EXPECTATION_FAILED = (417 , 'Expectation Failed' ,
107129 'Expect condition could not be satisfied' )
108130 IM_A_TEAPOT = (418 , 'I\' m a Teapot' ,
109131 'Server refuses to brew coffee because it is a teapot.' )
110132 MISDIRECTED_REQUEST = (421 , 'Misdirected Request' ,
111133 'Server is not able to produce a response' )
112- UNPROCESSABLE_ENTITY = 422 , 'Unprocessable Entity'
134+ UNPROCESSABLE_CONTENT = 422 , 'Unprocessable Content'
135+ UNPROCESSABLE_ENTITY = UNPROCESSABLE_CONTENT
113136 LOCKED = 423 , 'Locked'
114137 FAILED_DEPENDENCY = 424 , 'Failed Dependency'
115138 TOO_EARLY = 425 , 'Too Early'
@@ -148,3 +171,32 @@ def __new__(cls, value, phrase, description=''):
148171 NETWORK_AUTHENTICATION_REQUIRED = (511 ,
149172 'Network Authentication Required' ,
150173 'The client needs to authenticate to gain network access' )
174+
175+
176+ @_simple_enum (StrEnum )
177+ class HTTPMethod :
178+ """HTTP methods and descriptions
179+
180+ Methods from the following RFCs are all observed:
181+
182+ * RFC 9110: HTTP Semantics, obsoletes 7231, which obsoleted 2616
183+ * RFC 5789: PATCH Method for HTTP
184+ """
185+ def __new__ (cls , value , description ):
186+ obj = str .__new__ (cls , value )
187+ obj ._value_ = value
188+ obj .description = description
189+ return obj
190+
191+ def __repr__ (self ):
192+ return "<%s.%s>" % (self .__class__ .__name__ , self ._name_ )
193+
194+ CONNECT = 'CONNECT' , 'Establish a connection to the server.'
195+ DELETE = 'DELETE' , 'Remove the target.'
196+ GET = 'GET' , 'Retrieve the target.'
197+ HEAD = 'HEAD' , 'Same as GET, but only retrieve the status line and header section.'
198+ OPTIONS = 'OPTIONS' , 'Describe the communication options for the target.'
199+ PATCH = 'PATCH' , 'Apply partial modifications to a target.'
200+ POST = 'POST' , 'Perform target-specific processing with the request payload.'
201+ PUT = 'PUT' , 'Replace the target with the request payload.'
202+ TRACE = 'TRACE' , 'Perform a message loop-back test along the path to the target.'
0 commit comments