Refactor request handling with library-agnostic RequestData#2976
Refactor request handling with library-agnostic RequestData#2976adamtheturtle merged 1 commit intomainfrom
Conversation
Introduce RequestData as a frozen dataclass to replace PreparedRequest dependency in handler layer. Convert PreparedRequest to RequestData at the responses adapter boundary, removing type-checking conflicts and centralizing request normalization. This decouples core business logic from the requests library and enables future adapter support (respx, httpx, etc). - Add RequestData dataclass with method, path, headers, body fields - Update all handler signatures to accept RequestData instead of PreparedRequest - Move body normalization (None/str/bytes conversion) to adapter boundary - Remove duplicate _body_bytes() helpers from both API handler modules - Update decorators.py to convert at boundary in _wrap_callback Fixes #2974 Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
There was a problem hiding this comment.
Cursor Bugbot has reviewed your changes and found 1 potential issue.
Bugbot Autofix is OFF. To automatically fix reported issues with Cloud Agents, enable Autofix in the Cursor dashboard.
| elif isinstance(raw_body, str): | ||
| body_bytes = raw_body.encode(encoding="utf-8") | ||
| else: | ||
| body_bytes = raw_body |
There was a problem hiding this comment.
Query API body normalization behavior silently changed
Low Severity
The PR describes the two removed _body_bytes() helpers as "duplicate," but they had different behavior for str bodies. The old query API version returned b"" for string bodies (treating them same as None), while the services API version encoded them via .encode("utf-8"). The unified adapter in _wrap_callback now always encodes str to UTF-8, silently changing the query API's body handling semantics. If a str body ever reaches the query path, it will now be processed as real content rather than treated as empty.
There was a problem hiding this comment.
This behavioral change is intentional. The old query `_body_bytes()` silently discarding string bodies (returning `b""`) was arguably a bug — if a string body arrives, encoding it to UTF-8 is strictly more correct than throwing it away.
In practice this is a no-op: the `responses` library always provides `bytes` or `None` for the query endpoint's multipart form body, so the `str` branch is never reached for query requests.


Introduce RequestData dataclass as a frozen, library-agnostic representation of HTTP requests, replacing the requests library's PreparedRequest in the handler layer.
Changes:
This enables future adapter support (respx, httpx, etc) and eliminates type-checking conflicts between mypy, pyrefly, and ruff.
Fixes #2974
Note
Medium Risk
Changes the core interface between the
responsesadapter and all mock route handlers, so incorrect request normalization (body/path/headers) could break request validation or JSON parsing across endpoints.Overview
Refactors the mock request handling layer to use a new frozen
RequestDatadataclass (method,path,headers,body) instead ofrequests.PreparedRequest.MockVWS._wrap_callbacknow converts aPreparedRequestintoRequestData(including normalizing body to bytes) before invoking route handlers, and both Web Services and Web Query API handlers are updated to consumeRequestData, removing duplicated body-parsing helpers and switching frompath_urltopath.Written by Cursor Bugbot for commit def934d. This will update automatically on new commits. Configure here.