From ab06cbc0b7e337bdbc3369c9710aaf08ef35cb60 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Mateusz=20Gruszczy=C5=84ski?= Date: Wed, 4 Mar 2026 13:38:29 +0100 Subject: [PATCH] small fixes --- script.py | 58 ++++++++++++++++++++++++++++++++++++++++++ static/js/generator.js | 7 ++--- 2 files changed, 62 insertions(+), 3 deletions(-) create mode 100644 script.py diff --git a/script.py b/script.py new file mode 100644 index 0000000..eacea39 --- /dev/null +++ b/script.py @@ -0,0 +1,58 @@ +#!/usr/bin/env python3 +import json +import re +import requests + +BASE_URL = "http://geo-block.krk.itg.demo-ht.iadm" +ENDPOINT = "/api/generate" + +payload = { + "countries": [ + "PL" + ], + "aggregate": true, + "use_cache": true, + "app_type": "haproxy", + "app_variant": "map" +} + +resp = requests.post(BASE_URL + ENDPOINT, json=payload, timeout=120) + +print("Status:", resp.status_code) +print("X-From-Cache:", resp.headers.get("X-From-Cache")) +print("X-Cache-Type:", resp.headers.get("X-Cache-Type")) +print("X-Generated-At:", resp.headers.get("X-Generated-At")) + +ct = (resp.headers.get("Content-Type") or "").lower() + +if resp.status_code >= 400: + # try show JSON error, else text + try: + print(json.dumps(resp.json(), indent=2)) + except Exception: + print(resp.text) + raise SystemExit(1) + +if "application/json" in ct: + print(json.dumps(resp.json(), indent=2)) +else: + filename = "output" + cd = resp.headers.get("Content-Disposition") or "" + m = re.search(r'filename="?([^"]+)"?', cd) + if m: + filename = m.group(1) + else: + # fallback extension + if "text/csv" in ct: + filename += ".csv" + elif "javascript" in ct: + filename += ".js" + elif "text/plain" in ct: + filename += ".txt" + else: + filename += ".bin" + + with open(filename, "wb") as f: + f.write(resp.content) + + print("Saved to:", filename) diff --git a/static/js/generator.js b/static/js/generator.js index 4f84d5c..c59c0ba 100644 --- a/static/js/generator.js +++ b/static/js/generator.js @@ -107,6 +107,8 @@ function buildPythonScript() { payload.app_variant = $("pyAppVariant").value; } + const payloadJson = JSON.stringify(payload, null, 4); + const script = `#!/usr/bin/env python3 import json import re @@ -115,7 +117,8 @@ import requests BASE_URL = ${JSON.stringify(baseUrl)} ENDPOINT = ${JSON.stringify(endpoint)} -payload = ${JSON.stringify(payload, null, 4)} +payload_json = """${payloadJson}""" +payload = json.loads(payload_json) resp = requests.post(BASE_URL + ENDPOINT, json=payload, timeout=120) @@ -127,7 +130,6 @@ print("X-Generated-At:", resp.headers.get("X-Generated-At")) ct = (resp.headers.get("Content-Type") or "").lower() if resp.status_code >= 400: - # try show JSON error, else text try: print(json.dumps(resp.json(), indent=2)) except Exception: @@ -143,7 +145,6 @@ else: if m: filename = m.group(1) else: - # fallback extension if "text/csv" in ct: filename += ".csv" elif "javascript" in ct: