Skip to content

Commit 727c48b

Browse files
committed
chg: accumulate multiple params to list and logs
1 parent 7b7453e commit 727c48b

2 files changed

Lines changed: 31 additions & 32 deletions

File tree

src/datasync/nva/nva_search_resources.py

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
from collections import defaultdict
2+
13
import typer
24

35
from ..settings import (
@@ -61,8 +63,8 @@ def search_resources_api(
6163
"""
6264
Get resources from NVA API
6365
64-
This will first fetch the data from the NVA API based on the search parameters
65-
then write the data to the specified S3 location
66+
This will use the NVA API to fetch resources based on the provided parameters.
67+
The data will be written to the specified S3 location in parquet format.
6668
6769
Args:
6870
resource_name: Will be used for the output files on S3, avoid using '-'.
@@ -91,7 +93,7 @@ def search_resources_api(
9193
# Filter by contributor and publication year:
9294
uv run datasync nva search-resources-api \
9395
--resource-name "author-publications" \
94-
--filter contributor="https://api.nva.unit.no/cristin/person/1773250" \\
96+
--filter contributor="https://api.nva.unit.no/cristin/person/1773250" \
9597
--filter publication_year_since=2020
9698
"""
9799
log.debug("Starting NVA API search with parameters", filters=filters)
@@ -109,7 +111,7 @@ def search_resources_api(
109111
"in the output file names due to DLT naming conventions."
110112
)
111113

112-
search_params = {}
114+
search_params: defaultdict[str, list[str]] = defaultdict(list)
113115
for filter_str in filters:
114116
key, value = filter_str.split("=", 1)
115117
key = key.strip()
@@ -119,7 +121,8 @@ def search_resources_api(
119121
log.error("Valid filters", valid_filters=VALID_PARAMS_NVA_API)
120122
raise typer.Exit(code=1)
121123

122-
search_params[key] = value
124+
search_params[key].append(value)
125+
123126
log.debug("Parsed search parameters", search_params=search_params)
124127
if not search_params:
125128
log.error("Valid filters", valid_filters=VALID_PARAMS_NVA_API)
@@ -143,18 +146,16 @@ def search_resources_api(
143146
)
144147

145148
log.info(f"Fetching resources from {base_url}")
146-
load_info = pipeline.run(
149+
pipeline.run(
147150
nva_search_source(
148151
base_url=base_url,
149152
resource_name=resource_name,
150-
search_params=search_params,
153+
**search_params,
151154
),
152155
write_disposition="replace",
153156
loader_file_format="parquet",
154157
)
155158

156-
log.info("Pipeline run completed", load_info=load_info)
157-
158159
if apply_filter:
159160
con = setup_duckdb_s3_connection(
160161
endpoint_url=storage_endpoint_url,
@@ -174,7 +175,6 @@ def search_resources_api(
174175
write_timestamp(con, storage_bucket, storage_prefix)
175176
con.close()
176177

177-
log.info("NVA data sync completed")
178178
log.info(
179179
f"Data available at: {storage_endpoint_url}/{storage_bucket}/{storage_prefix}/"
180180
f"main/{resource_name}.parquet"

src/datasync/nva/utils.py

Lines changed: 21 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313

1414
def get_nva_resources(
1515
client: RESTClient,
16-
search_params: dict[str, str],
16+
search_params: dict[str, list[str]],
1717
):
1818
"""
1919
Function to fetch resources from NVA search API.
@@ -38,42 +38,39 @@ def get_nva_resources(
3838
def nva_search_source(
3939
base_url: str,
4040
resource_name: str = "resources",
41-
search_params: dict[str, str] | None = None,
41+
**kwargs: list[str],
4242
):
4343
"""
4444
DLT source for fetching resources from NVA search API.
4545
4646
Args:
4747
base_url: NVA API base URL
4848
resource_name: Name for the DLT resource
49-
search_params: Dictionary of search parameters (project, publisher, etc.)
49+
**kwargs: Search parameters as keyword arguments (project, publisher, etc.)
5050
5151
Example:
52-
# For project-based search:
53-
nva_search_source(
54-
resource_name="renew_hydro_resources",
55-
search_params={"project": "https://api.nva.unit.no/cristin/project/2732649"}
56-
)
57-
58-
# For publisher-based search:
59-
nva_search_source(
60-
resource_name="atlantic_salmon_resources",
61-
search_params={"publisher": "Vitenskapelig råd for lakseforvaltning"}
62-
)
63-
64-
# For more parameters: https://swagger-ui.nva.unit.no/
65-
"""
66-
if search_params is None:
67-
search_params = {}
52+
# For project-based search:
53+
nva_search_source(
54+
resource_name="renew_hydro_resources",
55+
project=["https://api.nva.unit.no/cristin/project/2732649"]
56+
)
57+
58+
# For publisher-based search:
59+
nva_search_source(
60+
resource_name="atlantic_salmon_resources",
61+
publisher=["Vitenskapelig råd for lakseforvaltning"]
62+
)
6863
64+
# For more parameters: https://swagger-ui.nva.unit.no/
65+
"""
6966
client = RESTClient(
7067
base_url=base_url,
7168
paginator=JSONLinkPaginator(next_url_path="nextResults"),
7269
data_selector="hits",
7370
)
7471

7572
yield dlt.resource(
76-
get_nva_resources(client, search_params),
73+
get_nva_resources(client, kwargs),
7774
name=resource_name,
7875
write_disposition="replace",
7976
primary_key="identifier",
@@ -203,5 +200,7 @@ def apply_filter_transformation(
203200
compression="zstd",
204201
overwrite=True,
205202
)
206-
207-
log.info("Filter applied and data written back to S3")
203+
log.info(
204+
"Filter applied and data written back to S3",
205+
number_of_resources=len(filtered_resources),
206+
)

0 commit comments

Comments
 (0)