#!/usr/bin/env python3 """Verify Google Maps API key by doing a cheap Geocoding API call. Output (JSON on stdout): {"ok": true, "message": "…"} {"ok": false, "message": "GOOGLE_MAPS_API_KEY not set"} {"ok": false, "message": "Missing dependency: pip install googlemaps"} {"ok": false, "message": "Geocoding API error: …"} """ import json import os import sys # Skald installs our deps with `pip install --target .pydeps` beside this file and # puts that dir on PYTHONPATH for the *server* process only — the verify step gets # a bare `sh -c "python3 verify.py"`, so `import googlemaps` would fail here even # on a correctly installed connector. Put .pydeps on sys.path ourselves. _PYDEPS = os.path.join(os.path.dirname(os.path.abspath(__file__)), ".pydeps") if os.path.isdir(_PYDEPS): sys.path.insert(0, _PYDEPS) def main() -> None: api_key = os.environ.get("GOOGLE_MAPS_API_KEY", "").strip() if not api_key: print(json.dumps({ "ok": False, "message": "GOOGLE_MAPS_API_KEY not set. Provide a valid Google Maps API key." })) sys.exit(1) try: import googlemaps # type: ignore except ImportError as e: print(json.dumps({ "ok": False, "message": f"Missing dependency: {e}. Run: pip install googlemaps" })) sys.exit(1) try: client = googlemaps.Client(key=api_key) result = client.geocode("Rome, IT") except Exception as e: print(json.dumps({ "ok": False, "message": f"Geocoding API error: {e}" })) sys.exit(1) if not result: print(json.dumps({ "ok": False, "message": "Geocoding API returned no result. The API key may be restricted or Geocoding API disabled." })) sys.exit(1) print(json.dumps({ "ok": True, "message": "Google Maps API key is valid. Directions, Geocoding, Places, and Distance Matrix APIs are reachable.", "details": { "probe_result": result[0].get("formatted_address", "Rome, Italy"), "lat": result[0].get("geometry", {}).get("location", {}).get("lat"), "lng": result[0].get("geometry", {}).get("location", {}).get("lng"), } })) if __name__ == "__main__": main()