Skip to content

Commit 9a0a7b4

Browse files
cibofoethanfurman
andauthored
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>
1 parent bb35d65 commit 9a0a7b4

File tree

4 files changed

+80
-6
lines changed

4 files changed

+80
-6
lines changed

Doc/library/http.rst

+47-4
Original file line numberDiff line numberDiff line change
@@ -21,8 +21,8 @@ HyperText Transfer Protocol:
2121
* :mod:`http.cookies` has utilities for implementing state management with cookies
2222
* :mod:`http.cookiejar` provides persistence of cookies
2323

24-
:mod:`http` is also a module that defines a number of HTTP status codes and
25-
associated messages through the :class:`http.HTTPStatus` enum:
24+
25+
The :mod:`http` module also defines the following enums that help you work with http related code:
2626

2727
.. class:: HTTPStatus
2828

@@ -53,8 +53,8 @@ HTTP status codes
5353
-----------------
5454

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

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

137137
.. versionadded:: 3.9
138138
Added ``103 EARLY_HINTS``, ``418 IM_A_TEAPOT`` and ``425 TOO_EARLY`` status codes.
139+
140+
.. class:: HTTPMethod
141+
142+
.. versionadded:: 3.11
143+
144+
A subclass of :class:`enum.StrEnum` that defines a set of HTTP methods and descriptions written in English.
145+
146+
Usage::
147+
148+
>>> from http import HTTPMethod
149+
>>> HTTMethod.GET
150+
HTTMethod.GET
151+
>>> HTTMethod.GET == 'GET'
152+
True
153+
>>> HTTMethod.GET.value
154+
'GET'
155+
>>> HTTMethod.GET.description
156+
'Transfer a current representation of the target resource.'
157+
>>> list(HTTPMethod)
158+
[HTTPMethod.GET, HTTPMethod.HEAD, ...]
159+
160+
.. _http-methods:
161+
162+
HTTP methods
163+
-----------------
164+
165+
Supported,
166+
`IANA-registered methods <https://www.iana.org/assignments/http-methods/http-methods.xhtml>`_
167+
available in :class:`http.HTTPMethod` are:
168+
169+
=========== =================================== ==================================================================
170+
Method Enum Name Details
171+
=========== =================================== ==================================================================
172+
``GET`` ``GET`` HTTP/1.1 :rfc:`7231`, Section 4.3.1
173+
``HEAD`` ``HEAD`` HTTP/1.1 :rfc:`7231`, Section 4.3.2
174+
``POST`` ``POST`` HTTP/1.1 :rfc:`7231`, Section 4.3.3
175+
``PUT`` ``PUT`` HTTP/1.1 :rfc:`7231`, Section 4.3.4
176+
``DELETE`` ``DELETE`` HTTP/1.1 :rfc:`7231`, Section 4.3.5
177+
``CONNECT`` ``CONNECT`` HTTP/1.1 :rfc:`7231`, Section 4.3.6
178+
``OPTIONS`` ``OPTIONS`` HTTP/1.1 :rfc:`7231`, Section 4.3.7
179+
``TRACE`` ``TRACE`` HTTP/1.1 :rfc:`7231`, Section 4.3.8
180+
``PATCH`` ``PATCH`` HTTP/1.1 :rfc:`5789`
181+
=========== =================================== ==================================================================

Lib/http/__init__.py

+31-2
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
1-
from enum import IntEnum, _simple_enum
1+
from enum import StrEnum, IntEnum, _simple_enum
22

3-
__all__ = ['HTTPStatus']
3+
__all__ = ['HTTPStatus', 'HTTPMethod']
44

55

66
@_simple_enum(IntEnum)
@@ -149,3 +149,32 @@ def __new__(cls, value, phrase, description=''):
149149
NETWORK_AUTHENTICATION_REQUIRED = (511,
150150
'Network Authentication Required',
151151
'The client needs to authenticate to gain network access')
152+
153+
154+
@_simple_enum(StrEnum)
155+
class HTTPMethod:
156+
"""HTTP methods and descriptions
157+
158+
Methods from the following RFCs are all observed:
159+
160+
* RFC 7231: Hypertext Transfer Protocol (HTTP/1.1), obsoletes 2616
161+
* RFC 5789: PATCH Method for HTTP
162+
"""
163+
def __new__(cls, value, description):
164+
obj = str.__new__(cls, value)
165+
obj._value_ = value
166+
obj.description = description
167+
return obj
168+
169+
def __repr__(self):
170+
return "<%s.%s>" % (self.__class__.__name__, self._name_)
171+
172+
CONNECT = 'CONNECT', 'Establish a connection to the server.'
173+
DELETE = 'DELETE', 'Remove the target.'
174+
GET = 'GET', 'Retrieve the target.'
175+
HEAD = 'HEAD', 'Same as GET, but only retrieve the status line and header section.'
176+
OPTIONS = 'OPTIONS', 'Describe the communication options for the target.'
177+
PATCH = 'PATCH', 'Apply partial modifications to a target.'
178+
POST = 'POST', 'Perform target-specific processing with the request payload.'
179+
PUT = 'PUT', 'Replace the target with the request payload.'
180+
TRACE = 'TRACE', 'Perform a message loop-back test along the path to the target.'

Misc/ACKS

+1
Original file line numberDiff line numberDiff line change
@@ -2004,6 +2004,7 @@ Arnaud Ysmal
20042004
Bernard Yue
20052005
Moshe Zadka
20062006
Bader Zaidan
2007+
Yair Zak
20072008
Elias Zamaria
20082009
Milan Zamazal
20092010
Artur Zaprzala
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
New http.HTTPMethod enum to represent all the available HTTP request methods in a convenient way

0 commit comments

Comments
 (0)