From ab25d557f86ebb76cb54f4a6f2bfb7a542696c04 Mon Sep 17 00:00:00 2001 From: Peter Stockings Date: Sat, 16 Dec 2023 19:55:51 +1100 Subject: [PATCH] Add ability to test http functions with postman like interface --- app.py | 14 ++- services.py | 25 +++-- templates/client.html | 124 +++++++++-------------- templates/dashboard/http_functions.html | 16 ++- templates/dashboard/timer_functions.html | 3 +- 5 files changed, 86 insertions(+), 96 deletions(-) diff --git a/app.py b/app.py index 5bdb812..8512101 100644 --- a/app.py +++ b/app.py @@ -5,7 +5,7 @@ from jinja2_fragments import render_block from flask_htmx import HTMX import requests from db import DataBase -from services import create_http_functions_view_model +from services import create_http_function_view_model, create_http_functions_view_model app = Flask(__name__) app.config.from_pyfile('config.py') @@ -32,6 +32,7 @@ DEFAULT_SCRIPT = """async (req) => { ${value} `)} +
${JSON.stringify(req.body, null, 2)}
`) }""" @@ -59,9 +60,14 @@ def map_isolator_response_to_flask_response(response): def home(): return render_template("home.html", script=DEFAULT_SCRIPT) -@ app.route("/client", methods=["GET"]) -def client(): - return render_template("client.html") +@ app.route("/client/", methods=["GET"]) +def client(function): + http_function = db.get_http_function(function) + if not http_function: + return jsonify({'error': 'Function not found'}), 404 + + http_function = create_http_function_view_model(http_function) + return render_template("client.html", **http_function) @ app.route("/dashboard", methods=["GET"]) def dashboard(): diff --git a/services.py b/services.py index ab0274d..6d8f908 100644 --- a/services.py +++ b/services.py @@ -1,18 +1,25 @@ +def create_http_function_view_model(http_function): + # Base URL for the function invocation + base_url = "https://function.peterstockings.com/f/" + + name = http_function['name'] + function_view_model = { + "id": http_function['id'], + "name": name, + "script_content": http_function['script_content'], + "url": f"{base_url}{name}", + "invoke_count": http_function['invoked_count'] + } + + return function_view_model + def create_http_functions_view_model(http_functions): # Base URL for the function invocation base_url = "https://function.peterstockings.com/f/" view_model = [] - for function in http_functions: - name = function['name'] - function_view_model = { - "id": function['id'], - "name": name, - "script_content": function['script_content'], - "url": f"{base_url}{name}", - "invoke_count": function['invoked_count'] - } + function_view_model = create_http_function_view_model(function) view_model.append(function_view_model) return view_model diff --git a/templates/client.html b/templates/client.html index 7f4688c..562c5ce 100644 --- a/templates/client.html +++ b/templates/client.html @@ -4,7 +4,8 @@
+ name="url" value="{{ url }}">
    + hover:text-orange-500 cursor-pointer" role="tab" id="tab:r0:0" aria-selected="true" aria-disabled="false" + aria-controls="panel:r0:0" tabindex="0" data-rttab="true">Query Params + hover:text-orange-500 cursor-pointer border-b-2 text-orange-600" role="tab" id="tab:r0:2" + aria-selected="false" aria-disabled="false" aria-controls="panel:r0:2" data-rttab="true">Body
-
Add
-
+