Skip to content

Commit 0fa758e

Browse files
thodson-usgsclaude
andcommitted
Unify status-check sites behind a small _raise_if_not_ok helper
Both pagination loops now had four call sites repeating ``if resp.status_code != 200: raise RuntimeError(_error_body(resp))``. Move that into a one-line helper alongside ``_error_body`` and call it from every site. Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
1 parent 0ad568c commit 0fa758e

1 file changed

Lines changed: 10 additions & 8 deletions

File tree

dataretrieval/waterdata/utils.py

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -339,6 +339,12 @@ def _error_body(resp: requests.Response):
339339
)
340340

341341

342+
def _raise_if_not_ok(resp: requests.Response) -> None:
343+
"""Raise ``RuntimeError(_error_body(resp))`` for any non-200 response."""
344+
if resp.status_code != 200:
345+
raise RuntimeError(_error_body(resp))
346+
347+
342348
def _construct_api_requests(
343349
service: str,
344350
properties: list[str] | None = None,
@@ -583,8 +589,7 @@ def _walk_pages(
583589
client = client or requests.Session()
584590
try:
585591
resp = client.send(req)
586-
if resp.status_code != 200:
587-
raise RuntimeError(_error_body(resp))
592+
_raise_if_not_ok(resp)
588593

589594
# Store the initial response for metadata
590595
initial_response = resp
@@ -606,8 +611,7 @@ def _walk_pages(
606611
headers=headers,
607612
data=content if method == "POST" else None,
608613
)
609-
if resp.status_code != 200:
610-
raise RuntimeError(_error_body(resp))
614+
_raise_if_not_ok(resp)
611615
dfs.append(_get_resp_data(resp, geopd=geopd))
612616
curr_url = _next_req_url(resp)
613617
except Exception: # noqa: BLE001
@@ -1045,8 +1049,7 @@ def get_stats_data(
10451049

10461050
try:
10471051
resp = client.send(req)
1048-
if resp.status_code != 200:
1049-
raise RuntimeError(_error_body(resp))
1052+
_raise_if_not_ok(resp)
10501053

10511054
# Store the initial response for metadata
10521055
initial_response = resp
@@ -1072,8 +1075,7 @@ def get_stats_data(
10721075
params=args,
10731076
headers=headers,
10741077
)
1075-
if resp.status_code != 200:
1076-
raise RuntimeError(_error_body(resp))
1078+
_raise_if_not_ok(resp)
10771079
body = resp.json()
10781080
all_dfs.append(_handle_stats_nesting(body, geopd=GEOPANDAS))
10791081
next_token = body.get("next")

0 commit comments

Comments
 (0)