feat: add nowcast point alarm get client for id and recipient lookups

Reads IDs from CSV or TXT, writes per-query SUCCESS, NOT_FOUND or FAILED rows to CSV or JSON Lines and mirrors results into SQLite.
This commit is contained in:
2026-10-23 19:12:03 +03:00
parent 0958d26e9f
commit 32a587a1c6
3 changed files with 195 additions and 0 deletions
+74
View File
@@ -510,3 +510,77 @@ def prompt_export(
write_records(path, records, store.webhooks(account_id))
print(f"{len(records)} kayıt yazıldı: {path.resolve()}")
return path
def read_id_list(path: Path, column: str) -> list[str]:
"""Read IDs from a CSV (header column) or TXT (one per line; blank and '#' lines skipped)."""
suffix = path.suffix.lower()
if suffix == ".csv":
with path.open(encoding="utf-8-sig", newline="") as handle:
rows = [row for row in csv.reader(handle, delimiter=";") if row and not row[0].startswith("#")]
if not rows or column not in [name.strip() for name in rows[0]]:
raise ConfigurationError(f"CSV başlığında '{column}' sütunu bulunamadı: {path}")
index = [name.strip() for name in rows[0]].index(column)
values = [row[index].strip() for row in rows[1:] if len(row) > index]
elif suffix == ".txt":
values = [line.strip() for line in path.read_text(encoding="utf-8-sig").splitlines()]
values = [line for line in values if line and not line.startswith("#")]
else:
raise ConfigurationError(f"Girdi dosyası .csv veya .txt uzantılı olmalıdır: {path}")
ids = list(dict.fromkeys(value for value in values if value))
if not ids:
raise ConfigurationError(f"Girdi dosyasında ID bulunamadı: {path}")
return ids
RESULT_COLUMNS = ["query", "status", "httpStatus"] + RECORD_COLUMNS + ["errorBody"]
def lookup_rows(
service: PointAlarmService,
store: PointStore,
queries: list[str],
by: str,
account_id: str | None,
) -> tuple[list[dict[str, Any]], list[dict[str, Any]]]:
"""Run get-by-id / get-by-recipient-id per query; returns (result rows, webhooks)."""
rows: list[dict[str, Any]] = []
for query in queries:
try:
response = (
service.get_by_id(query) if by == "id" else service.get_by_recipient_id(query)
)
except ApiError as exc:
status = "NOT_FOUND" if exc.status_code == 404 else "FAILED"
rows.append(
{
"query": query,
"status": status,
"httpStatus": exc.status_code,
"errorBody": (
json.dumps(exc.response_data, ensure_ascii=False)
if exc.response_data is not None
else str(exc)
),
}
)
LOGGER.warning("%s için sonuç %s (HTTP %s).", query, status, exc.status_code)
continue
data = response.data
items = data if isinstance(data, list) else [data]
items = [item for item in items if isinstance(item, dict)]
if not items:
rows.append({"query": query, "status": "NOT_FOUND", "httpStatus": response.status_code})
for item in items:
store.upsert_registration(item, account_id)
rows.append(
dict(
flatten_registration(item),
query=query,
status="SUCCESS",
httpStatus=response.status_code,
)
)
store.commit()
webhooks = store.webhooks(account_id) if account_id else store.webhooks()
return rows, webhooks