handle Maps worker response casing
This commit is contained in:
+19
-4
@@ -181,6 +181,20 @@ def as_text(value: Any) -> str:
|
||||
return " ".join(str(value).split()).strip()
|
||||
|
||||
|
||||
def field_value(value: Any, *names: str) -> Any:
|
||||
"""Read JSON fields from both tagged and default Go JSON casing."""
|
||||
if not isinstance(value, dict):
|
||||
return None
|
||||
for name in names:
|
||||
if name in value:
|
||||
return value[name]
|
||||
wanted = {name.lower() for name in names}
|
||||
for key, item in value.items():
|
||||
if str(key).lower() in wanted:
|
||||
return item
|
||||
return None
|
||||
|
||||
|
||||
def as_float(value: Any) -> float | None:
|
||||
try:
|
||||
if value in (None, ""):
|
||||
@@ -382,7 +396,7 @@ def submit_gmaps_job(query: str, include_email: bool = False, extra_reviews: boo
|
||||
"max_time": max(60, min(3600, GMAPS_REQUEST_TIMEOUT)),
|
||||
}
|
||||
response = http_json("POST", GMAPS_URL + endpoint, payload, gmaps_headers(), timeout=30)
|
||||
job_id = response.get("job_id") or response.get("id")
|
||||
job_id = field_value(response, "job_id", "id", "JobID", "ID")
|
||||
if not job_id:
|
||||
raise RuntimeError("gmaps_job_id_missing")
|
||||
return str(job_id)
|
||||
@@ -399,8 +413,8 @@ def poll_gmaps_job(job_id: str, progress: Callable[[str, int, int], None] | None
|
||||
terminal_bad = {"failed", "cancelled", "canceled", "discarded"}
|
||||
while time.monotonic() < deadline:
|
||||
response = http_json("GET", GMAPS_URL + "/api/v1/jobs/" + quote_plus(str(job_id)), headers=gmaps_headers(), timeout=30)
|
||||
status = str(response.get("status") or "pending").lower()
|
||||
result_count = as_int(response.get("result_count"))
|
||||
status = str(field_value(response, "status", "Status") or "pending").lower()
|
||||
result_count = as_int(field_value(response, "result_count", "ResultCount"))
|
||||
if progress:
|
||||
progress(status, result_count, 0)
|
||||
if status in terminal_ok:
|
||||
@@ -410,7 +424,8 @@ def poll_gmaps_job(job_id: str, progress: Callable[[str, int, int], None] | None
|
||||
return download_standard_results(job_id)
|
||||
return extract_gmaps_results(response)
|
||||
if status in terminal_bad:
|
||||
raise RuntimeError("gmaps_job_" + status + (":" + as_text(response.get("error")) if response.get("error") else ""))
|
||||
error = field_value(response, "error", "Error")
|
||||
raise RuntimeError("gmaps_job_" + status + (":" + as_text(error) if error else ""))
|
||||
time.sleep(max(0.5, GMAPS_POLL_SECONDS))
|
||||
raise RuntimeError("gmaps_job_timeout")
|
||||
|
||||
|
||||
+1
-1
@@ -78,7 +78,7 @@ class BrowserOnlyAppTests(unittest.TestCase):
|
||||
with patch.object(server, "GMAPS_URL", "http://gmaps:8080"), \
|
||||
patch.object(server, "GMAPS_API_MODE", "standard"), \
|
||||
patch.object(server, "GMAPS_API_KEY", ""), \
|
||||
patch("api.server.http_json", side_effect=[{"id": "job-1"}, {"id": "job-1", "status": "pending"}, {"id": "job-1", "status": "working"}, {"id": "job-1", "status": "ok"}]) as http_json, \
|
||||
patch("api.server.http_json", side_effect=[{"id": "job-1"}, {"ID": "job-1", "Status": "pending"}, {"ID": "job-1", "Status": "working"}, {"ID": "job-1", "Status": "ok"}]) as http_json, \
|
||||
patch("api.server.http_bytes", return_value=csv_body) as http_bytes:
|
||||
job_id = server.submit_gmaps_job("plumbers Cape Town")
|
||||
results = server.poll_gmaps_job(job_id)
|
||||
|
||||
Reference in New Issue
Block a user