summaryrefslogtreecommitdiff
path: root/talermerchantdemos/httpcommon/__init__.py
diff options
context:
space:
mode:
authorFlorian Dold <florian@dold.me>2021-04-21 21:07:18 +0200
committerFlorian Dold <florian@dold.me>2021-04-21 21:07:18 +0200
commit2b84f3bd5de3a500b63193924bea2f3dfd0c9afd (patch)
tree08d5a2bd354ab7849d731eabe7b132429b6d2225 /talermerchantdemos/httpcommon/__init__.py
parent4356540b2eb12b2c307270566b0b4ce969e12c60 (diff)
downloadtaler-merchant-demos-2b84f3bd5de3a500b63193924bea2f3dfd0c9afd.tar.gz
taler-merchant-demos-2b84f3bd5de3a500b63193924bea2f3dfd0c9afd.tar.bz2
taler-merchant-demos-2b84f3bd5de3a500b63193924bea2f3dfd0c9afd.zip
common base template, new logo and lots of cleanup
Diffstat (limited to 'talermerchantdemos/httpcommon/__init__.py')
-rw-r--r--talermerchantdemos/httpcommon/__init__.py65
1 files changed, 33 insertions, 32 deletions
diff --git a/talermerchantdemos/httpcommon/__init__.py b/talermerchantdemos/httpcommon/__init__.py
index 443160b..39cd696 100644
--- a/talermerchantdemos/httpcommon/__init__.py
+++ b/talermerchantdemos/httpcommon/__init__.py
@@ -6,6 +6,16 @@ from datetime import datetime
import time
from flask_babel import gettext
+
+class BackendException(Exception):
+ """Exception for failed communication with the Taler merchant backend"""
+
+ def __init__(self, message, backend_status=None, backend_json=None):
+ super().__init__(message)
+ self.backend_status = backend_status
+ self.backend_json = backend_json
+
+
##
# POST a request to the backend, and return a error
# response if any error occurs.
@@ -14,28 +24,30 @@ from flask_babel import gettext
# this request.
# @param json the POST's body.
# @return the backend response (JSON format).
-def backend_post(backend_url, endpoint, json):
- headers = {"Authorization": "ApiKey sandbox"}
+def backend_post(backend_url, endpoint, json, auth_token=None):
+ headers = dict()
+ if auth_token is not None:
+ headers["Authorization"] = "Bearer " + auth_token
final_url = urljoin(backend_url, endpoint)
print("POSTing to: " + final_url)
try:
resp = requests.post(final_url, json=json, headers=headers)
except requests.ConnectionError:
- err_abort(500, message=gettext("Could not establish connection to backend"))
+ raise BackendException(
+ message=gettext("Could not establish connection to backend")
+ )
try:
response_json = resp.json()
except ValueError:
- err_abort(
- 500,
+ raise BackendException(
message=gettext("Could not parse response from backend"),
- status_code=resp.status_code,
+ backend_status=resp.status_code,
)
if resp.status_code != 200:
- err_abort(
- 500,
+ raise BackendException(
message=gettext("Backend returned error status"),
- json=response_json,
- status_code=resp.status_code,
+ backend_status=resp.status_code,
+ backend_json=response_json,
)
print("Backend responds to {}: {}".format(final_url, str(response_json)))
return response_json
@@ -48,24 +60,27 @@ def backend_post(backend_url, endpoint, json):
# @param params (dict type of) URL parameters to append to the request.
# @return the JSON response from the backend, or a error response
# if something unexpected happens.
-def backend_get(backend_url, endpoint, params):
- headers = {"Authorization": "ApiKey sandbox"}
+def backend_get(backend_url, endpoint, params, auth_token=None):
+ headers = dict()
+ if auth_token is not None:
+ headers["Authorization"] = "Bearer " + auth_token
final_url = urljoin(backend_url, endpoint)
print("GETting: " + final_url + " with params: " + str(params))
try:
resp = requests.get(final_url, params=params, headers=headers)
except requests.ConnectionError:
- err_abort(500, message=gettext("Could not establish connection to backend"))
+ raise BackendException(
+ message=gettext("Could not establish connection to backend")
+ )
try:
response_json = resp.json()
except ValueError:
- err_abort(500, message=gettext("Could not parse response from backend"))
+ raise BackendException(message=gettext("Could not parse response from backend"))
if resp.status_code != 200:
- err_abort(
- 500,
+ raise BackendException(
message=gettext("Backend returned error status"),
- json=response_json,
- status_code=resp.status_code,
+ backend_status=resp.status_code,
+ backend_json=response_json,
)
print("Backend responds to {}: {}".format(final_url, str(response_json)))
return response_json
@@ -98,20 +113,6 @@ def self_localized(lang):
return "/" + lang + "/" + parts[2]
-##
-# Return a error response to the client.
-#
-# @param abort_status_code status code to return along the response.
-# @param params _kw_ arguments to passed verbatim to the templating engine.
-def err_abort(abort_status_code, **params):
- t = flask.render_template("templates/error.html.j2", lang=get_locale(), **params)
- flask.abort(flask.make_response(t, abort_status_code))
-
-
-def fallback_404(error):
- return "Page not found"
-
-
class Deadline:
def __init__(self, value):
self.value = value