Skip to content

Commit a9cf2d7

Browse files
thodson-usgsclaude
andcommitted
Tighten _request_bytes: type the param and drop redundant str() wrap
- Annotate ``req`` as ``requests.PreparedRequest`` (the only caller flow is ``build_request(...).prepare()``; ``requests`` is already imported). - ``_cql2_param`` returns ``str``, which ``requests.Request(data=...)`` carries through to ``req.body``. The hot path on POST routes was ``str(body).encode("utf-8")``; ``str(<str>)`` is a no-op, so drop it and let ``body.encode("utf-8")`` allocate once. - Trim docstring: replaces the rotting "PR 233" / "currently only monitoring-locations" anchors with a behavioral description that doesn't rely on which routes happen to be POST today. No behavior change. 30 chunker unit tests pass. Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
1 parent 2941328 commit a9cf2d7

1 file changed

Lines changed: 6 additions & 10 deletions

File tree

dataretrieval/waterdata/chunking.py

Lines changed: 6 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -211,25 +211,21 @@ def _chunk_bytes(chunk: list) -> int:
211211
return len(",".join(map(str, chunk)))
212212

213213

214-
def _request_bytes(req: Any) -> int:
214+
def _request_bytes(req: requests.PreparedRequest) -> int:
215215
"""Total bytes of a prepared request: URL + body.
216216
217-
For the GET-routed services PR 233 introduced, the multi-value list
218-
lives in the URL and ``req.body`` is ``None`` — this reduces to URL
219-
length. For POST-routed services (currently only ``monitoring-
220-
locations``, which the upstream API still rejects comma-separated
221-
values for and routes through CQL2 JSON), the multi-value list lives
222-
in the body and the URL stays ~200 bytes regardless of payload;
223-
counting body bytes is the only way the planner can recognize that
224-
a POST request needs to chunk.
217+
GET routes have ``body=None`` and reduce to URL length. POST routes
218+
(CQL2 JSON body) need body bytes — the URL stays short regardless of
219+
payload, so URL-only sizing would underestimate the request and skip
220+
chunking when it's needed.
225221
"""
226222
url_len = len(req.url)
227223
body = req.body
228224
if body is None:
229225
return url_len
230226
if isinstance(body, (bytes, bytearray)):
231227
return url_len + len(body)
232-
return url_len + len(str(body).encode("utf-8"))
228+
return url_len + len(body.encode("utf-8"))
233229

234230

235231
def _worst_case_args(

0 commit comments

Comments
 (0)