refactor(waterdata): coherent error handling + safe cleanups for non-OGC getters#298
Merged
thodson-usgs merged 2 commits intoMay 30, 2026
Conversation
…OGC getters Non-OGC getter error coherence + safe cleanups. All public signatures, kwargs (incl. the upstream-driven Samples camelCase), and return shapes are unchanged — the only behavior change is the exception TYPE on HTTP errors. Error coherence: - Route the direct-httpx getters (get_samples, get_samples_summary, get_codes, ratings._search/_download_and_parse) through the module's typed _raise_for_non_200 instead of a bare response.raise_for_status(), so a 429 raises RateLimited and a 5xx raises ServiceUnavailable (with Retry-After and USGS-aware messages) — the same typed errors the OGC/stats path already raises. NOTE: this changes the exception type from httpx.HTTPStatusError; flagged for review. Safe cleanups: - Extract `_get_samples_csv` and call it from get_samples/get_samples_summary (collapses the duplicated GET + read_csv tail). - Fix get_channel docstring drift: channel_flow was labeled as units (it's the discharge value), channel_flow_unit was undocumented, and measurement_type carried last_modified's pasted RFC-3339 block. - _format_api_dates: resolve the local timezone only after the all-NA / duration / interval guards (skip the lookup when it's discarded). Tests: add typed-error regression tests (429 -> RateLimited, 5xx -> ServiceUnavailable). 118 samples/ratings/utils tests pass; ruff clean. Deliberately NOT included (kept backward-compatible / out of scope): no kwarg renames (Samples camelCase mirrors the upstream API), no get_codes return-shape change, no ssl_check additions. The _aggregate_paginated_response / _combine_chunk_responses response-merge dedup is deferred to avoid colliding with the in-flight chunking PR (DOI-USGS#295). Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
…non-ogc-getter-coherence
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
Coherence pass on the non-OGC getters (Samples, codes, ratings) — the backlog split out of PR #295's review. Two themes: error-handling coherence and safe cleanups.
All public signatures, kwargs (including the Samples camelCase, which mirrors the upstream API), and return shapes are unchanged. The one behavior-affecting change is the exception type on HTTP errors — flagged below for an explicit decision.
Error coherence (behavior: exception type)
The OGC and stats paths raise the module's typed errors (
RateLimitedon 429,ServiceUnavailableon 5xx, withRetry-Afterand USGS-aware messages via_raise_for_non_200). The direct-httpx.getgetters did not — they used a bareresponse.raise_for_status()(generichttpx.HTTPStatusError). This PR routes them through_raise_for_non_200:get_samples,get_samples_summary,get_codes(api.py)ratings._search,ratings._download_and_parse(ratings.py)So a caller can now
except RateLimited/except ServiceUnavailableacross the whole module.Safe cleanups (behavior-preserving)
_get_samples_csvhelper — collapses the duplicated GET +read_csv+ metadata tail shared byget_samples/get_samples_summary.get_channeldocstring drift —channel_flowwas described as units (it's the discharge value);channel_flow_unitwas undocumented;measurement_typecarriedlast_modified's pasted RFC-3339 block._format_api_dates— resolve the local timezone only after the all-NA / duration / interval early-return guards (it was computed then discarded on those paths).Tests
RateLimited, 5xx →ServiceUnavailable).ruffclean.Intentionally excluded (per scope + backward-compat)
get_codesreturn-shape change, nossl_checkadditions (those were the "breaking" option)._aggregate_paginated_response/_combine_chunk_responsesresponse-merge dedup is deferred to avoid conflicting with the in-flight chunking PR fix: repair WQP_Metadata.site_info and streamstats.Watershed #295.🤖 Generated with Claude Code