Forked from digitaldavenyc/gist:5b6e9bea03633d62c517
Created
August 31, 2014 04:56
-
-
Save pwwilson/177468529e312cf02c4e to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def status_code_behavior(r): | |
""" | |
Holds a dictionary of all possible HTTP status codes and returns a string value. | |
:param code: HTTP Response status code | |
:return: {String} | |
""" | |
# Copied from multiple sources... | |
# https://developer.mozilla.org/en-US/docs/Web/HTTP/Response_codes | |
# http://httpstatus.es/ | |
# http://www.w3.org/Protocols/rfc2616/rfc2616-sec10.html | |
# https://www.iana.org/assignments/http-status-codes/http-status-codes.xhtml | |
codes = { | |
200: (SAVE, 'Continue'), # The request has succeeded. | |
201: (ERROR, 'Created'), # The request has succeeded and a new resource has been created as a result of it. typically the response sent after a PUT request. | |
202: (ERROR, 'Accepted'), # The request has been received but not yet acted upon. | |
203: (SAVE, 'Non-Authoritative Information'), # This response code means returned meta-information set is not exact set as available from the origin server, but collected from a local or a third party copy. | |
204: (SAVE, 'No Content'), # There is no content to send for this request, but the headers may be useful. The user-agent may update its cached headers for this resource with the new ones. | |
205: (SAVE, 'Reset Content'), # This response code is sent after accomplishing request to tell user agent reset document view which sent this request. | |
206: (ERROR, 'Partial Content'), # This response code is used because of range header sent by the client to separate download into multiple streams. | |
207: (ERROR, 'Multi-Status'), # A Multi-Status response conveys information about multiple resources in situations where multiple status codes might be appropriate. | |
208: (ERROR, 'Already Reported'), # Results previously returned | |
226: (ERROR, 'IM Used'), # Request fulfilled, response is instance-manipulations | |
300: (REDIRECT, 'Multiple Choices'), # The request has more than one possible responses. User-agent or user should choose one of them. There is no standardized way to choose one of the responses. | |
301: (REDIRECT, 'Moved Permanently'), # This response code means that URI of requested resource has been changed. Probably, new URI would be given in the response. | |
302: (REDIRECT, 'Found'), # This response code means that URI of requested resource has been changed temporarily. New changes in the URI might be made in the future. | |
303: (REDIRECT, 'See Other'), # Server sent this response to directing client to get requested resource to another URI with an GET request. | |
304: (SAVE, 'Not Modified'), # This is used for caching purposes. It is telling to client that response has not been modified. | |
305: (ERROR, 'Use Proxy'), # This means requested response must be accessed by a proxy. This response code is not largely supported because security reasons. | |
306: (ERROR, 'Unused'), # This response code is no longer used, it is just reserved currently. It was used in a previous version of the HTTP 1.1 specification. | |
307: (REDIRECT, 'Temporary Redirect'), # Server sent this response to directing client to get requested resource to another URI with same method that used prior request. | |
308: (REDIRECT, 'Permanent Redirect'), # This means that the resource is now permanently located at another URI, specified by the Location: HTTP Response header. | |
400: (ERROR, 'Bad Request'), # This response means that server could not understand the request due to invalid syntax. | |
401: (ERROR, 'Unauthorized'), # Authentication is needed to get requested response. This is similar to 403, but in this case, authentication is possible. | |
402: (ERROR, 'Payment Required'), # This response code is reserved for future use. Initial aim for creating this code was using it for digital payment systems. | |
403: (ERROR, 'Forbidden'), # Client does not have access rights to the content so server is rejecting to give proper response. | |
404: (ERROR, 'Not Found'), # Server can not find requested resource. This response code probably is most famous one due to its frequency to occur in web. | |
405: (ERROR, 'Method Not Allowed'), # The request method is known by the server but has been disabled and cannot be used. | |
406: (ERROR, 'Not Acceptable'), # This response is sent when the web server, after performing server-driven content negotiation, doesn't find any content following the criteria given by the user agent. | |
407: (ERROR, 'Proxy Authentication Required'), # This is similar to 401 but authentication is needed to be done by a proxy. | |
408: (ERROR, 'Request Timeout'), # This response is sent on an idle connection by some servers, even without any previous request by the client. | |
409: (ERROR, 'Conflict'), # This response would be sent when a request conflict with current state of server. | |
410: (ERROR, 'Gone'), # This response would be sent when requested content has been deleted from server. | |
411: (ERROR, 'Length Required'), # Server rejected the request because the Content-Length header field is not defined and the server requires it. | |
412: (ERROR, 'Precondition Failed'), # The client has indicated preconditions in its headers which the server does not meet. | |
413: (ERROR, 'Payload Too Large'), # Request entity is larger than limits defined by server; the server might close the connection or return an Retry-After header field. | |
414: (ERROR, 'URI Too Long'), # The URI requested by the client is too long for the server to handle. | |
415: (ERROR, 'Unsupported Media Type'), # The media format of the requested data is not supported by the server, so the server is rejecting the request. | |
416: (ERROR, 'Range Not Satisfiable'), # The range specified by the Range header field in the request can't be fulfilled; it's possible that the range is outside the size of the target URI's data. | |
417: (ERROR, 'Expectation Failed'), # This response code means the expectation indicated by the Expect request header field can't be met by the server. | |
422: (ERROR, 'Unprocessable Entity'), # Request unable to be followed due to semantic errors. | |
423: (ERROR, 'Locked'), # Resource that is being accessed is locked. | |
424: (ERROR, 'Failed Dependency'), # Failed due to failure of a previous request. | |
426: (ERROR, 'Upgrade Required'), # Client should switch to a different protocol. | |
428: (ERROR, 'Precondition Required'), # Origin server requires the request to be conditional. | |
429: (ERROR, 'Too Many Requests'), # User has sent too many requests in a given amount of time | |
431: (ERROR, 'Request Header Fields Too Large'), # Server is unwilling to process the request | |
500: (ERROR, 'Bad Request'), # The server has encountered a situation it doesn't know how to handle. | |
501: (ERROR, 'Not Implemented'), # The request method is not supported by the server and cannot be handled. | |
502: (ERROR, 'Bad Gateway'), # This error response means that the server, while working as a gateway to get a response needed to handle the request, got an invalid response. | |
503: (ERROR, 'Service Unavailable'), # The server is not ready to handle the request. Common causes are a server that is down for maintenance or that is overloaded | |
504: (ERROR, 'Gateway Timeout'), # This error response is given when the server is acting as a gateway and cannot get a response in time. | |
505: (ERROR, 'HTTP Version Not Supported'), # The HTTP version used in the request is not supported by the server. | |
506: (ERROR, 'Variant Also Negotiates'), # Content negotiation for the request results in a circular reference | |
507: (ERROR, 'Insufficient Storage'), # Server is unable to store the representation | |
508: (ERROR, 'Loop Detected'), # Server detected an infinite loop while processing the request | |
509: (ERROR, 'Limit Exceeded'), # Bandwidth limit exceeded | |
510: (ERROR, 'Not Extended'), # Further extensions to the request are required | |
511: (ERROR, 'Network Authentication Required'), # Client needs to authenticate to gain network access | |
} | |
try: | |
action = codes[r.status_code] | |
code = r.status_code | |
except (KeyError, AttributeError): | |
action = ERROR | |
code = r.status_code | |
# Return string action from codes dictionary. | |
return action, code |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment