Refactor: Move response code into function

This commit is contained in:
Unrud 2016-06-10 14:30:58 +02:00
parent 063e827588
commit 54b47c4a3e

View File

@ -218,6 +218,15 @@ class Application:
def __call__(self, environ, start_response): def __call__(self, environ, start_response):
"""Manage a request.""" """Manage a request."""
def response(status, headers={}, answer=None):
# Start response
status = "%i %s" % (status,
client.responses.get(status, "Unknown"))
self.logger.debug("Answer status: %s" % status)
start_response(status, list(headers.items()))
# Return response content
return [answer] if answer else []
self.logger.info("%s request at %s received" % ( self.logger.info("%s request at %s received" % (
environ["REQUEST_METHOD"], environ["PATH_INFO"])) environ["REQUEST_METHOD"], environ["PATH_INFO"]))
headers = pprint.pformat(self.headers_log(environ)) headers = pprint.pformat(self.headers_log(environ))
@ -234,9 +243,7 @@ class Application:
# Request path not starting with base_prefix, not allowed # Request path not starting with base_prefix, not allowed
self.logger.debug( self.logger.debug(
"Path not starting with prefix: %s", environ["PATH_INFO"]) "Path not starting with prefix: %s", environ["PATH_INFO"])
status, headers, _ = NOT_ALLOWED return response(*NOT_ALLOWED)
start_response(status, list(headers.items()))
return []
# Sanitize request URI # Sanitize request URI
environ["PATH_INFO"] = storage.sanitize_path( environ["PATH_INFO"] = storage.sanitize_path(
@ -275,10 +282,7 @@ class Application:
status = client.SEE_OTHER status = client.SEE_OTHER
self.logger.info("/.well-known/ redirection to: %s" % redirect) self.logger.info("/.well-known/ redirection to: %s" % redirect)
headers = {"Location": redirect} headers = {"Location": redirect}
status = "%i %s" % ( return response(status, headers)
status, client.responses.get(status, "Unknown"))
start_response(status, list(headers.items()))
return []
is_authenticated = self.is_authenticated(user, password) is_authenticated = self.is_authenticated(user, password)
is_valid_user = is_authenticated or not user is_valid_user = is_authenticated or not user
@ -342,13 +346,7 @@ class Application:
for key in self.configuration.options("headers"): for key in self.configuration.options("headers"):
headers[key] = self.configuration.get("headers", key) headers[key] = self.configuration.get("headers", key)
# Start response return response(status, headers, answer)
status = "%i %s" % (status, client.responses.get(status, "Unknown"))
self.logger.debug("Answer status: %s" % status)
start_response(status, list(headers.items()))
# Return response content
return [answer] if answer else []
# All these functions must have the same parameters, some are useless # All these functions must have the same parameters, some are useless
# pylint: disable=W0612,W0613,R0201 # pylint: disable=W0612,W0613,R0201