gnunet-svn
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

[taler-taler-merchant-demos] branch master updated: reducing code


From: gnunet
Subject: [taler-taler-merchant-demos] branch master updated: reducing code
Date: Wed, 22 Jul 2020 17:22:58 +0200

This is an automated email from the git hooks/post-receive script.

ms pushed a commit to branch master
in repository taler-merchant-demos.

The following commit(s) were added to refs/heads/master by this push:
     new 3f4d32f  reducing code
3f4d32f is described below

commit 3f4d32f4ef2c9e7949a2696989a27553f29f0e38
Author: MS <ms@taler.net>
AuthorDate: Wed Jul 22 17:22:53 2020 +0200

    reducing code
---
 talermerchantdemos/httpcommon/__init__.py |   9 +++
 talermerchantdemos/survey/survey.py       | 105 +-----------------------------
 2 files changed, 12 insertions(+), 102 deletions(-)

diff --git a/talermerchantdemos/httpcommon/__init__.py 
b/talermerchantdemos/httpcommon/__init__.py
index 1aecbcd..392eaa2 100644
--- a/talermerchantdemos/httpcommon/__init__.py
+++ b/talermerchantdemos/httpcommon/__init__.py
@@ -1,5 +1,14 @@
 import requests
 
+##
+# 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.
diff --git a/talermerchantdemos/survey/survey.py 
b/talermerchantdemos/survey/survey.py
index 7a71ded..0f24a16 100644
--- a/talermerchantdemos/survey/survey.py
+++ b/talermerchantdemos/survey/survey.py
@@ -41,31 +41,7 @@ CURRENCY = 
TC["taler"]["currency"].value_string(required=True)
 APIKEY = TC["frontends"]["backend_apikey"].value_string(required=True)
 app.config.from_object(__name__)
 LOGGER = logging.getLogger(__name__)
-
-
-##
-# Helper function that returns a HTTP response with
-# a prettified version of a backend error response.
-#
-# @param requests_response the error response coming from
-#        the merchant backend.
-# @return a flask-native response object.
-def backend_error(requests_response):
-    LOGGER.error(
-        "Backend error: status code: " + str(requests_response.status_code)
-    )
-    try:
-        return flask.jsonify(
-            requests_response.json()
-        ), requests_response.status_code
-    except json.decoder.JSONDecodeError:
-        LOGGER.error(
-            "Backend error (NO JSON returned): status code: " +
-            str(requests_response.status_code)
-        )
-        return flask.jsonify(
-            dict(error="Backend died, no JSON got from it")
-        ), 502
+INSTANCED_URL = urljoin(BACKEND_URL, "/survey")
 
 
 ##
@@ -89,81 +65,6 @@ def utility_processor():
     return dict(env=env, prettydate=prettydate)
 
 
-##
-# 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.
-#
-# @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
-
-
-##
-# 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=None):
-    headers = {"Authorization": "ApiKey " + APIKEY}
-    if params is None:
-        params = dict()
-    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
-
-
 ##
 # Exception handler to capture all the unmanaged errors.
 #
@@ -200,7 +101,7 @@ def favicon():
 #         in a prettified format.
 @app.route("/survey-stats", methods=["GET"])
 def survey_stats():
-    stats = backend_get("tip-query")
+    stats = backend_get(INSTANCED_URL, "tip-query")
     return flask.render_template("templates/survey_stats.html", stats=stats)
 
 
@@ -225,7 +126,7 @@ def submit_survey():
         next_url=os.environ.get("TALER_ENV_URL_INTRO", "https://taler.net/";),
         justification="Payment methods survey"
     )
-    backend_resp = backend_post("tip-authorize", tip_spec)
+    backend_resp = backend_post(INSTANCED_URL, "tip-authorize", tip_spec)
 
     taler_tip_uri = backend_resp.get("taler_tip_uri")
     if taler_tip_uri:

-- 
To stop receiving notification emails like this one, please contact
gnunet@gnunet.org.



reply via email to

[Prev in Thread] Current Thread [Next in Thread]