Repository navigation
π₯ Add sandbox API client and split query params from request bodies #21
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
Changes from all commits
Commits
Show all changes
15 commits
Select commit
Hold shift + click to select a range
0e4c9ec
Update management API spec from staging
marius-baseten 34f9127
Add sandbox API client and split query params from request bodies
marius-baseten 9554c44
Resolve $ref'd and path-item parameters into operations
marius-baseten f11199d
Fix review findings: alias handling, streaming raw responses, status β¦
marius-baseten e667289
Trim comment bloat in the generator scripts
marius-baseten 54f7026
Export SandboxClient and AsyncSandboxClient from baseten.client
marius-baseten 643c03c
Address review: restore prod URLs in the management spec
marius-baseten b710216
Accept query and body on every API, matching the JS generator
marius-baseten 25fed32
Fix review findings: date-only YAML scalars, raw docstring periods
marius-baseten df2f82b
Advertise only sendable stdin types on the async client
marius-baseten d4e6df2
Add the sandbox hub image catalog to the spec and client
marius-baseten c3b9786
Rename the starter-image catalog types to library vocabulary
marius-baseten 0d275b4
Swap the starter-image catalog to the served library_images endpoint
marius-baseten 35faa27
Forward-patch the sandbox record to the served shape
marius-baseten 82ab929
Use shared management spec and tidy generated docstrings
cretz File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,205 @@ | ||
| from __future__ import annotations | ||
|
|
||
| from collections.abc import Mapping | ||
| from dataclasses import dataclass | ||
| from typing import Self | ||
|
|
||
| import httpx | ||
|
|
||
| import baseten.client.sandboxapi | ||
| from baseten.client._user_agent import with_user_agent | ||
|
|
||
|
|
||
| @dataclass(frozen=True) | ||
| class SandboxClientOptions: | ||
| """Options for :class:`SandboxClient` and :class:`AsyncSandboxClient`. | ||
|
|
||
| Obtain via :attr:`SandboxClient.options` to inspect the values a client | ||
| was constructed with. | ||
| """ | ||
|
|
||
| token: str | ||
| """Sandbox token for authentication.""" | ||
|
|
||
| base_url: str | ||
| """Base URL of the sandbox, without a trailing slash. | ||
|
|
||
| Each sandbox is reached at its own host, so there is no default. | ||
| """ | ||
|
|
||
| headers: Mapping[str, str] | None = None | ||
| """Additional headers to send on every request.""" | ||
|
|
||
|
|
||
| class SandboxClient: | ||
| """Synchronous client for one sandbox's execution-plane API. | ||
|
|
||
| Can be used as a context manager to ensure the underlying HTTP client is | ||
| closed on exit. | ||
| """ | ||
|
|
||
| def __init__( | ||
| self, | ||
| *, | ||
| token: str, | ||
| base_url: str, | ||
| headers: Mapping[str, str] | None = None, | ||
| http_client_override: httpx.Client | None = None, | ||
| close_http_client_on_close: bool | None = None, | ||
| ) -> None: | ||
| """Create a new synchronous sandbox client. | ||
|
|
||
| Args: | ||
| token: Sandbox token for authentication. | ||
| base_url: Base URL of the sandbox, without a trailing slash. | ||
| headers: Additional headers to send on every request. | ||
| http_client_override: Pre-configured httpx client. When provided, | ||
| the caller is responsible for setting base URL and all | ||
| headers. | ||
| close_http_client_on_close: Whether :meth:`close` should close the | ||
| underlying HTTP client. Defaults to ``True`` when the client | ||
| is created internally, ``False`` when *http_client_override* | ||
| is provided. | ||
| """ | ||
| self._options = SandboxClientOptions( | ||
| token=token, base_url=base_url, headers=headers | ||
| ) | ||
| if http_client_override is None: | ||
| request_headers: dict[str, str] = {**(headers or {})} | ||
| # Empty token is an advanced opt-out from sending Authorization. | ||
| if token != "": | ||
| request_headers["Authorization"] = f"Bearer {token}" | ||
| self._http_client = httpx.Client( | ||
| base_url=base_url, | ||
| headers=with_user_agent(request_headers), | ||
| ) | ||
| self.close_http_client_on_close = ( | ||
| True | ||
| if close_http_client_on_close is None | ||
| else close_http_client_on_close | ||
| ) | ||
| else: | ||
| self._http_client = http_client_override | ||
| self.close_http_client_on_close = ( | ||
| False | ||
| if close_http_client_on_close is None | ||
| else close_http_client_on_close | ||
| ) | ||
| self._api = baseten.client.sandboxapi.ApiClient(self._http_client) | ||
|
|
||
| @property | ||
| def options(self) -> SandboxClientOptions: | ||
| """Client options.""" | ||
| return self._options | ||
|
|
||
| @property | ||
| def http_client(self) -> httpx.Client: | ||
| """The underlying HTTP client.""" | ||
| return self._http_client | ||
|
|
||
| @property | ||
| def api(self) -> baseten.client.sandboxapi.ApiClient: | ||
| """The generated API client. | ||
|
|
||
| The generated API surface is not covered by stability guarantees and | ||
| may change between versions. | ||
| """ | ||
| return self._api | ||
|
|
||
| def close(self) -> None: | ||
| """Close the client, optionally closing the underlying HTTP client.""" | ||
| if self.close_http_client_on_close: | ||
| self._http_client.close() | ||
|
|
||
| def __enter__(self) -> Self: | ||
| return self | ||
|
|
||
| def __exit__(self, *args: object) -> None: | ||
| self.close() | ||
|
|
||
|
|
||
| class AsyncSandboxClient: | ||
| """Asynchronous client for one sandbox's execution-plane API. | ||
|
|
||
| Can be used as an async context manager to ensure the underlying HTTP | ||
| client is closed on exit. | ||
| """ | ||
|
|
||
| def __init__( | ||
| self, | ||
| *, | ||
| token: str, | ||
| base_url: str, | ||
| headers: Mapping[str, str] | None = None, | ||
| http_client_override: httpx.AsyncClient | None = None, | ||
| close_http_client_on_close: bool | None = None, | ||
| ) -> None: | ||
| """Create a new asynchronous sandbox client. | ||
|
|
||
| Args: | ||
| token: Sandbox token for authentication. | ||
| base_url: Base URL of the sandbox, without a trailing slash. | ||
| headers: Additional headers to send on every request. | ||
| http_client_override: Pre-configured httpx client. When provided, | ||
| the caller is responsible for setting base URL and all | ||
| headers. | ||
| close_http_client_on_close: Whether :meth:`close` should close the | ||
| underlying HTTP client. Defaults to ``True`` when the client | ||
| is created internally, ``False`` when *http_client_override* | ||
| is provided. | ||
| """ | ||
| self._options = SandboxClientOptions( | ||
| token=token, base_url=base_url, headers=headers | ||
| ) | ||
| if http_client_override is None: | ||
| request_headers: dict[str, str] = {**(headers or {})} | ||
| # Empty token is an advanced opt-out from sending Authorization. | ||
| if token != "": | ||
| request_headers["Authorization"] = f"Bearer {token}" | ||
| self._http_client = httpx.AsyncClient( | ||
| base_url=base_url, | ||
| headers=with_user_agent(request_headers), | ||
| ) | ||
| self.close_http_client_on_close = ( | ||
| True | ||
| if close_http_client_on_close is None | ||
| else close_http_client_on_close | ||
| ) | ||
| else: | ||
| self._http_client = http_client_override | ||
| self.close_http_client_on_close = ( | ||
| False | ||
| if close_http_client_on_close is None | ||
| else close_http_client_on_close | ||
| ) | ||
| self._api = baseten.client.sandboxapi.AsyncApiClient(self._http_client) | ||
|
|
||
| @property | ||
| def options(self) -> SandboxClientOptions: | ||
| """Client options.""" | ||
| return self._options | ||
|
|
||
| @property | ||
| def http_client(self) -> httpx.AsyncClient: | ||
| """The underlying HTTP client.""" | ||
| return self._http_client | ||
|
|
||
| @property | ||
| def api(self) -> baseten.client.sandboxapi.AsyncApiClient: | ||
| """The generated API client. | ||
|
|
||
| The generated API surface is not covered by stability guarantees and | ||
| may change between versions. | ||
| """ | ||
| return self._api | ||
|
|
||
| async def close(self) -> None: | ||
| """Close the client, optionally closing the underlying HTTP client.""" | ||
| if self.close_http_client_on_close: | ||
| await self._http_client.aclose() | ||
|
|
||
| async def __aenter__(self) -> Self: | ||
| return self | ||
|
|
||
| async def __aexit__(self, *args: object) -> None: | ||
| await self.close() |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.