Skip to content

Commit 740deeb

Browse files
authored
Docs: Add code samples/snippets; Feat: add parse_json() as convenience method on Handles (#182)
1 parent 011315c commit 740deeb

File tree

3 files changed

+59
-32
lines changed

3 files changed

+59
-32
lines changed

airbyte/cloud/__init__.py

Lines changed: 31 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,23 +3,50 @@
33
44
You can use this module to interact with Airbyte Cloud, OSS, and Enterprise.
55
6-
Usage example:
6+
## Examples
7+
8+
### Basic Usage Example:
79
810
```python
911
import airbyte as ab
1012
from airbyte import cloud
1113
14+
# Initialize an Airbyte Cloud workspace object
1215
workspace = cloud.CloudWorkspace(
1316
workspace_id="123",
1417
api_key=ab.get_secret("AIRBYTE_CLOUD_API_KEY"),
1518
)
1619
17-
sync_result = workspace.run_sync(
18-
connection_id="456",
19-
)
20+
# Run a sync job on Airbyte Cloud
21+
connection = workspace.get_connection(connection_id="456")
22+
sync_result = connection.run_sync()
2023
print(sync_result.get_job_status())
2124
```
2225
26+
### Example Read From Cloud Destination:
27+
28+
If your destination is supported, you can read records directly from the
29+
`SyncResult` object. Currently this is supported in Snowflake and BigQuery only.
30+
31+
32+
```
33+
# Assuming we've already created a `connection` object
34+
35+
# Get the latest job result and print the stream names
36+
sync_result = connection.get_sync_result()
37+
print(sync_result.stream_names)
38+
39+
# Get a dataset from the sync result
40+
dataset: CachedDataset = sync_result.get_dataset("users")
41+
42+
# Get a SQLAlchemy table to use in SQL queries...
43+
users_table = dataset.to_sql_table()
44+
print(f"Table name: {users_table.name}")
45+
46+
# Or iterate over the dataset directly
47+
for record in dataset:
48+
print(record)
49+
```
2350
2451
ℹ️ **Experimental Features**
2552

airbyte/secrets/base.py

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -126,7 +126,8 @@ class SecretHandle:
126126
"""A handle for a secret in a secret manager.
127127
128128
This class is used to store a reference to a secret in a secret manager.
129-
The secret is not retrieved until the `get_value()` method is called on the handle.
129+
The secret is not retrieved until the `get_value()` or `parse_json()` methods are
130+
called.
130131
"""
131132

132133
def __init__(
@@ -144,3 +145,12 @@ def get_value(self) -> SecretString:
144145
Subclasses can optionally override this method to provide a more optimized code path.
145146
"""
146147
return cast(SecretString, self.parent.get_secret(self.secret_name))
148+
149+
def parse_json(self) -> dict:
150+
"""Parse the secret as JSON.
151+
152+
This method is a convenience method to parse the secret as JSON without
153+
needing to call `get_value()` first. If the secret is not a valid JSON
154+
string, a `PyAirbyteInputError` will be raised.
155+
"""
156+
return self.get_value().parse_json()

airbyte/secrets/google_gsm.py

Lines changed: 17 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -4,39 +4,29 @@
44
Usage Example:
55
66
```python
7-
gsm_secrets_manager = GoogleGSMSecretManager(
8-
project=AIRBYTE_INTERNAL_GCP_PROJECT,
7+
import ab
8+
from airbyte import secrets
9+
10+
# Initialize the Google GSM secret manager
11+
gsm = secrets.GoogleGSMSecretManager(
12+
project="my-project",
913
credentials_json=ab.get_secret("GCP_GSM_CREDENTIALS"),
1014
)
11-
first_secret: SecretHandle = next(
12-
gsm_secrets_manager.fetch_connector_secrets(
13-
connector_name=connector_name,
14-
),
15-
None,
16-
)
15+
# Get secrets with the label 'connector=source-github'
16+
secrets = gsm.fetch_connector_secrets("source-github")
1717
18-
print(f"Found '{connector_name}' credential secret '${first_secret.secret_name}'.")
19-
return first_secret.get_value().parse_json()
20-
```
18+
# Get the first secret and parse the JSON value
19+
config: dict = next(secrets, None).parse_json()
2120
22-
More compact example:
23-
24-
```python
25-
gsm_secrets_manager = GoogleGSMSecretManager(
26-
project=AIRBYTE_INTERNAL_GCP_PROJECT,
27-
credentials_json=ab.get_secret("GCP_GSM_CREDENTIALS"),
28-
)
29-
connector_config: dict = (
30-
next(
31-
gsm_secrets_manager.fetch_connector_secrets(
32-
connector_name=connector_name,
33-
),
34-
None,
35-
)
36-
.get_value()
37-
.parse_json()
21+
# Pass the config to your source
22+
source = ab.get_source(
23+
"source-github",
24+
config=config,
25+
streams="*",
3826
)
27+
read_result = source.read()
3928
```
29+
4030
"""
4131

4232
from __future__ import annotations

0 commit comments

Comments
 (0)