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
51 changes: 47 additions & 4 deletions Doc/library/http.rst
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,8 @@ HyperText Transfer Protocol:
* :mod:`http.cookies` has utilities for implementing state management with cookies
* :mod:`http.cookiejar` provides persistence of cookies

:mod:`http` is also a module that defines a number of HTTP status codes and
associated messages through the :class:`http.HTTPStatus` enum:

The :mod:`http` module also defines the following enums that help you work with http related code:

.. class:: HTTPStatus

Expand Down Expand Up @@ -53,8 +53,8 @@ HTTP status codes
-----------------

Supported,
`IANA-registered <https://www.iana.org/assignments/http-status-codes/http-status-codes.xhtml>`_
status codes available in :class:`http.HTTPStatus` are:
`IANA-registered status codes <https://www.iana.org/assignments/http-status-codes/http-status-codes.xhtml>`_
available in :class:`http.HTTPStatus` are:

======= =================================== ==================================================================
Code Enum Name Details
Expand Down Expand Up @@ -136,3 +136,46 @@ equal to the constant name (i.e. ``http.HTTPStatus.OK`` is also available as

.. versionadded:: 3.9
Added ``103 EARLY_HINTS``, ``418 IM_A_TEAPOT`` and ``425 TOO_EARLY`` status codes.

.. class:: HTTPMethod

.. versionadded:: 3.11

A subclass of :class:`enum.StrEnum` that defines a set of HTTP methods and descriptions written in English.

Usage::

>>> from http import HTTPMethod
>>> HTTMethod.GET
HTTMethod.GET
>>> HTTMethod.GET == 'GET'
True
>>> HTTMethod.GET.value
'GET'
>>> HTTMethod.GET.description
'Transfer a current representation of the target resource.'
>>> list(HTTPMethod)
[HTTPMethod.GET, HTTPMethod.HEAD, ...]

.. _http-methods:

HTTP methods
-----------------

Supported,
`IANA-registered methods <https://www.iana.org/assignments/http-methods/http-methods.xhtml>`_
available in :class:`http.HTTPMethod` are:

=========== =================================== ==================================================================
Method Enum Name Details
=========== =================================== ==================================================================
``GET`` ``GET`` HTTP/1.1 :rfc:`7231`, Section 4.3.1
``HEAD`` ``HEAD`` HTTP/1.1 :rfc:`7231`, Section 4.3.2
``POST`` ``POST`` HTTP/1.1 :rfc:`7231`, Section 4.3.3
``PUT`` ``PUT`` HTTP/1.1 :rfc:`7231`, Section 4.3.4
``DELETE`` ``DELETE`` HTTP/1.1 :rfc:`7231`, Section 4.3.5
``CONNECT`` ``CONNECT`` HTTP/1.1 :rfc:`7231`, Section 4.3.6
``OPTIONS`` ``OPTIONS`` HTTP/1.1 :rfc:`7231`, Section 4.3.7
``TRACE`` ``TRACE`` HTTP/1.1 :rfc:`7231`, Section 4.3.8
``PATCH`` ``PATCH`` HTTP/1.1 :rfc:`5789`
=========== =================================== ==================================================================
33 changes: 31 additions & 2 deletions Lib/http/__init__.py
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,32 @@ 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

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.'
1 change: 1 addition & 0 deletions Misc/ACKS
Original file line number Diff line number Diff line change
Expand Up @@ -2004,6 +2004,7 @@ Arnaud Ysmal
Bernard Yue
Moshe Zadka
Bader Zaidan
Yair Zak
Elias Zamaria
Milan Zamazal
Artur Zaprzala
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
New http.HTTPMethod enum to represent all the available HTTP request methods in a convenient way