|
1 | | -"""A template that is a good start for vibe coding REST API Source. Works best with `dlt ai` command cursor rules""" |
| 1 | +"""Template for building a `dlt` pipeline to ingest data from a REST API.""" |
2 | 2 |
|
3 | 3 | import dlt |
4 | | -from dlt.sources.rest_api import ( |
5 | | - RESTAPIConfig, |
6 | | - rest_api_resources, |
7 | | -) |
| 4 | +from dlt.sources.rest_api import rest_api_resources |
| 5 | +from dlt.sources.rest_api.typing import RESTAPIConfig |
8 | 6 |
|
9 | 7 |
|
| 8 | +# if no argument is provided, `access_token` is read from `.dlt/secrets.toml` |
10 | 9 | @dlt.source |
11 | | -def source(access_token=dlt.secrets.value): |
| 10 | +def rest_api_source(access_token: str = dlt.secrets.value): |
| 11 | + """Define dlt resources from REST API endpoints.""" |
12 | 12 | config: RESTAPIConfig = { |
13 | 13 | "client": { |
14 | | - # TODO: place valid base url here |
| 14 | + # TODO set base URL for the REST API |
15 | 15 | "base_url": "https://example.com/v1/", |
16 | | - # TODO: configure the right auth or remove if api does not need authentication |
17 | | - # NOTE: pass secrets and other configuration in source function signature |
18 | | - "auth": { |
19 | | - "type": "bearer", |
20 | | - "token": access_token, |
21 | | - }, |
| 16 | + # TODO configure the right authentication method or remove |
| 17 | + "auth": {"type": "bearer", "token": access_token}, |
22 | 18 | }, |
23 | 19 | "resources": [ |
24 | | - # TODO: add resource definitions here |
| 20 | + # TODO define resources per endpoint |
25 | 21 | ], |
| 22 | + # set `resource_defaults` to apply configuration to all endpoints |
26 | 23 | } |
27 | 24 |
|
28 | 25 | yield from rest_api_resources(config) |
29 | 26 |
|
30 | 27 |
|
31 | | -def get_data() -> None: |
32 | | - pipeline = dlt.pipeline( |
33 | | - pipeline_name="rest_api_github", |
34 | | - destination="duckdb", |
35 | | - dataset_name="rest_api_data", |
36 | | - ) |
37 | | - |
38 | | - # TODO: during debugging feel free to pass access token explicitly |
39 | | - # NOTE: use `secrets.toml` or env variables to pass configuration in production |
40 | | - access_token = "my_access_token" |
41 | | - load_info = pipeline.run(source(access_token)) |
42 | | - print(load_info) # noqa |
| 28 | +pipeline = dlt.pipeline( |
| 29 | + pipeline_name="rest_api_ingest", |
| 30 | + destination="duckdb", |
| 31 | + # `refresh="drop_sources"` ensures the data and the state is cleaned |
| 32 | + # on each `pipeline.run()`; remove the argument once you have a |
| 33 | + # working pipeline. |
| 34 | + refresh="drop_sources", |
| 35 | +) |
43 | 36 |
|
44 | 37 |
|
45 | 38 | if __name__ == "__main__": |
46 | | - get_data() |
| 39 | + load_info = pipeline.run(rest_api_source()) |
| 40 | + print(load_info) # noqa: T201 |
0 commit comments