"""In-container probe: status and error code of attachment endpoints. Used as evidence that a replica answers with the common JSON error format while S3 is unavailable (the load balancer may answer 503 on its own before this). """ import json import os import urllib.error import urllib.request TOKEN = os.environ["TOKEN"] ATTACHMENT_ID = os.environ["ATTACHMENT_ID"] def probe(url): request = urllib.request.Request(url, headers={"Authorization": "Bearer " + TOKEN}) try: with urllib.request.urlopen(request, timeout=10) as response: return {"status": response.status} except urllib.error.HTTPError as error: payload = json.loads(error.read().decode("utf-8")) return { "status": error.code, "error_code": payload["error"]["code"], "has_request_id": bool(error.headers.get("X-Request-Id")), } base = "http://127.0.0.1:8080/api/v1/attachments/" + ATTACHMENT_ID print(json.dumps({"metadata": probe(base), "content": probe(base + "/content")}))