Skip to content

Commit d1284e1

Browse files
thodson-usgsclaude
andcommitted
refactor(ogc): pass the network-error host to _run_sync explicitly
`_run_sync` reported a connection failure against `_ogc_base_url.get()` — the engine's request-builder base (`{base}/collections/{service}/items`). `wateruse` builds its own NWDC requests and never uses that builder, so it was wrapping its fan-out in `with _ogc_base_url(WATERUSE_URL)` purely to steer that error message: borrowing an unrelated ambient and setting it to a `/data` endpoint that would be nonsensical had the request-builder ever run in scope. Give `_run_sync` an explicit `error_url` parameter (default `None` → the scoped `_ogc_base_url`, unchanged for the OGC / NGWMN getters). `wateruse` passes its endpoint directly and no longer touches the request-builder ambient. Behavior-identical; surfaced by /simplify (altitude). Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01Sjb14HkwuCydKSKMsaXsgd
1 parent 8e3d2b8 commit d1284e1

2 files changed

Lines changed: 24 additions & 10 deletions

File tree

dataretrieval/ogc/engine.py

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -945,6 +945,7 @@ def _run_sync(
945945
make_coro: Callable[[], Awaitable[tuple[pd.DataFrame, httpx.Response]]],
946946
*,
947947
service: str,
948+
error_url: str | httpx.URL | None = None,
948949
) -> tuple[pd.DataFrame, httpx.Response]:
949950
"""Drive an async OGC fetch to completion from synchronous code.
950951
@@ -957,6 +958,11 @@ def _run_sync(
957958
Shared by the non-chunked fetch paths; the chunked OGC getters
958959
drive their own portal
959960
inside :meth:`chunking.ChunkedCall.resume`.
961+
962+
A connection failure on the initial request is surfaced as a typed
963+
``NetworkError`` against ``error_url`` when given (callers that build their
964+
own requests, e.g. ``wateruse``), else the request-builder base the caller
965+
scoped via ``_ogc_base_url`` (the OGC / NGWMN getters).
960966
"""
961967
with _progress.progress_context(service=service):
962968
with start_blocking_portal() as portal:
@@ -970,9 +976,15 @@ def _run_sync(
970976
except httpx.TransportError as exc:
971977
# The initial-request connection failure ``_paginate`` lets
972978
# through raw; mid-pagination failures are already typed.
973-
# Report the base URL actually targeted (NGWMN/sibling APIs
974-
# set their own via ``_ogc_base_url``), not a hardcoded host.
975-
raise _network_error(_ogc_base_url.get(), exc) from exc
979+
# Report the base URL actually targeted: callers that build
980+
# their own requests (``wateruse``) pass ``error_url``; the OGC
981+
# getters leave it unset and fall back to the request-builder
982+
# base they scoped via ``_ogc_base_url`` (NGWMN/sibling APIs set
983+
# their own), not a hardcoded host.
984+
raise _network_error(
985+
error_url if error_url is not None else _ogc_base_url.get(),
986+
exc,
987+
) from exc
976988

977989

978990
# ``AGENCY-ID``: a hyphen-separated agency prefix and local id. The local id

dataretrieval/wateruse.py

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@
5353

5454
from dataretrieval.codes.states import to_state
5555
from dataretrieval.exceptions import DataRetrievalError
56-
from dataretrieval.ogc.engine import _ogc_base_url, _paginate, _run_sync
56+
from dataretrieval.ogc.engine import _paginate, _run_sync
5757
from dataretrieval.ogc.planning import _combine_chunk_frames, _combine_chunk_responses
5858
from dataretrieval.utils import (
5959
HTTPX_DEFAULTS,
@@ -234,12 +234,14 @@ def get_wateruse(
234234
for location in _resolve_locations(state, county, huc)
235235
]
236236
# ``_run_sync`` drives the async fan-out via an anyio portal, so it is safe
237-
# even inside an already-running event loop (e.g. a Jupyter notebook);
238-
# ``_ogc_base_url`` sets the host reported in any connection-error message.
239-
with _ogc_base_url(WATERUSE_URL):
240-
df, response = _run_sync(
241-
lambda: _fan_out(requests, headers, ssl_check), service="wateruse"
242-
)
237+
# even inside an already-running event loop (e.g. a Jupyter notebook).
238+
# ``error_url`` is the host reported in any connection-error message (this
239+
# module builds its own requests, so it has no OGC request-builder base).
240+
df, response = _run_sync(
241+
lambda: _fan_out(requests, headers, ssl_check),
242+
service="wateruse",
243+
error_url=WATERUSE_URL,
244+
)
243245
return df, BaseMetadata(response)
244246

245247

0 commit comments

Comments
 (0)