summaryrefslogtreecommitdiff
path: root/talermerchantdemos/httpcommon
diff options
context:
space:
mode:
Diffstat (limited to 'talermerchantdemos/httpcommon')
-rw-r--r--talermerchantdemos/httpcommon/__init__.py50
1 files changed, 18 insertions, 32 deletions
diff --git a/talermerchantdemos/httpcommon/__init__.py b/talermerchantdemos/httpcommon/__init__.py
index 650228d..443160b 100644
--- a/talermerchantdemos/httpcommon/__init__.py
+++ b/talermerchantdemos/httpcommon/__init__.py
@@ -7,15 +7,6 @@ import time
from flask_babel import gettext
##
-# 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", **params)
- flask.abort(flask.make_response(t, abort_status_code))
-
-##
# POST a request to the backend, and return a error
# response if any error occurs.
#
@@ -28,9 +19,7 @@ def backend_post(backend_url, endpoint, json):
final_url = urljoin(backend_url, endpoint)
print("POSTing to: " + final_url)
try:
- resp = requests.post(
- final_url, json=json, headers=headers
- )
+ resp = requests.post(final_url, json=json, headers=headers)
except requests.ConnectionError:
err_abort(500, message=gettext("Could not establish connection to backend"))
try:
@@ -39,14 +28,14 @@ def backend_post(backend_url, endpoint, json):
err_abort(
500,
message=gettext("Could not parse response from backend"),
- status_code=resp.status_code
+ status_code=resp.status_code,
)
if resp.status_code != 200:
err_abort(
500,
message=gettext("Backend returned error status"),
json=response_json,
- status_code=resp.status_code
+ status_code=resp.status_code,
)
print("Backend responds to {}: {}".format(final_url, str(response_json)))
return response_json
@@ -64,9 +53,7 @@ def backend_get(backend_url, endpoint, params):
final_url = urljoin(backend_url, endpoint)
print("GETting: " + final_url + " with params: " + str(params))
try:
- resp = requests.get(
- final_url, params=params, headers=headers
- )
+ resp = requests.get(final_url, params=params, headers=headers)
except requests.ConnectionError:
err_abort(500, message=gettext("Could not establish connection to backend"))
try:
@@ -78,19 +65,21 @@ def backend_get(backend_url, endpoint, params):
500,
message=gettext("Backend returned error status"),
json=response_json,
- status_code=resp.status_code
+ status_code=resp.status_code,
)
print("Backend responds to {}: {}".format(final_url, str(response_json)))
return response_json
+
def get_locale():
- parts = request.path.split('/', 2)
- if (2 >= len(parts)):
+ parts = request.path.split("/", 2)
+ if 2 >= len(parts):
# Totally unexpected path format, do not localize
return "en"
lang = parts[1]
return lang
+
##
# Helper function used inside Jinja2 logic to create a links
# to the current page but in a different language. Used to
@@ -102,8 +91,8 @@ def self_localized(lang):
"""
path = request.path
# path must have the form "/$LANG/$STUFF"
- parts = path.split('/', 2)
- if (2 >= len(parts)):
+ parts = path.split("/", 2)
+ if 2 >= len(parts):
# Totally unexpected path format, do not localize
return path
return "/" + lang + "/" + parts[2]
@@ -115,30 +104,27 @@ def self_localized(lang):
# @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
- )
+ 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
+
def isExpired(self):
if self.value == "never":
return False
now = int(round(time.time()) * 1000)
now_dt = datetime.fromtimestamp(now / 1000)
deadline_dt = datetime.fromtimestamp(self.value / 1000)
- print("debug: checking refund expiration, now: {}, deadline: {}".format(
- now_dt.strftime("%c"), deadline_dt.strftime("%c")
- ))
+ print(
+ "debug: checking refund expiration, now: {}, deadline: {}".format(
+ now_dt.strftime("%c"), deadline_dt.strftime("%c")
+ )
+ )
return now > self.value