From 9362a1898a36fcfa17ef8405d5fee149a2cba242 Mon Sep 17 00:00:00 2001 From: yair zak Date: Wed, 27 Apr 2022 21:20:46 +0300 Subject: [PATCH 01/10] Added HTTPMethod enum to http --- Lib/http/__init__.py | 31 +++++++++++++++++++++++++++++-- 1 file changed, 29 insertions(+), 2 deletions(-) diff --git a/Lib/http/__init__.py b/Lib/http/__init__.py index 8b980e24a5603f..33c6209a63bdbb 100644 --- a/Lib/http/__init__.py +++ b/Lib/http/__init__.py @@ -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,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.' + 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.' From 401cf0e60dac437a180fe2b10048c4f100daa36b Mon Sep 17 00:00:00 2001 From: "blurb-it[bot]" <43283697+blurb-it[bot]@users.noreply.github.com> Date: Wed, 27 Apr 2022 19:45:58 +0000 Subject: [PATCH 02/10] =?UTF-8?q?=F0=9F=93=9C=F0=9F=A4=96=20Added=20by=20b?= =?UTF-8?q?lurb=5Fit.?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../next/Library/2022-04-27-19-45-58.gh-issue-91996.YEEIzk.rst | 1 + 1 file changed, 1 insertion(+) create mode 100644 Misc/NEWS.d/next/Library/2022-04-27-19-45-58.gh-issue-91996.YEEIzk.rst diff --git a/Misc/NEWS.d/next/Library/2022-04-27-19-45-58.gh-issue-91996.YEEIzk.rst b/Misc/NEWS.d/next/Library/2022-04-27-19-45-58.gh-issue-91996.YEEIzk.rst new file mode 100644 index 00000000000000..72d9a597a1a59a --- /dev/null +++ b/Misc/NEWS.d/next/Library/2022-04-27-19-45-58.gh-issue-91996.YEEIzk.rst @@ -0,0 +1 @@ +New http.HTTPMethod enum to represent all the available HTTP request methods in a convenient way From 445fbef00e41d6013879371bf28affb1d7f1e3e7 Mon Sep 17 00:00:00 2001 From: yair zak Date: Wed, 27 Apr 2022 22:54:41 +0300 Subject: [PATCH 03/10] Added new contributor name to ACKS --- Misc/ACKS | 1 + 1 file changed, 1 insertion(+) diff --git a/Misc/ACKS b/Misc/ACKS index 2b9485e7aa80c9..91cd4332d60465 100644 --- a/Misc/ACKS +++ b/Misc/ACKS @@ -2004,6 +2004,7 @@ Arnaud Ysmal Bernard Yue Moshe Zadka Bader Zaidan +Yair Zak Elias Zamaria Milan Zamazal Artur Zaprzala From 7e8bed8a905ac76e54a7b06dbb2a913020c68be1 Mon Sep 17 00:00:00 2001 From: yair zak Date: Fri, 29 Apr 2022 00:06:57 +0300 Subject: [PATCH 04/10] Updated http documentation to include HTTPMethod --- Doc/library/http.rst | 45 ++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 45 insertions(+) diff --git a/Doc/library/http.rst b/Doc/library/http.rst index 1569d504c7f92a..eaac96e1e63057 100644 --- a/Doc/library/http.rst +++ b/Doc/library/http.rst @@ -24,6 +24,9 @@ HyperText Transfer Protocol: :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 .. versionadded:: 3.5 @@ -136,3 +139,45 @@ 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 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`` ``DELETE`` 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` \ No newline at end of file From b7048d5d711b508fc394cf609c928a34b8e807f9 Mon Sep 17 00:00:00 2001 From: yair zak Date: Sat, 30 Apr 2022 12:58:28 +0300 Subject: [PATCH 05/10] Fixed the HTTPMethod table in the http docs --- Doc/library/http.rst | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/Doc/library/http.rst b/Doc/library/http.rst index eaac96e1e63057..55b8bb4c8312cc 100644 --- a/Doc/library/http.rst +++ b/Doc/library/http.rst @@ -169,9 +169,9 @@ Supported, `IANA-registered `_ methods 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 @@ -180,4 +180,5 @@ Method Enum Name Details ``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` \ No newline at end of file +``PATCH`` ``PATCH`` HTTP/1.1 :rfc:`5789` +=========== =================================== ================================================================== \ No newline at end of file From caa960b71755e1f3108923dfb4db119ea8f6df15 Mon Sep 17 00:00:00 2001 From: yair zak Date: Sat, 30 Apr 2022 13:03:10 +0300 Subject: [PATCH 06/10] Removed outdated lines from http docs --- Doc/library/http.rst | 3 --- 1 file changed, 3 deletions(-) diff --git a/Doc/library/http.rst b/Doc/library/http.rst index 55b8bb4c8312cc..5d80f653a7df69 100644 --- a/Doc/library/http.rst +++ b/Doc/library/http.rst @@ -21,9 +21,6 @@ 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: From 4c2d21c690a5ab5beb80b0c2b099bf94f64a9d85 Mon Sep 17 00:00:00 2001 From: yair zak Date: Sat, 30 Apr 2022 13:37:57 +0300 Subject: [PATCH 07/10] Added new line at the end of the http docs --- Doc/library/http.rst | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Doc/library/http.rst b/Doc/library/http.rst index 5d80f653a7df69..3ee3b30152bce2 100644 --- a/Doc/library/http.rst +++ b/Doc/library/http.rst @@ -178,4 +178,4 @@ Method Enum Name Details ``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` -=========== =================================== ================================================================== \ No newline at end of file +=========== =================================== ================================================================== From 783a65ce507965077e3d44798460bdccf21b1fd5 Mon Sep 17 00:00:00 2001 From: yair zak Date: Sat, 30 Apr 2022 17:33:58 +0300 Subject: [PATCH 08/10] Fixed http docs rst error --- Doc/library/http.rst | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/Doc/library/http.rst b/Doc/library/http.rst index 3ee3b30152bce2..f5893574da8fd4 100644 --- a/Doc/library/http.rst +++ b/Doc/library/http.rst @@ -53,8 +53,8 @@ HTTP status codes ----------------- Supported, -`IANA-registered `_ -status codes available in :class:`http.HTTPStatus` are: +`IANA-registered status codes `_ +available in :class:`http.HTTPStatus` are: ======= =================================== ================================================================== Code Enum Name Details @@ -163,8 +163,8 @@ HTTP methods ----------------- Supported, -`IANA-registered `_ -methods available in :class:`http.HTTPMethod` are: +`IANA-registered methods `_ +available in :class:`http.HTTPMethod` are: =========== =================================== ================================================================== Method Enum Name Details From 6c113feb6c0c1b0b6cb8bc877f25619c0986d126 Mon Sep 17 00:00:00 2001 From: Ethan Furman Date: Thu, 5 May 2022 14:39:11 -0700 Subject: [PATCH 09/10] correction and alphabetization --- Doc/library/http.rst | 2 +- Lib/http/__init__.py | 19 +++++++++---------- 2 files changed, 10 insertions(+), 11 deletions(-) diff --git a/Doc/library/http.rst b/Doc/library/http.rst index f5893574da8fd4..5895a41d849bd1 100644 --- a/Doc/library/http.rst +++ b/Doc/library/http.rst @@ -172,7 +172,7 @@ 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`` ``DELETE`` HTTP/1.1 :rfc:`7231`, Section 4.3.4 +``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 diff --git a/Lib/http/__init__.py b/Lib/http/__init__.py index 33c6209a63bdbb..6042ff5c5c50d4 100644 --- a/Lib/http/__init__.py +++ b/Lib/http/__init__.py @@ -163,16 +163,15 @@ class HTTPMethod: 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.' - 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.' + 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.' From f11c291fbd27d3daedb19d7f3becec888b76ca23 Mon Sep 17 00:00:00 2001 From: Ethan Furman Date: Thu, 5 May 2022 14:45:19 -0700 Subject: [PATCH 10/10] change repr to exclude duplicate name --- Lib/http/__init__.py | 3 +++ 1 file changed, 3 insertions(+) diff --git a/Lib/http/__init__.py b/Lib/http/__init__.py index 6042ff5c5c50d4..cd2885dc7757b4 100644 --- a/Lib/http/__init__.py +++ b/Lib/http/__init__.py @@ -166,6 +166,9 @@ def __new__(cls, value, description): 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.'