import json import os from bootstrap import build_client from automapa_map_services.exceptions import AutomapaException def geocode(client): response = client.geocoder().geocode( city="Warszawa", street="Domaniewska", house="37", country="PL" ) matches = response.raw().get("result") if matches is None or matches == []: return {"status": "NO_RESULTS"} if not isinstance(matches, list): raise RuntimeError("UNEXPECTED_RESULT") first = matches[0] return {"status": "OK", "lng": first.get("x"), "lat": first.get("y"), "city": first.get("city"), "quality": first.get("quality")} def revgeocode(client): response = client.geocoder().revgeocode( point=[21.0065983, 52.1832837], get_postal_code=True ) address = response.raw().get("result") if not address: return {"status": "NO_RESULTS"} if not isinstance(address, dict): raise RuntimeError("UNEXPECTED_RESULT") return {"status": "OK", "city": address.get("city"), "street": address.get("street"), "house": address.get("house"), "pcode": address.get("pcode")} if __name__ == "__main__": try: client = build_client(os.environ.get("AUTOMAPA_SESSION_ID", "")) print(json.dumps({"geocode": geocode(client), "revgeocode": revgeocode(client)})) except (AutomapaException, RuntimeError): raise SystemExit("SDK_ERROR_OR_SESSION_REQUIRED")