diff --git a/pyproject.toml b/pyproject.toml index 442efdf8..7c27fdf3 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -39,7 +39,7 @@ dev-dependencies = [ "time-machine==2.9.0", "nox==2023.4.22", "dirty-equals>=0.6.0", - + ] [tool.rye.scripts] diff --git a/src/anthropic/_base_client.py b/src/anthropic/_base_client.py index 6e4be17a..5ed9f54d 100644 --- a/src/anthropic/_base_client.py +++ b/src/anthropic/_base_client.py @@ -399,18 +399,6 @@ def _build_headers(self, options: FinalRequestOptions) -> httpx.Headers: return headers - def _prepare_request( - self, - request: httpx.Request, # noqa: ARG002 - ) -> None: - """This method is used as a callback for mutating the `Request` object - after it has been constructed. - - This is useful for cases where you want to add certain headers based off of - the request properties, e.g. `url`, `method` etc. - """ - return None - def _prepare_url(self, url: str) -> URL: """ Merge a URL argument together with any 'base_url' on the client, @@ -463,7 +451,7 @@ def _build_request( kwargs["data"] = self._serialize_multipartform(json_data) # TODO: report this error to httpx - request = self._client.build_request( # pyright: ignore[reportUnknownMemberType] + return self._client.build_request( # pyright: ignore[reportUnknownMemberType] headers=headers, timeout=self.timeout if isinstance(options.timeout, NotGiven) else options.timeout, method=options.method, @@ -477,8 +465,6 @@ def _build_request( files=options.files, **kwargs, ) - self._prepare_request(request) - return request def _serialize_multipartform(self, data: Mapping[object, object]) -> dict[str, object]: items = self.qs.stringify_items( @@ -781,6 +767,24 @@ def __exit__( ) -> None: self.close() + def _prepare_options( + self, + options: FinalRequestOptions, # noqa: ARG002 + ) -> None: + """Hook for mutating the given options""" + return None + + def _prepare_request( + self, + request: httpx.Request, # noqa: ARG002 + ) -> None: + """This method is used as a callback for mutating the `Request` object + after it has been constructed. + This is useful for cases where you want to add certain headers based off of + the request properties, e.g. `url`, `method` etc. + """ + return None + @overload def request( self, @@ -842,8 +846,11 @@ def _request( stream: bool, stream_cls: type[_StreamT] | None, ) -> ResponseT | _StreamT: + self._prepare_options(options) + retries = self._remaining_retries(remaining_retries, options) request = self._build_request(options) + self._prepare_request(request) try: response = self._client.send(request, auth=self.custom_auth, stream=stream) @@ -1201,6 +1208,24 @@ async def __aexit__( ) -> None: await self.close() + async def _prepare_options( + self, + options: FinalRequestOptions, # noqa: ARG002 + ) -> None: + """Hook for mutating the given options""" + return None + + async def _prepare_request( + self, + request: httpx.Request, # noqa: ARG002 + ) -> None: + """This method is used as a callback for mutating the `Request` object + after it has been constructed. + This is useful for cases where you want to add certain headers based off of + the request properties, e.g. `url`, `method` etc. + """ + return None + @overload async def request( self, @@ -1262,8 +1287,11 @@ async def _request( stream_cls: type[_AsyncStreamT] | None, remaining_retries: int | None, ) -> ResponseT | _AsyncStreamT: + await self._prepare_options(options) + retries = self._remaining_retries(remaining_retries, options) request = self._build_request(options) + await self._prepare_request(request) try: response = await self._client.send(request, auth=self.custom_auth, stream=stream)