Skip to content

chore(internal): minor restructuring of base client #15

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Oct 27, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ dependencies = [
"typing-extensions>=4.5, <5",
"anyio>=3.5.0, <4",
"distro>=1.7.0, <2",

]
requires-python = ">= 3.7"

Expand All @@ -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/orb/_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