8000 gh-91996: Adding an HTTPMethod StrEnum to http by cibofo · Pull Request #91997 · python/cpython · GitHub
[go: up one dir, main page]

Skip to content

gh-91996: Adding an HTTPMethod StrEnum to http #91997

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 10 commits into from
May 5, 2022
Next Next commit
Added HTTPMethod enum to http
  • Loading branch information
cibofo authored and ethanfurman committed May 5, 2022
commit 9362a1898a36fcfa17ef8405d5fee149a2cba242
31 changes: 29 additions & 2 deletions Lib/http/__init__.py
View file Open in desktop
Original file line number Diff line number Diff line change
@@ -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)
Expand Down Expand Up @@ -149,3 +149,30 @@ def __new__(cls, value, phrase, description=''):
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

GET = 'GET', 'Transfer a current representation of the target resource.'

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

These should be in some sort of order, probably alphabetical, possibly grouped by safety: GET, HEAD, OPTIONS / DELETE, PATCH, POST, PUT / CONNECT, TRACE.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I used the same order from the RFCs, this is the usual order they are sorted (e.g. https://developer.mozilla.org/en-US/docs/Web/HTTP/Methods)

HEAD = 'HEAD', 'Same as GET, but only transfer the status line and header section.'
POST = 'POST', 'Perform resource-specific processing on the request payload.'
PUT = 'PUT', 'Replace all current representations of the target resource with the request payload.'
DELETE = 'DELETE', 'Remove all current representations of the target resource.'
CONNECT = 'CONNECT', 'Establish a tunnel to the server identified by the target resource.'
OPTIONS = 'OPTIONS', 'Describe the communication options for the target resource.'
TRACE = 'TRACE', 'Perform a message loop-back test along the path to the target resource.'
PATCH = 'PATCH', 'Apply partial modifications to a resource.'
0