Skip to content

Commit f669602

Browse files
feat: fetch license mappings from registry
1 parent 9172f53 commit f669602

File tree

2 files changed

+84
-0
lines changed

2 files changed

+84
-0
lines changed

oc4ids_datastore_pipeline/pipeline.py

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,25 @@ def fetch_registered_datasets() -> dict[str, str]:
3030
raise Exception("Failed to fetch datasets list from registry", e)
3131

3232

33+
def fetch_license_mappings() -> dict[str, str]:
34+
logger.info("Fetching license mappings from registry")
35+
try:
36+
url = "https://opendataservices.github.io/oc4ids-registry/datatig/type/license/records_api.json" # noqa: E501
37+
r = requests.get(url)
38+
r.raise_for_status()
39+
json_data = r.json()
40+
return {
41+
urls["fields"]["url"]["value"]: license["fields"]["title"]["value"]
42+
for license in json_data["records"].values()
43+
for urls in license["fields"]["urls"]["values"]
44+
}
45+
except Exception as e:
46+
logger.warning(
47+
"Failed to fetch license mappings from registry, with error: " + str(e),
48+
)
49+
return {}
50+
51+
3352
def download_json(url: str) -> Any:
3453
logger.info(f"Downloading json from {url}")
3554
try:

tests/test_pipeline.py

Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88

99
from oc4ids_datastore_pipeline.pipeline import (
1010
download_json,
11+
fetch_license_mappings,
1112
fetch_registered_datasets,
1213
process_dataset,
1314
validate_json,
@@ -43,6 +44,70 @@ def test_fetch_registered_datasets_raises_failure_exception(
4344
assert "Mocked exception" in str(exc_info.value)
4445

4546

47+
def test_fetch_license_mappings(mocker: MockerFixture) -> None:
48+
mock_response = MagicMock()
49+
mock_response.json.return_value = {
50+
"records": {
51+
"license_1": {
52+
"fields": {
53+
"title": {"value": "License 1"},
54+
"urls": {
55+
"values": [
56+
{
57+
"fields": {
58+
"url": {"value": "https://license_1.com/license"}
59+
}
60+
},
61+
{
62+
"fields": {
63+
"url": {
64+
"value": "https://license_1.com/different_url"
65+
}
66+
}
67+
},
68+
]
69+
},
70+
}
71+
},
72+
"license_2": {
73+
"fields": {
74+
"title": {"value": "License 2"},
75+
"urls": {
76+
"values": [
77+
{
78+
"fields": {
79+
"url": {"value": "https://license_2.com/license"}
80+
}
81+
},
82+
]
83+
},
84+
}
85+
},
86+
}
87+
}
88+
patch_get = mocker.patch("oc4ids_datastore_pipeline.pipeline.requests.get")
89+
patch_get.return_value = mock_response
90+
91+
result = fetch_license_mappings()
92+
93+
assert result == {
94+
"https://license_1.com/license": "License 1",
95+
"https://license_1.com/different_url": "License 1",
96+
"https://license_2.com/license": "License 2",
97+
}
98+
99+
100+
def test_fetch_license_mappings_catches_exception(
101+
mocker: MockerFixture,
102+
) -> None:
103+
patch_get = mocker.patch("oc4ids_datastore_pipeline.pipeline.requests.get")
104+
patch_get.side_effect = Exception("Mocked exception")
105+
106+
result = fetch_license_mappings()
107+
108+
assert result == {}
109+
110+
46111
def test_download_json_raises_failure_exception(mocker: MockerFixture) -> None:
47112
patch_get = mocker.patch("oc4ids_datastore_pipeline.pipeline.requests.get")
48113
patch_get.side_effect = Exception("Mocked exception")

0 commit comments

Comments
 (0)