From 4c364161fabffb95bdf5443037cf67f5ec208aa1 Mon Sep 17 00:00:00 2001 From: MS Date: Wed, 22 Jul 2020 17:08:18 +0200 Subject: factor out http code --- talermerchantdemos/blog/blog.py | 78 +++---------------------------- talermerchantdemos/httpcommon/__init__.py | 63 +++++++++++++++++++++++++ 2 files changed, 70 insertions(+), 71 deletions(-) create mode 100644 talermerchantdemos/httpcommon/__init__.py diff --git a/talermerchantdemos/blog/blog.py b/talermerchantdemos/blog/blog.py index 51a1596..1bea20b 100644 --- a/talermerchantdemos/blog/blog.py +++ b/talermerchantdemos/blog/blog.py @@ -33,6 +33,7 @@ import time from cachelib import UWSGICache, SimpleCache from taler.util.talerconfig import TalerConfig from ..blog.content import ARTICLES, get_article_file, get_image_file +import .backend_get .backend_post BASE_DIR = os.path.dirname(os.path.abspath(__file__)) app = flask.Flask(__name__, template_folder=BASE_DIR) @@ -73,70 +74,6 @@ def err_abort(abort_status_code, **params): t = flask.render_template("templates/error.html", **params) flask.abort(flask.make_response(t, abort_status_code)) - -## -# Issue a GET request to the backend. -# -# @param endpoint the backend endpoint where to issue the request. -# @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(endpoint, params): - headers = {"Authorization": "ApiKey " + APIKEY} - try: - resp = requests.get( - urljoin(BACKEND_URL, endpoint), params=params, headers=headers - ) - except requests.ConnectionError: - err_abort(500, message="Could not establish connection to backend") - try: - response_json = resp.json() - except ValueError: - err_abort(500, message="Could not parse response from backend") - if resp.status_code != 200: - err_abort( - 500, - message="Backend returned error status", - json=response_json, - status_code=resp.status_code - ) - return response_json - - -## -# POST a request to the backend, and return a error -# response if any error occurs. -# -# @param endpoint the backend endpoint where to POST -# this request. -# @param json the POST's body. -# @return the backend response (JSON format). -def backend_post(endpoint, json): - headers = {"Authorization": "ApiKey " + APIKEY} - try: - resp = requests.post( - urljoin(BACKEND_URL, endpoint), json=json, headers=headers - ) - except requests.ConnectionError: - err_abort(500, message="Could not establish connection to backend") - try: - response_json = resp.json() - except ValueError: - err_abort( - 500, - message="Could not parse response from backend", - status_code=resp.status_code - ) - if resp.status_code != 200: - err_abort( - 500, - message="Backend returned error status", - json=response_json, - status_code=resp.status_code - ) - return response_json - - ## # "Fallback" exception handler to capture all the unmanaged errors. # @@ -151,7 +88,6 @@ def internal_error(e): stack=traceback.format_exc() ) - ## # Serve the main index page. # @@ -180,7 +116,7 @@ except ImportError: def confirm_refund(order_id): # Here we don't care about the session ID pay_params = dict(order_id=order_id) - pay_status = backend_get("check-payment", pay_params) + pay_status = backend_get(BACKEND_URL, "check-payment", pay_params) if not pay_status.get("paid"): err_abort( 400, @@ -213,7 +149,7 @@ def refund(order_id): ), 401 session_id = flask.session.get("session_id", "") pay_params = dict(order_id=order_id, session_id=session_id) - pay_status = backend_get("check-payment", pay_params) + pay_status = backend_get(BACKEND_URL, "check-payment", pay_params) if not pay_status.get("paid"): err_abort( 402, @@ -226,7 +162,7 @@ def refund(order_id): reason="Demo reimbursement", refund=ARTICLE_AMOUNT ) - resp = backend_post("refund", refund_spec) + resp = backend_post(BACKEND_URL, "refund", refund_spec) try: # delete from paid article cache paid_articles_cache.delete(session_id + "-" + article_name) @@ -297,7 +233,7 @@ def get_qrcode_svg(data): @app.route("/check-status//") def check_status(order_id, session_id): pay_params = dict(order_id=order_id, session_id=session_id) - pay_status = backend_get("check-payment", pay_params) + pay_status = backend_get(BACKEND_URL, "check-payment", pay_params) return flask.jsonify(paid=pay_status["paid"]) @@ -348,7 +284,7 @@ def article(article_name, data=None): refund_deadline=dict(t_ms=1000*int(time.time() + 10 * 30)), wire_transfer_deadline=dict(t_ms=1000*int(time.time() + 15 * 30)), ) - order_resp = backend_post("private/orders", dict(order=order)) + order_resp = backend_post(BACKEND_URL, "private/orders", dict(order=order)) order_id = order_resp["order_id"] return flask.redirect( flask.url_for( @@ -360,7 +296,7 @@ def article(article_name, data=None): # pay_params = dict(order_id=order_id, session_id=session_id) - pay_status = backend_get("private/orders/{}".format(order_id), params=dict()) + pay_status = backend_get(BACKEND_URL, "private/orders/{}".format(order_id), params=dict()) if pay_status.get("paid"): # Checks to do: # diff --git a/talermerchantdemos/httpcommon/__init__.py b/talermerchantdemos/httpcommon/__init__.py new file mode 100644 index 0000000..1aecbcd --- /dev/null +++ b/talermerchantdemos/httpcommon/__init__.py @@ -0,0 +1,63 @@ +import requests + +## +# POST a request to the backend, and return a error +# response if any error occurs. +# +# @param endpoint the backend endpoint where to POST +# this request. +# @param json the POST's body. +# @return the backend response (JSON format). +def backend_post(backend_url, endpoint, json): + headers = {"Authorization": "ApiKey " + APIKEY} + try: + resp = requests.post( + urljoin(backend_url, endpoint), json=json, headers=headers + ) + except requests.ConnectionError: + err_abort(500, message="Could not establish connection to backend") + try: + response_json = resp.json() + except ValueError: + err_abort( + 500, + message="Could not parse response from backend", + status_code=resp.status_code + ) + if resp.status_code != 200: + err_abort( + 500, + message="Backend returned error status", + json=response_json, + status_code=resp.status_code + ) + return response_json + + +## +# Issue a GET request to the backend. +# +# @param endpoint the backend endpoint where to issue the request. +# @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 " + APIKEY} + try: + resp = requests.get( + urljoin(backend_url, endpoint), params=params, headers=headers + ) + except requests.ConnectionError: + err_abort(500, message="Could not establish connection to backend") + try: + response_json = resp.json() + except ValueError: + err_abort(500, message="Could not parse response from backend") + if resp.status_code != 200: + err_abort( + 500, + message="Backend returned error status", + json=response_json, + status_code=resp.status_code + ) + return response_json -- cgit v1.2.3