Skip to content

Commit

Permalink
chore(internal): minor restructuring of base client (#213)
Browse files Browse the repository at this point in the history
  • Loading branch information
stainless-bot authored Oct 27, 2023
1 parent ce81114 commit 60dc609
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 16 deletions.
2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ dev-dependencies = [
"time-machine==2.9.0",
"nox==2023.4.22",
"dirty-equals>=0.6.0",

]

[tool.rye.scripts]
Expand Down
58 changes: 43 additions & 15 deletions src/anthropic/_base_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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,
Expand All @@ -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(
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -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)
Expand Down

0 comments on commit 60dc609

Please sign in to comment.