gh-91996: Add an HTTPMethod StrEnum to http (GH-91997)

* Add HTTPMethod enum to http

Create a StrEnum for the 9 common HTTP methods.

Co-authored-by: Ethan Furman <ethan@stoneleaf.us>
This commit is contained in:
cibofo 2022-05-06 01:39:02 +03:00 committed by GitHub
parent bb35d6504a
commit 9a0a7b4868
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
4 changed files with 80 additions and 6 deletions

View file

@ -1,6 +1,6 @@
from enum import IntEnum, _simple_enum
from enum import StrEnum, IntEnum, _simple_enum
__all__ = ['HTTPStatus']
__all__ = ['HTTPStatus', 'HTTPMethod']
@_simple_enum(IntEnum)
@ -149,3 +149,32 @@ class HTTPStatus:
NETWORK_AUTHENTICATION_REQUIRED = (511,
'Network Authentication Required',
'The client needs to authenticate to gain network access')
@_simple_enum(StrEnum)
class HTTPMethod:
"""HTTP methods and descriptions
Methods from the following RFCs are all observed:
* RFC 7231: Hypertext Transfer Protocol (HTTP/1.1), obsoletes 2616
* RFC 5789: PATCH Method for HTTP
"""
def __new__(cls, value, description):
obj = str.__new__(cls, value)
obj._value_ = value
obj.description = description
return obj
def __repr__(self):
return "<%s.%s>" % (self.__class__.__name__, self._name_)
CONNECT = 'CONNECT', 'Establish a connection to the server.'
DELETE = 'DELETE', 'Remove the target.'
GET = 'GET', 'Retrieve the target.'
HEAD = 'HEAD', 'Same as GET, but only retrieve the status line and header section.'
OPTIONS = 'OPTIONS', 'Describe the communication options for the target.'
PATCH = 'PATCH', 'Apply partial modifications to a target.'
POST = 'POST', 'Perform target-specific processing with the request payload.'
PUT = 'PUT', 'Replace the target with the request payload.'
TRACE = 'TRACE', 'Perform a message loop-back test along the path to the target.'