Sends request params through a client whose transport records the request and answers `{}`.
| 52 | |
| 53 | |
| 54 | class Send: |
| 55 | """Sends request params through a client whose transport records the request and answers `{}`.""" |
| 56 | |
| 57 | request: httpx2.Request |
| 58 | """the last request that went over the wire""" |
| 59 | |
| 60 | def __init__(self, client: Anthropic | AsyncAnthropic) -> None: |
| 61 | transport = httpx2.MockTransport(self._record) |
| 62 | self._client: Anthropic | AsyncAnthropic |
| 63 | if isinstance(client, Anthropic): |
| 64 | self._client = client.with_options(http_client=httpx2.Client(transport=transport), max_retries=0) |
| 65 | else: |
| 66 | self._client = client.with_options(http_client=httpx2.AsyncClient(transport=transport), max_retries=0) |
| 67 | |
| 68 | def _record(self, request: httpx2.Request) -> httpx2.Response: |
| 69 | self.request = request |
| 70 | return httpx2.Response(200, json={}) |
| 71 | |
| 72 | async def __call__(self, body: object, *, extra_body: Mapping[str, object] | None = None) -> Any: |
| 73 | """The JSON that `body` is sent as, or `None` when the request went out without content.""" |
| 74 | options = make_request_options(extra_body=extra_body) |
| 75 | if isinstance(self._client, Anthropic): |
| 76 | self._client.post("/foo", body=body, options=options, cast_to=httpx2.Response) |
| 77 | else: |
| 78 | await self._client.post("/foo", body=body, options=options, cast_to=httpx2.Response) |
| 79 | return json.loads(self.request.content) if self.request.content else None |
| 80 | |
| 81 | async def query( |
| 82 | self, |
| 83 | query: Mapping[str, object] | None = None, |
| 84 | *, |
| 85 | extra_query: Mapping[str, object] | None = None, |
| 86 | extra_body: Mapping[str, object] | None = None, |
| 87 | default_query: Mapping[str, object] | None = None, |
| 88 | ) -> dict[str, str]: |
| 89 | """The query string that `query` is sent as, parsed back into a dict.""" |
| 90 | options = make_request_options(query=query, extra_query=extra_query, extra_body=extra_body) |
| 91 | client = self._client.with_options(default_query=default_query) |
| 92 | if isinstance(client, Anthropic): |
| 93 | client.get("/foo", options=options, cast_to=httpx2.Response) |
| 94 | else: |
| 95 | await client.get("/foo", options=options, cast_to=httpx2.Response) |
| 96 | return dict(self.request.url.params) |
| 97 | |
| 98 | async def form(self, body: Mapping[str, object]) -> bytes: |
| 99 | """The multipart content that `body` is sent as next to a file.""" |
| 100 | options = make_request_options(extra_headers={"Content-Type": "multipart/form-data; boundary=boundary"}) |
| 101 | files = [("file", b"contents")] |
| 102 | if isinstance(self._client, Anthropic): |
| 103 | self._client.post("/foo", body=body, files=files, options=options, cast_to=httpx2.Response) |
| 104 | else: |
| 105 | await self._client.post("/foo", body=body, files=files, options=options, cast_to=httpx2.Response) |
| 106 | return self.request.content |
| 107 | |
| 108 | |
| 109 | @pytest.fixture(params=[True, False], ids=["sync", "async"]) |