Skip to content

Commit f882e93

Browse files
feat(cloud): Add CloudWorkspace.from_env() classmethod (#909)
Co-authored-by: Devin AI <158243242+devin-ai-integration[bot]@users.noreply.github.com>
1 parent 441105e commit f882e93

File tree

1 file changed

+55
-0
lines changed

1 file changed

+55
-0
lines changed

airbyte/cloud/workspaces.py

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,12 @@
4545
from airbyte import exceptions as exc
4646
from airbyte._util import api_util, text_util
4747
from airbyte._util.api_util import get_web_url_root
48+
from airbyte.cloud.auth import (
49+
resolve_cloud_api_url,
50+
resolve_cloud_client_id,
51+
resolve_cloud_client_secret,
52+
resolve_cloud_workspace_id,
53+
)
4854
from airbyte.cloud.connections import CloudConnection
4955
from airbyte.cloud.connectors import (
5056
CloudDestination,
@@ -94,6 +100,55 @@ def __post_init__(self) -> None:
94100
self.client_id = SecretString(self.client_id)
95101
self.client_secret = SecretString(self.client_secret)
96102

103+
@classmethod
104+
def from_env(
105+
cls,
106+
workspace_id: str | None = None,
107+
*,
108+
api_root: str | None = None,
109+
) -> CloudWorkspace:
110+
"""Create a CloudWorkspace using credentials from environment variables.
111+
112+
This factory method resolves credentials from environment variables,
113+
providing a convenient way to create a workspace without explicitly
114+
passing credentials.
115+
116+
Environment variables used:
117+
- `AIRBYTE_CLOUD_CLIENT_ID`: Required. The OAuth client ID.
118+
- `AIRBYTE_CLOUD_CLIENT_SECRET`: Required. The OAuth client secret.
119+
- `AIRBYTE_CLOUD_WORKSPACE_ID`: The workspace ID (if not passed as argument).
120+
- `AIRBYTE_CLOUD_API_URL`: Optional. The API root URL (defaults to Airbyte Cloud).
121+
122+
Args:
123+
workspace_id: The workspace ID. If not provided, will be resolved from
124+
the `AIRBYTE_CLOUD_WORKSPACE_ID` environment variable.
125+
api_root: The API root URL. If not provided, will be resolved from
126+
the `AIRBYTE_CLOUD_API_URL` environment variable, or default to
127+
the Airbyte Cloud API.
128+
129+
Returns:
130+
A CloudWorkspace instance configured with credentials from the environment.
131+
132+
Raises:
133+
PyAirbyteSecretNotFoundError: If required credentials are not found in
134+
the environment.
135+
136+
Example:
137+
```python
138+
# With workspace_id from environment
139+
workspace = CloudWorkspace.from_env()
140+
141+
# With explicit workspace_id
142+
workspace = CloudWorkspace.from_env(workspace_id="your-workspace-id")
143+
```
144+
"""
145+
return cls(
146+
workspace_id=resolve_cloud_workspace_id(workspace_id),
147+
client_id=resolve_cloud_client_id(),
148+
client_secret=resolve_cloud_client_secret(),
149+
api_root=resolve_cloud_api_url(api_root),
150+
)
151+
97152
@property
98153
def workspace_url(self) -> str | None:
99154
"""The web URL of the workspace."""

0 commit comments

Comments
 (0)