|
45 | 45 | from airbyte import exceptions as exc |
46 | 46 | from airbyte._util import api_util, text_util |
47 | 47 | 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 | +) |
48 | 54 | from airbyte.cloud.connections import CloudConnection |
49 | 55 | from airbyte.cloud.connectors import ( |
50 | 56 | CloudDestination, |
@@ -94,6 +100,55 @@ def __post_init__(self) -> None: |
94 | 100 | self.client_id = SecretString(self.client_id) |
95 | 101 | self.client_secret = SecretString(self.client_secret) |
96 | 102 |
|
| 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 | + |
97 | 152 | @property |
98 | 153 | def workspace_url(self) -> str | None: |
99 | 154 | """The web URL of the workspace.""" |
|
0 commit comments