def _log(method, key, **extras): if method in log_actions: extra = "" if extras: extra = "(%s)" % " ".join(["%s=%s" % item for item in extras.items()]) logger.info( "CACHE %s: %s%s" % ( method, key, extra ) )
def handle_response(self, response): ''' Takes a response object, and either returns it, or raises an appropriate exception based on the status code of the response. ''' logger.info("Parsing data from URL: %s %s" % ( response.request.method, response.request.url )) if response.status_code in range(200, 400): # then we cool return response # if we're here, it's an exception logger.error("URL returned an error: %s %s (%s)" % ( response.request.method, response.request.url, response )) # seriously, python, no switch statement? if response.status_code == 400: raise exceptions.BadRequest(response) elif response.status_code == 401: raise exceptions.Unauthorized(response) elif response.status_code == 403: raise exceptions.Forbidden(response) elif response.status_code == 404: raise exceptions.NotFound(response) elif response.status_code == 405: raise exceptions.MethodNotAllowed(response) elif response.status_code == 409: raise exceptions.Conflict(response) elif response.status_code == 422: raise exceptions.Invalid(response) elif response.status_code in range(400, 500): raise exceptions.ClientError(response) elif response.status_code in range(500, 600): raise exceptions.ServerError(response) else: raise exceptions.ResponseError( response, "Unhandled status code: %s" % response.status_code )