small fixes
This commit is contained in:
58
script.py
Normal file
58
script.py
Normal file
@@ -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)
|
||||
@@ -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:
|
||||
|
||||
Reference in New Issue
Block a user