Commit d454cd9
refactor(ogc): reuse + unify the OGC engine — pager, aggregation, ambient state
Reuse and unify the OGC engine's HTTP pagination / aggregation / error-recovery /
ambient-state plumbing instead of carrying parallel implementations. Net source
reduction of ~66 LOC, behavior-preserving, plus one rate-limit correctness fix.
wateruse reuse:
- wateruse drives the engine's generic `_paginate` (with an injected
`raise_for_status` for the NWDC `{detail}` envelope), `_run_sync` (anyio portal,
Jupyter-safe), and `_combine_chunk_frames` / `_combine_chunk_responses`
aggregators — replacing a hand-rolled pager, thread bridge, and bespoke
aggregation. `_resolve_locations` becomes a `_LOCATION_BUILDERS` table dispatch,
dropping a 3-way if/elif and a duplicated selector enumeration.
Engine unification:
- `planning._merge_response`: one low-level "fold N responses into one" behind both
pagination (`_paginate`) and chunked/fan-out aggregation
(`_combine_chunk_responses`), replacing two near-duplicate implementations; deletes
`engine._aggregate_paginated_response`.
- `utils.Ambient[T]`: a generic ContextVar-with-scope class collapsing each per-call
ambient (`_row_cap`, `_ogc_base_url`, `_dialect`, the chunker's `_chunked_client`)
from a var + hand-written `@contextmanager` setter pair into one declaration.
`with _x(value):` call sites unchanged; readers shorten to `_x.get()`.
- `_paginate`'s verbatim per-page progress block deduped into a `report_page` closure.
- `_combine_chunk_responses`: dropped a dead single-response branch.
- `_QUOTA_HEADER` moved to the base `planning` module — dedups the literal and fixes
a layering inversion (planning had hard-coded it, unable to import from chunking).
- `_cql2_param`: CQL2 filter list built as a comprehension.
- `engine._check_id_format`: inlined into its only caller; dead re-export dropped.
Rate-limit correctness fix:
- `x-ratelimit-remaining` now reports the LOWEST value any concurrent sub-request
saw (the quota actually left after a fan-out), via a shared `_lowest_remaining`,
instead of the last-by-index — fixing a latent inaccuracy in the OGC chunker too.
Behavior-preserving (live-verified); offline OGC/wateruse/utils/progress suites
green; ruff + mypy --strict clean.
Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01Sjb14HkwuCydKSKMsaXsgd1 parent 9345743 commit d454cd9
10 files changed
Lines changed: 297 additions & 354 deletions
File tree
- dataretrieval
- ogc
- waterdata
- tests
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
69 | 69 | | |
70 | 70 | | |
71 | 71 | | |
72 | | - | |
73 | | - | |
| 72 | + | |
74 | 73 | | |
75 | 74 | | |
76 | 75 | | |
77 | 76 | | |
78 | 77 | | |
79 | 78 | | |
80 | | - | |
| 79 | + | |
81 | 80 | | |
82 | 81 | | |
83 | 82 | | |
| |||
106 | 105 | | |
107 | 106 | | |
108 | 107 | | |
109 | | - | |
110 | | - | |
111 | | - | |
112 | 108 | | |
113 | 109 | | |
114 | 110 | | |
| |||
152 | 148 | | |
153 | 149 | | |
154 | 150 | | |
155 | | - | |
156 | | - | |
157 | | - | |
158 | | - | |
159 | | - | |
160 | | - | |
161 | | - | |
162 | | - | |
163 | | - | |
164 | | - | |
165 | | - | |
166 | | - | |
167 | | - | |
168 | | - | |
169 | | - | |
170 | | - | |
171 | | - | |
172 | | - | |
173 | | - | |
174 | | - | |
175 | | - | |
176 | | - | |
177 | | - | |
178 | | - | |
179 | | - | |
180 | | - | |
181 | | - | |
182 | | - | |
183 | | - | |
184 | | - | |
185 | | - | |
186 | | - | |
| 151 | + | |
| 152 | + | |
| 153 | + | |
| 154 | + | |
| 155 | + | |
187 | 156 | | |
188 | 157 | | |
189 | 158 | | |
| |||
197 | 166 | | |
198 | 167 | | |
199 | 168 | | |
200 | | - | |
201 | | - | |
| 169 | + | |
| 170 | + | |
202 | 171 | | |
203 | 172 | | |
204 | 173 | | |
| |||
541 | 510 | | |
542 | 511 | | |
543 | 512 | | |
544 | | - | |
| 513 | + | |
545 | 514 | | |
546 | 515 | | |
547 | 516 | | |
| |||
0 commit comments