Skip to content

Commit 4db463e

Browse files
adamtheturtleclaude
andcommitted
Use RequestData instead of PreparedRequest in respx adapter
Convert httpx.Request directly to RequestData, removing the PreparedRequest intermediate. This eliminates the requests library dependency from the respx module and removes all type suppression comments (type: ignore, noqa). Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
1 parent c0dc2f2 commit 4db463e

File tree

1 file changed

+13
-17
lines changed

1 file changed

+13
-17
lines changed

src/mock_vws/_respx_mock_server/decorators.py

Lines changed: 13 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -10,9 +10,8 @@
1010
import httpx
1111
import respx
1212
from beartype import BeartypeConf, beartype
13-
from requests import PreparedRequest
14-
from requests.structures import CaseInsensitiveDict
1513

14+
from mock_vws._mock_common import RequestData
1615
from mock_vws._requests_mock_server.decorators import MissingSchemeError
1716
from mock_vws._requests_mock_server.mock_web_query_api import (
1817
MockVuforiaWebQueryAPI,
@@ -37,24 +36,21 @@
3736
_BRISQUE_TRACKING_RATER = BrisqueTargetTrackingRater()
3837

3938

40-
def _to_prepared_request(request: httpx.Request) -> PreparedRequest:
41-
"""Convert an httpx.Request to a requests.PreparedRequest.
39+
def _to_request_data(request: httpx.Request) -> RequestData:
40+
"""Convert an httpx.Request to a RequestData.
4241
4342
Args:
4443
request: The httpx request to convert.
4544
4645
Returns:
47-
A PreparedRequest with headers, body, method, and url set.
48-
The ``path_url`` property is derived automatically from ``url``.
46+
A RequestData with method, path, headers, and body set.
4947
"""
50-
prepared = PreparedRequest()
51-
prepared.method = request.method
52-
prepared.url = str(request.url) # type: ignore[call-overload]
53-
prepared.headers = CaseInsensitiveDict( # type: ignore[misc]
54-
{k: v for k, v in request.headers.items()} # noqa: C416
48+
return RequestData(
49+
method=request.method,
50+
path=request.url.raw_path.decode(encoding="ascii"),
51+
headers=request.headers,
52+
body=request.content,
5553
)
56-
prepared.body = request.content
57-
return prepared
5854

5955

6056
@beartype(conf=BeartypeConf(is_pep484_tower=True))
@@ -158,12 +154,12 @@ def add_vumark_database(self, vumark_database: VuMarkDatabase) -> None:
158154

159155
def _make_callback(
160156
self,
161-
handler: Callable[[PreparedRequest], _ResponseType],
157+
handler: Callable[[RequestData], _ResponseType],
162158
) -> Callable[[httpx.Request], httpx.Response]:
163159
"""Create a respx-compatible callback from a handler.
164160
165161
Args:
166-
handler: A handler that takes a PreparedRequest and returns a
162+
handler: A handler that takes a RequestData and returns a
167163
response tuple.
168164
169165
Returns:
@@ -187,7 +183,7 @@ def callback(request: httpx.Request) -> httpx.Response:
187183
httpx.ReadTimeout: The response delay exceeded the read
188184
timeout.
189185
"""
190-
prepared = _to_prepared_request(request=request)
186+
request_data = _to_request_data(request=request)
191187
timeout_info: dict[str, float | None] = request.extensions.get(
192188
"timeout", {}
193189
)
@@ -198,7 +194,7 @@ def callback(request: httpx.Request) -> httpx.Response:
198194
message="Response delay exceeded read timeout",
199195
request=request,
200196
)
201-
status_code, headers, body = handler(prepared)
197+
status_code, headers, body = handler(request_data)
202198
sleep_fn(delay_seconds)
203199
if isinstance(body, str):
204200
body = body.encode()

0 commit comments

Comments
 (0)