Skip to content

feat(api): update via SDK Studio #35

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 28, 2024
Merged
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
47 changes: 25 additions & 22 deletions README.md
Original file line number Diff line number Diff line change
@@ -33,14 +33,13 @@ from browserbase import Browserbase
client = Browserbase(
# This is the default and can be omitted
api_key=os.environ.get("BROWSERBASE_API_KEY"),
# or 'production' | 'local'; defaults to "production".
environment="development",
)

context = client.contexts.create(
project_id="projectId",
session = client.sessions.create(
project_id="your_project_id",
proxies=True,
)
print(context.id)
print(session.id)
```

While you can provide an `api_key` keyword argument,
@@ -60,16 +59,15 @@ from browserbase import AsyncBrowserbase
client = AsyncBrowserbase(
# This is the default and can be omitted
api_key=os.environ.get("BROWSERBASE_API_KEY"),
# or 'production' | 'local'; defaults to "production".
environment="development",
)


async def main() -> None:
context = await client.contexts.create(
project_id="projectId",
session = await client.sessions.create(
project_id="your_project_id",
proxies=True,
)
print(context.id)
print(session.id)


asyncio.run(main())
@@ -102,8 +100,9 @@ from browserbase import Browserbase
client = Browserbase()

try:
client.contexts.create(
project_id="projectId",
client.sessions.create(
project_id="your_project_id",
proxies=True,
)
except browserbase.APIConnectionError as e:
print("The server could not be reached")
@@ -147,8 +146,9 @@ client = Browserbase(
)

# Or, configure per-request:
client.with_options(max_retries=5).contexts.create(
project_id="projectId",
client.with_options(max_retries=5).sessions.create(
project_id="your_project_id",
proxies=True,
)
```

@@ -172,8 +172,9 @@ client = Browserbase(
)

# Override per-request:
client.with_options(timeout=5.0).contexts.create(
project_id="projectId",
client.with_options(timeout=5.0).sessions.create(
project_id="your_project_id",
proxies=True,
)
```

@@ -213,13 +214,14 @@ The "raw" Response object can be accessed by prefixing `.with_raw_response.` to
from browserbase import Browserbase

client = Browserbase()
response = client.contexts.with_raw_response.create(
project_id="projectId",
response = client.sessions.with_raw_response.create(
project_id="your_project_id",
proxies=True,
)
print(response.headers.get('X-My-Header'))

context = response.parse() # get the object that `contexts.create()` would have returned
print(context.id)
session = response.parse() # get the object that `sessions.create()` would have returned
print(session.id)
```

These methods return an [`APIResponse`](https://github.com/browserbase/sdk-python/tree/main/src/browserbase/_response.py) object.
@@ -233,8 +235,9 @@ The above interface eagerly reads the full response body when you make the reque
To stream the response body, use `.with_streaming_response` instead, which requires a context manager and only reads the response body once you call `.read()`, `.text()`, `.json()`, `.iter_bytes()`, `.iter_text()`, `.iter_lines()` or `.parse()`. In the async client, these are async methods.

```python
with client.contexts.with_streaming_response.create(
project_id="projectId",
with client.sessions.with_streaming_response.create(
project_id="your_project_id",
proxies=True,
) as response:
print(response.headers.get("X-My-Header"))

2 changes: 1 addition & 1 deletion SECURITY.md
Original file line number Diff line number Diff line change
@@ -20,7 +20,7 @@ or products provided by Browserbase please follow the respective company's secur

### Browserbase Terms and Policies

Please contact dev-feedback@browserbase.com for any questions or concerns regarding security of our services.
Please contact support@browserbase.com for any questions or concerns regarding security of our services.

---

4 changes: 2 additions & 2 deletions pyproject.toml
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
[project]
name = "browserbase"
version = "0.1.0-alpha.6"
description = "The official Python library for the browserbase API"
description = "The official Python library for the Browserbase API"
dynamic = ["readme"]
license = "Apache-2.0"
authors = [
{ name = "Browserbase", email = "dev-feedback@browserbase.com" },
{ name = "Browserbase", email = "support@browserbase.com" },
]
dependencies = [
"httpx>=0.23.0, <1",
2 changes: 0 additions & 2 deletions src/browserbase/__init__.py
Original file line number Diff line number Diff line change
@@ -4,7 +4,6 @@
from ._types import NOT_GIVEN, NoneType, NotGiven, Transport, ProxiesTypes
from ._utils import file_from_path
from ._client import (
ENVIRONMENTS,
Client,
Stream,
Timeout,
@@ -69,7 +68,6 @@
"AsyncStream",
"Browserbase",
"AsyncBrowserbase",
"ENVIRONMENTS",
"file_from_path",
"BaseModel",
"DEFAULT_TIMEOUT",
87 changes: 14 additions & 73 deletions src/browserbase/_client.py
Original file line number Diff line number Diff line change
@@ -3,8 +3,8 @@
from __future__ import annotations

import os
from typing import Any, Dict, Union, Mapping, cast
from typing_extensions import Self, Literal, override
from typing import Any, Union, Mapping
from typing_extensions import Self, override

import httpx

@@ -33,7 +33,6 @@
)

__all__ = [
"ENVIRONMENTS",
"Timeout",
"Transport",
"ProxiesTypes",
@@ -45,12 +44,6 @@
"AsyncClient",
]

ENVIRONMENTS: Dict[str, str] = {
"production": "https://api.browserbase.com",
"development": "https://api.dev.browserbase.com",
"local": "http://api.localhost",
}


class Browserbase(SyncAPIClient):
contexts: resources.ContextsResource
@@ -63,14 +56,11 @@ class Browserbase(SyncAPIClient):
# client options
api_key: str

_environment: Literal["production", "development", "local"] | NotGiven

def __init__(
self,
*,
api_key: str | None = None,
environment: Literal["production", "development", "local"] | NotGiven = NOT_GIVEN,
base_url: str | httpx.URL | None | NotGiven = NOT_GIVEN,
base_url: str | httpx.URL | None = None,
timeout: Union[float, Timeout, None, NotGiven] = NOT_GIVEN,
max_retries: int = DEFAULT_MAX_RETRIES,
default_headers: Mapping[str, str] | None = None,
@@ -89,7 +79,7 @@ def __init__(
# part of our public interface in the future.
_strict_response_validation: bool = False,
) -> None:
"""Construct a new synchronous browserbase client instance.
"""Construct a new synchronous Browserbase client instance.

This automatically infers the `api_key` argument from the `BROWSERBASE_API_KEY` environment variable if it is not provided.
"""
@@ -101,31 +91,10 @@ def __init__(
)
self.api_key = api_key

self._environment = environment

base_url_env = os.environ.get("BROWSERBASE_BASE_URL")
if is_given(base_url) and base_url is not None:
# cast required because mypy doesn't understand the type narrowing
base_url = cast("str | httpx.URL", base_url) # pyright: ignore[reportUnnecessaryCast]
elif is_given(environment):
if base_url_env and base_url is not None:
raise ValueError(
"Ambiguous URL; The `BROWSERBASE_BASE_URL` env var and the `environment` argument are given. If you want to use the environment, you must pass base_url=None",
)

try:
base_url = ENVIRONMENTS[environment]
except KeyError as exc:
raise ValueError(f"Unknown environment: {environment}") from exc
elif base_url_env is not None:
base_url = base_url_env
else:
self._environment = environment = "production"

try:
base_url = ENVIRONMENTS[environment]
except KeyError as exc:
raise ValueError(f"Unknown environment: {environment}") from exc
if base_url is None:
base_url = os.environ.get("BROWSERBASE_BASE_URL")
if base_url is None:
base_url = f"https://api.browserbase.com"

super().__init__(
version=__version__,
@@ -169,7 +138,6 @@ def copy(
self,
*,
api_key: str | None = None,
environment: Literal["production", "development", "local"] | None = None,
base_url: str | httpx.URL | None = None,
timeout: float | Timeout | None | NotGiven = NOT_GIVEN,
http_client: httpx.Client | None = None,
@@ -205,7 +173,6 @@ def copy(
return self.__class__(
api_key=api_key or self.api_key,
base_url=base_url or self.base_url,
environment=environment or self._environment,
timeout=self.timeout if isinstance(timeout, NotGiven) else timeout,
http_client=http_client,
max_retries=max_retries if is_given(max_retries) else self.max_retries,
@@ -263,14 +230,11 @@ class AsyncBrowserbase(AsyncAPIClient):
# client options
api_key: str

_environment: Literal["production", "development", "local"] | NotGiven

def __init__(
self,
*,
api_key: str | None = None,
environment: Literal["production", "development", "local"] | NotGiven = NOT_GIVEN,
base_url: str | httpx.URL | None | NotGiven = NOT_GIVEN,
base_url: str | httpx.URL | None = None,
timeout: Union[float, Timeout, None, NotGiven] = NOT_GIVEN,
max_retries: int = DEFAULT_MAX_RETRIES,
default_headers: Mapping[str, str] | None = None,
@@ -289,7 +253,7 @@ def __init__(
# part of our public interface in the future.
_strict_response_validation: bool = False,
) -> None:
"""Construct a new async browserbase client instance.
"""Construct a new async Browserbase client instance.

This automatically infers the `api_key` argument from the `BROWSERBASE_API_KEY` environment variable if it is not provided.
"""
@@ -301,31 +265,10 @@ def __init__(
)
self.api_key = api_key

self._environment = environment

base_url_env = os.environ.get("BROWSERBASE_BASE_URL")
if is_given(base_url) and base_url is not None:
# cast required because mypy doesn't understand the type narrowing
base_url = cast("str | httpx.URL", base_url) # pyright: ignore[reportUnnecessaryCast]
elif is_given(environment):
if base_url_env and base_url is not None:
raise ValueError(
"Ambiguous URL; The `BROWSERBASE_BASE_URL` env var and the `environment` argument are given. If you want to use the environment, you must pass base_url=None",
)

try:
base_url = ENVIRONMENTS[environment]
except KeyError as exc:
raise ValueError(f"Unknown environment: {environment}") from exc
elif base_url_env is not None:
base_url = base_url_env
else:
self._environment = environment = "production"

try:
base_url = ENVIRONMENTS[environment]
except KeyError as exc:
raise ValueError(f"Unknown environment: {environment}") from exc
if base_url is None:
base_url = os.environ.get("BROWSERBASE_BASE_URL")
if base_url is None:
base_url = f"https://api.browserbase.com"

super().__init__(
version=__version__,
@@ -369,7 +312,6 @@ def copy(
self,
*,
api_key: str | None = None,
environment: Literal["production", "development", "local"] | None = None,
base_url: str | httpx.URL | None = None,
timeout: float | Timeout | None | NotGiven = NOT_GIVEN,
http_client: httpx.AsyncClient | None = None,
@@ -405,7 +347,6 @@ def copy(
return self.__class__(
api_key=api_key or self.api_key,
base_url=base_url or self.base_url,
environment=environment or self._environment,
timeout=self.timeout if isinstance(timeout, NotGiven) else timeout,
http_client=http_client,
max_retries=max_retries if is_given(max_retries) else self.max_retries,
Loading