prefect_dbt.cloud.credentials
¶
Module containing credentials for interacting with dbt Cloud
DbtCloudCredentials
¶
Bases: CredentialsBlock
Credentials block for credential use across dbt Cloud tasks and flows.
Attributes:
Name | Type | Description |
---|---|---|
api_key |
SecretStr
|
API key to authenticate with the dbt Cloud administrative API. Refer to the Authentication docs for retrieving the API key. |
account_id |
int
|
ID of dbt Cloud account with which to interact. |
domain |
Optional[str]
|
Domain at which the dbt Cloud API is hosted. |
Examples:
Load stored dbt Cloud credentials:
from prefect_dbt.cloud import DbtCloudCredentials
dbt_cloud_credentials = DbtCloudCredentials.load("BLOCK_NAME")
Use DbtCloudCredentials instance to trigger a job run:
from prefect_dbt.cloud import DbtCloudCredentials
credentials = DbtCloudCredentials(api_key="my_api_key", account_id=123456789)
async with dbt_cloud_credentials.get_administrative_client() as client:
client.trigger_job_run(job_id=1)
Load saved dbt Cloud credentials within a flow:
from prefect import flow
from prefect_dbt.cloud import DbtCloudCredentials
from prefect_dbt.cloud.jobs import trigger_dbt_cloud_job_run
@flow
def trigger_dbt_cloud_job_run_flow():
credentials = DbtCloudCredentials.load("my-dbt-credentials")
trigger_dbt_cloud_job_run(dbt_cloud_credentials=credentials, job_id=1)
trigger_dbt_cloud_job_run_flow()
Source code in prefect_dbt/cloud/credentials.py
21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 |
|
get_administrative_client
¶
Returns a newly instantiated client for working with the dbt Cloud administrative API.
Returns:
Type | Description |
---|---|
DbtCloudAdministrativeClient
|
An authenticated dbt Cloud administrative API client. |
Source code in prefect_dbt/cloud/credentials.py
85 86 87 88 89 90 91 92 93 94 95 96 97 |
|
get_client
¶
Returns a newly instantiated client for working with the dbt Cloud API.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
client_type |
Literal['administrative', 'metadata']
|
Type of client to return. Accepts either 'administrative' or 'metadata'. |
required |
Returns:
Type | Description |
---|---|
Union[DbtCloudAdministrativeClient, DbtCloudMetadataClient]
|
The authenticated client of the requested type. |
Source code in prefect_dbt/cloud/credentials.py
175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 |
|
get_metadata_client
¶
Returns a newly instantiated client for working with the dbt Cloud metadata API.
Example
Sending queries via the returned metadata client:
from prefect_dbt import DbtCloudCredentials
credentials_block = DbtCloudCredentials.load("test-account")
metadata_client = credentials_block.get_metadata_client()
query = """
{
metrics(jobId: 123) {
uniqueId
name
packageName
tags
label
runId
description
type
sql
timestamp
timeGrains
dimensions
meta
resourceType
filters {
field
operator
value
}
model {
name
}
}
}
"""
metadata_client.query(query)
# Result:
# {
# "data": {
# "metrics": [
# {
# "uniqueId": "metric.tpch.total_revenue",
# "name": "total_revenue",
# "packageName": "tpch",
# "tags": [],
# "label": "Total Revenue ($)",
# "runId": 108952046,
# "description": "",
# "type": "sum",
# "sql": "net_item_sales_amount",
# "timestamp": "order_date",
# "timeGrains": ["day", "week", "month"],
# "dimensions": ["status_code", "priority_code"],
# "meta": {},
# "resourceType": "metric",
# "filters": [],
# "model": { "name": "fct_orders" }
# }
# ]
# }
# }
Returns:
Type | Description |
---|---|
DbtCloudMetadataClient
|
An authenticated dbt Cloud metadata API client. |
Source code in prefect_dbt/cloud/credentials.py
99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 |
|