-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlookup.py
More file actions
40 lines (36 loc) · 1.77 KB
/
Copy pathlookup.py
File metadata and controls
40 lines (36 loc) · 1.77 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
"""Predio — Python example with typed-error handling.
Usage: PREDIO_API_KEY=pk_... python lookup.py 13077A01800039
Only stdlib (urllib) so it runs anywhere; use requests/httpx in real code if you prefer.
"""
import json
import os
import sys
import urllib.error
import urllib.request
API_KEY = os.environ.get("PREDIO_API_KEY")
if not API_KEY:
sys.exit("Set PREDIO_API_KEY (free key: https://prediohq.com)")
rc = sys.argv[1] if len(sys.argv) > 1 else "9872023VH5797S0001WX"
req = urllib.request.Request(
f"https://api.prediohq.com/v1/inmueble/{rc}",
headers={"x-api-key": API_KEY},
)
try:
with urllib.request.urlopen(req) as res:
body = json.load(res)
prop = body["property"]
addr = prop.get("address") or {}
print(f"{addr.get('municipality', '?')} — {prop['class']} / {prop['use']}")
print(f"Built area: {prop.get('builtAreaM2', 'n/a')} m² · year: {prop.get('yearBuilt', 'n/a')}")
print(f"Source: {body['source']['provider']} · cached: {body['source']['cached']}")
except urllib.error.HTTPError as e:
# Every error is JSON with a stable {error: {code, message}} shape — branch on code.
body = json.load(e)
code = body.get("error", {}).get("code")
if code in ("NOT_FOUND", "INVALID_REFERENCE"): # 404/422 — bad input: don't retry
sys.exit(f"Bad input (don't retry): {body['error']['message']}")
if code == "PAYMENT_REQUIRED": # 402 — top up, then retrying is safe
sys.exit(f"Out of credits: {body['error']['message']}")
if code == "RATE_LIMITED": # 429 — back off and retry
sys.exit(f"Rate limited, retry after: {e.headers.get('retry-after', '?')}s")
sys.exit(f"Transient ({e.code}): {body.get('error', {}).get('message', 'unknown')}") # 5xx — retryable