Skip to content

Commit 42e5caf

Browse files
committed
chg(doffin): add doffin pipeline
1 parent 727c48b commit 42e5caf

2 files changed

Lines changed: 103 additions & 0 deletions

File tree

src/datasync/doffin.py

Lines changed: 101 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,101 @@
1+
import dlt
2+
import typer
3+
from dlt.destinations.impl.filesystem.factory import filesystem
4+
from dlt.sources.credentials import AwsCredentials
5+
from dlt.sources.rest_api import rest_api_source
6+
from dlt.sources.rest_api.config_setup import PageNumberPaginator
7+
8+
from .settings import env, log
9+
10+
app = typer.Typer(help="Export Doffin notices to Parquet in S3 bucket")
11+
12+
DOFFIN_API_KEY = env("DOFFIN_API_KEY", default="")
13+
DOFFIN_BASE_URL = env("DOFFIN_BASE_URL", default="https://api.doffin.no/public/v2/")
14+
DOFFIN_PAGE_SIZE = int(env("DOFFIN_PAGE_SIZE", default="100"))
15+
16+
DOFFIN_AWS_ACCESS_KEY = env("DOFFIN_AWS_ACCESS_KEY", default="")
17+
DOFFIN_AWS_SECRET_KEY = env("DOFFIN_AWS_SECRET_KEY", default="")
18+
DOFFIN_AWS_ENDPOINT = env("DOFFIN_AWS_ENDPOINT", default="")
19+
DOFFIN_AWS_BUCKET = env("DOFFIN_AWS_BUCKET", default="")
20+
DOFFIN_AWS_PREFIX = env("DOFFIN_AWS_PREFIX", default="doffin")
21+
DOFFIN_S3_REGION = env("DOFFIN_S3_REGION", default="us-east-1")
22+
23+
24+
@app.command()
25+
def run(
26+
api_key: str = DOFFIN_API_KEY,
27+
base_url: str = DOFFIN_BASE_URL,
28+
page_size: int = DOFFIN_PAGE_SIZE,
29+
access_key: str = DOFFIN_AWS_ACCESS_KEY,
30+
secret_key: str = DOFFIN_AWS_SECRET_KEY,
31+
endpoint_url: str = DOFFIN_AWS_ENDPOINT,
32+
bucket: str = DOFFIN_AWS_BUCKET,
33+
prefix: str = DOFFIN_AWS_PREFIX,
34+
region: str = DOFFIN_S3_REGION,
35+
):
36+
source = rest_api_source(
37+
{
38+
"client": {
39+
"base_url": base_url,
40+
"headers": {"Ocp-Apim-Subscription-Key": api_key},
41+
},
42+
"resources": [
43+
{
44+
"name": "notices",
45+
"endpoint": {
46+
"path": "search",
47+
"params": {"numHitsPerPage": page_size},
48+
"data_selector": "hits",
49+
"paginator": PageNumberPaginator(
50+
page_param="page",
51+
base_page=1,
52+
total_path=None,
53+
# doffin api doesn't allow more than 1.000 rows
54+
# being fetched
55+
maximum_page=1000 // page_size,
56+
),
57+
},
58+
}
59+
],
60+
}
61+
)
62+
63+
credentials = AwsCredentials(
64+
s3_url_style="path",
65+
endpoint_url=endpoint_url,
66+
aws_secret_access_key=secret_key,
67+
aws_access_key_id=access_key,
68+
region_name=region,
69+
)
70+
71+
pipeline = dlt.pipeline(
72+
pipeline_name="doffin",
73+
destination=filesystem(
74+
bucket_url=f"s3://{bucket}/{prefix}",
75+
credentials=credentials,
76+
layout="{table_name}.{ext}",
77+
),
78+
dataset_name="doffin",
79+
)
80+
81+
load_info = pipeline.run(
82+
source, loader_file_format="parquet", write_disposition="replace"
83+
)
84+
85+
row_counts = {}
86+
if pipeline.last_trace and pipeline.last_trace.last_normalize_info:
87+
row_counts = pipeline.last_trace.last_normalize_info.row_counts or {}
88+
89+
notices_rows_loaded = row_counts.get("notices", 0)
90+
91+
log.info(
92+
"doffin load complete",
93+
path=f"s3://{bucket}/{prefix}/notices.parquet",
94+
load_id=load_info.loads_ids[0] if load_info.loads_ids else None,
95+
rows_loaded=notices_rows_loaded,
96+
row_counts=row_counts,
97+
)
98+
99+
100+
if __name__ == "__main__":
101+
app()

src/datasync/main.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66

77
from . import (
88
dms,
9+
doffin,
910
gbif_backbone,
1011
grass,
1112
ninagen,
@@ -21,6 +22,7 @@
2122
)
2223
app.add_typer(nva.app, name="nva")
2324
app.add_typer(ubw.app, name="ubw")
25+
app.add_typer(doffin.app, name="doffin")
2426
app.add_typer(dms.app, name="dms")
2527
app.add_typer(ninagen.app, name="ninagen")
2628
app.add_typer(pit_registering_salmon.app, name="pit-registering-salmon")

0 commit comments

Comments
 (0)