We don't need these wrapper classes, just call HTTPResponse directly

This commit is contained in:
Alexandre Aubin 2021-03-12 03:14:34 +01:00
parent f7199f7a64
commit f790bde101

View file

@ -252,7 +252,7 @@ class _ActionsMapPlugin(object):
try: try:
kwargs["password"] = request.POST["password"] kwargs["password"] = request.POST["password"]
except KeyError: except KeyError:
raise HTTPBadRequestResponse("Missing password parameter") raise HTTPResponse("Missing password parameter", 400)
kwargs["profile"] = request.POST.get("profile", "default") kwargs["profile"] = request.POST.get("profile", "default")
return callback(**kwargs) return callback(**kwargs)
@ -387,7 +387,7 @@ class _ActionsMapPlugin(object):
self.logout(profile) self.logout(profile)
except: except:
pass pass
raise HTTPUnauthorizedResponse(e.strerror) raise HTTPResponse(e.strerror, 401)
else: else:
# Update dicts with new values # Update dicts with new values
s_tokens[profile] = s_new_token s_tokens[profile] = s_new_token
@ -420,7 +420,7 @@ class _ActionsMapPlugin(object):
if profile not in request.get_cookie( if profile not in request.get_cookie(
"session.tokens", secret=s_secret, default={} "session.tokens", secret=s_secret, default={}
): ):
raise HTTPUnauthorizedResponse(m18n.g("not_logged_in")) raise HTTPResponse(m18n.g("not_logged_in"), 401)
else: else:
del self.secrets[s_id] del self.secrets[s_id]
authenticator = self.actionsmap.get_authenticator_for_profile(profile) authenticator = self.actionsmap.get_authenticator_for_profile(profile)
@ -448,7 +448,7 @@ class _ActionsMapPlugin(object):
wsock = request.environ.get("wsgi.websocket") wsock = request.environ.get("wsgi.websocket")
if not wsock: if not wsock:
raise HTTPErrorResponse(m18n.g("websocket_request_expected")) raise HTTPResponse(m18n.g("websocket_request_expected"), 500)
while True: while True:
item = queue.get() item = queue.get()
@ -484,7 +484,7 @@ class _ActionsMapPlugin(object):
try: try:
ret = self.actionsmap.process(arguments, timeout=30, route=_route) ret = self.actionsmap.process(arguments, timeout=30, route=_route)
except MoulinetteError as e: except MoulinetteError as e:
raise HTTPBadRequestResponse(e) raise moulinette_error_to_http_response(e)
except Exception as e: except Exception as e:
if isinstance(e, HTTPResponse): if isinstance(e, HTTPResponse):
raise e raise e
@ -492,7 +492,7 @@ class _ActionsMapPlugin(object):
tb = traceback.format_exc() tb = traceback.format_exc()
logs = {"route": _route, "arguments": arguments, "traceback": tb} logs = {"route": _route, "arguments": arguments, "traceback": tb}
return HTTPErrorResponse(json_encode(logs)) return HTTPResponse(json_encode(logs), 500)
else: else:
return format_for_response(ret) return format_for_response(ret)
finally: finally:
@ -520,7 +520,7 @@ class _ActionsMapPlugin(object):
] ]
except KeyError: except KeyError:
msg = m18n.g("authentication_required") msg = m18n.g("authentication_required")
raise HTTPUnauthorizedResponse(msg) raise HTTPResponse(msg, 401)
else: else:
return authenticator(token=(s_id, s_token)) return authenticator(token=(s_id, s_token))
@ -546,37 +546,17 @@ class _ActionsMapPlugin(object):
# HTTP Responses ------------------------------------------------------- # HTTP Responses -------------------------------------------------------
def moulinette_error_to_http_response(self):
class HTTPOKResponse(HTTPResponse): content = error.content()
def __init__(self, output=""): if isinstance(content, dict):
super(HTTPOKResponse, self).__init__(output, 200) return HTTPResponse(
json_encode(content),
400,
class HTTPBadRequestResponse(HTTPResponse): headers={"Content-type": "application/json"},
def __init__(self, error=""): )
else:
if isinstance(error, MoulinetteError): return HTTPResponse(content, 400)
content = error.content()
if isinstance(content, dict):
super(HTTPBadRequestResponse, self).__init__(
json_encode(content),
400,
headers={"Content-type": "application/json"},
)
else:
super(HTTPBadRequestResponse, self).__init__(content, 400)
else:
super(HTTPBadRequestResponse, self).__init__(error, 400)
class HTTPUnauthorizedResponse(HTTPResponse):
def __init__(self, output=""):
super(HTTPUnauthorizedResponse, self).__init__(output, 401)
class HTTPErrorResponse(HTTPResponse):
def __init__(self, output=""):
super(HTTPErrorResponse, self).__init__(output, 500)
def format_for_response(content): def format_for_response(content):