Uh oh!
There was an error while loading.Please reload this page.
- Notifications
You must be signed in to change notification settings - Fork33.7k
Closed
Description
Feature or enhancement
Add five properties tohttp.HTTPStatus that indicate the category the status belongs to:
classHTTPStatus: ...@propertydefis_informational(self):return100<=self<200@propertydefis_success(self):return200<=self<300@propertydefis_redirection(self):return300<=self<400@propertydefis_client_error(self):return400<=self<500@propertydefis_server_error(self):return500<=self<600
Pitch
Programs that deal with HTTP requests usually need to deal with the various status codes. Oftentimes, knowing the broad category is sufficient to make quick decisions. For example, a client needs to handle responses differently depending on the status code. Assumingstatus is anHTTPStatus, instead of writing:
if200<=status<300:handle_response(response)elif300<=status<400:handle_redirect(response)elif400<=status<500:raiseClientError(status)elif500<=status<600:maybe_retry(response.request)
One could more elegantly write:
ifstatus.is_success:handle_response(response)elifstatus.is_redirection:handle_redirect(response)elifstatus.is_client_error:raiseClientError(status)elifstatus.is_server_error:maybe_retry(response.request)
Enums are safer, more readable and more expressive than magic numbers. I think these small additions would only strengthenHTTPStatus.
Let me know if this interests you, and I can open a PR.