Skip to content

Adding an HTTPMethod str enum to http in stdlib #91996

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

Closed
cibofo opened this issue Apr 27, 2022 · 1 comment
Closed

Adding an HTTPMethod str enum to http in stdlib #91996

cibofo opened this issue Apr 27, 2022 · 1 comment
Assignees
Labels
type-feature A feature request or enhancement

Comments

@cibofo
Copy link
Contributor

cibofo commented Apr 27, 2022

Feature or enhancement

Adding an HTTPMethod str enum to http in stdlib

Pitch

The same as http.HTTPStatus but for http methods.
It can be helpful when dealing with the need to explicitly write the http method name, for example, when using urllib3.

When writing HTTP related code its common to explicitly write HTTP methods names as magic strings which are prone to mistypes and errors.

For example a simple search in amazon's botocore (HTTP related code, https://github.com/boto/botocore) for the string 'GET' shows 77 references and a search for 'POST' shows 43 references.

Example of code now:
client.request('GET', url, headers=headers)

Illustration of the same code with the new feature:
client.request(HTTPMethod.GET, url, headers=headers)

Which is much safer and more comfortable.

You can see real examples of this when searching for urllib3 examples https://www.programcreek.com/python/example/72024/urllib3

In reality, in many projects, we just create the HTTPMethod enum ourselves which is just an unnecessary duplication of code.

The implementation should follow the appropriate HTTP RFCs listed below:
https://datatracker.ietf.org/doc/html/rfc7231#section-4
https://datatracker.ietf.org/doc/html/rfc5789#section-2

@cibofo cibofo added the type-feature A feature request or enhancement label Apr 27, 2022
@cibofo
Copy link
Contributor Author

cibofo commented Apr 27, 2022

PR #91997

@ethanfurman ethanfurman self-assigned this Apr 27, 2022
ethanfurman added a commit that referenced this issue May 5, 2022
* Add HTTPMethod enum to http

Create a StrEnum for the 9 common HTTP methods.

Co-authored-by: Ethan Furman <ethan@stoneleaf.us>
@cibofo cibofo closed this as completed May 5, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
type-feature A feature request or enhancement
Projects
None yet
Development

No branches or pull requests

2 participants