Skip to content

Commit c1c765f

Browse files
authored
HDXDSYS-2507 Add optional retriever parameter to resource download method of HDX Python API (#94)
* Allow retriever to be passed to download method
1 parent 0bc4baf commit c1c765f

3 files changed

Lines changed: 44 additions & 27 deletions

File tree

requirements.txt

Lines changed: 23 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ attrs==25.4.0
1212
# referencing
1313
babel==2.17.0
1414
# via mkdocs-material
15-
backrefs==5.9
15+
backrefs==6.0.1
1616
# via mkdocs-material
1717
cachetools==6.2.1
1818
# via google-auth
@@ -22,17 +22,17 @@ cfgv==3.4.0
2222
# via pre-commit
2323
chardet==5.2.0
2424
# via frictionless
25-
charset-normalizer==3.4.3
25+
charset-normalizer==3.4.4
2626
# via requests
27-
ckanapi==4.8
27+
ckanapi==4.9
2828
# via hdx-python-api (pyproject.toml)
2929
click==8.3.0
3030
# via
3131
# mkdocs
3232
# typer
3333
colorama==0.4.6
3434
# via mkdocs-material
35-
coverage==7.10.7
35+
coverage==7.11.3
3636
# via pytest-cov
3737
defopt==7.0.0
3838
# via hdx-python-api (pyproject.toml)
@@ -44,7 +44,7 @@ docopt==0.6.2
4444
# via
4545
# ckanapi
4646
# num2words
47-
docutils==0.22.2
47+
docutils==0.22.3
4848
# via defopt
4949
email-validator==2.3.0
5050
# via hdx-python-api (pyproject.toml)
@@ -56,7 +56,7 @@ frictionless==5.18.1
5656
# via hdx-python-utilities
5757
ghp-import==2.1.0
5858
# via mkdocs
59-
google-auth==2.41.1
59+
google-auth==2.43.0
6060
# via
6161
# google-auth-oauthlib
6262
# gspread
@@ -70,7 +70,7 @@ hdx-python-utilities==3.9.5
7070
# via
7171
# hdx-python-api (pyproject.toml)
7272
# hdx-python-country
73-
humanize==4.13.0
73+
humanize==4.14.0
7474
# via frictionless
7575
identify==2.6.15
7676
# via pre-commit
@@ -82,7 +82,7 @@ ijson==3.4.0.post0
8282
# via hdx-python-utilities
8383
inflect==7.5.0
8484
# via quantulum3
85-
iniconfig==2.1.0
85+
iniconfig==2.3.0
8686
# via pytest
8787
isodate==0.7.2
8888
# via frictionless
@@ -110,14 +110,14 @@ loguru==0.7.3
110110
# via hdx-python-utilities
111111
makefun==1.16.0
112112
# via hdx-python-api (pyproject.toml)
113-
markdown==3.9
113+
markdown==3.10
114114
# via
115115
# mkdocs
116116
# mkdocs-material
117117
# pymdown-extensions
118118
markdown-it-py==4.0.0
119119
# via rich
120-
marko==2.2.0
120+
marko==2.2.1
121121
# via frictionless
122122
markupsafe==3.0.3
123123
# via
@@ -137,7 +137,7 @@ mkdocs==1.6.1
137137
# mkdocs-material
138138
mkdocs-get-deps==0.2.0
139139
# via mkdocs
140-
mkdocs-material==9.6.21
140+
mkdocs-material==9.7.0
141141
# via mkapi
142142
mkdocs-material-extensions==1.3.1
143143
# via mkdocs-material
@@ -175,28 +175,28 @@ ply==3.11
175175
# libhxl
176176
pockets==0.9.1
177177
# via sphinxcontrib-napoleon
178-
pre-commit==4.3.0
178+
pre-commit==4.4.0
179179
# via hdx-python-api (pyproject.toml)
180180
pyasn1==0.6.1
181181
# via
182182
# pyasn1-modules
183183
# rsa
184184
pyasn1-modules==0.4.2
185185
# via google-auth
186-
pydantic==2.12.0
186+
pydantic==2.12.4
187187
# via frictionless
188-
pydantic-core==2.41.1
188+
pydantic-core==2.41.5
189189
# via pydantic
190190
pygments==2.19.2
191191
# via
192192
# mkdocs-material
193193
# pytest
194194
# rich
195-
pymdown-extensions==10.16.1
195+
pymdown-extensions==10.17.1
196196
# via mkdocs-material
197197
pyphonetics==0.5.3
198198
# via hdx-python-utilities
199-
pytest==8.4.2
199+
pytest==9.0.0
200200
# via
201201
# hdx-python-api (pyproject.toml)
202202
# pytest-check
@@ -232,7 +232,7 @@ quantulum3==0.9.2
232232
# via hdx-python-api (pyproject.toml)
233233
ratelimit==2.2.1
234234
# via hdx-python-utilities
235-
referencing==0.36.2
235+
referencing==0.37.0
236236
# via
237237
# jsonschema
238238
# jsonschema-specifications
@@ -245,21 +245,21 @@ requests==2.32.5
245245
# mkdocs-material
246246
# requests-file
247247
# requests-oauthlib
248-
requests-file==2.1.0
248+
requests-file==3.0.1
249249
# via hdx-python-utilities
250250
requests-oauthlib==2.0.0
251251
# via google-auth-oauthlib
252252
rfc3986==2.0.0
253253
# via frictionless
254254
rich==14.2.0
255255
# via typer
256-
rpds-py==0.27.1
256+
rpds-py==0.28.0
257257
# via
258258
# jsonschema
259259
# referencing
260260
rsa==4.9.1
261261
# via google-auth
262-
ruamel-yaml==0.18.15
262+
ruamel-yaml==0.18.16
263263
# via hdx-python-utilities
264264
ruamel-yaml-clib==0.2.14
265265
# via ruamel-yaml
@@ -279,7 +279,7 @@ six==1.17.0
279279
# sphinxcontrib-napoleon
280280
sphinxcontrib-napoleon==0.7
281281
# via defopt
282-
structlog==25.4.0
282+
structlog==25.5.0
283283
# via libhxl
284284
tableschema-to-template==0.0.13
285285
# via hdx-python-utilities
@@ -291,7 +291,7 @@ text-unidecode==1.3
291291
# via python-slugify
292292
typeguard==4.4.4
293293
# via inflect
294-
typer==0.19.2
294+
typer==0.20.0
295295
# via frictionless
296296
typing-extensions==4.15.0
297297
# via
@@ -313,7 +313,7 @@ urllib3==2.5.0
313313
# requests
314314
validators==0.35.0
315315
# via frictionless
316-
virtualenv==20.35.3
316+
virtualenv==20.35.4
317317
# via pre-commit
318318
watchdog==6.0.0
319319
# via mkdocs

src/hdx/data/resource.py

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
from hdx.data.resource_view import ResourceView
1616
from hdx.utilities.dateparse import now_utc, now_utc_notz, parse_date
1717
from hdx.utilities.downloader import Download
18+
from hdx.utilities.retriever import Retrieve
1819
from hdx.utilities.typehint import ListTuple
1920
from hdx.utilities.uuid import is_valid_uuid
2021

@@ -591,12 +592,15 @@ def search_in_hdx(
591592
logger.debug(result)
592593
return resources
593594

594-
def download(self, folder: Optional[str] = None) -> Tuple[str, str]:
595+
def download(
596+
self, folder: Optional[str] = None, retriever: Optional[Retrieve] = None
597+
) -> Tuple[str, str]:
595598
"""Download resource store to provided folder or temporary folder if no folder
596599
supplied
597600
598601
Args:
599602
folder (Optional[str]): Folder to download resource to. Defaults to None.
603+
retriever (Optional[Retrieve]): Retrieve object to use. Defaults to None.
600604
601605
Returns:
602606
Tuple[str, str]: (URL downloaded, Path to downloaded file)
@@ -613,14 +617,16 @@ def download(self, folder: Optional[str] = None) -> Tuple[str, str]:
613617
filename = f"{filename}{file_format}"
614618
apikey = self.configuration.get_api_key()
615619
if apikey:
616-
headers = {"Authorization": self.configuration.get_api_key()}
620+
headers = {"Authorization": apikey}
617621
else:
618622
headers = None
619623
with Download(
620624
full_agent=self.configuration.get_user_agent(),
621625
use_env=False,
622626
headers=headers,
623627
) as downloader:
628+
if retriever:
629+
downloader = retriever.clone(downloader)
624630
path = downloader.download_file(url, folder=folder, filename=filename)
625631
return url, path
626632

tests/hdx/data/test_resource.py

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,9 @@
1616
from hdx.data.resource import Resource
1717
from hdx.utilities.dateparse import parse_date
1818
from hdx.utilities.dictandlist import merge_two_dictionaries
19-
from hdx.utilities.downloader import DownloadError
19+
from hdx.utilities.downloader import Download, DownloadError
20+
from hdx.utilities.path import get_temp_dir
21+
from hdx.utilities.retriever import Retrieve
2022

2123
resultdict = {
2224
"cache_last_updated": None,
@@ -940,17 +942,26 @@ def test_search_in_hdx(self, configuration, search):
940942

941943
def test_download(self, configuration, read):
942944
resource = Resource.read_from_hdx("74b74ae1-df0c-4716-829f-4f939a046811")
943-
resource2 = Resource.read_from_hdx("74b74ae1-df0c-4716-829f-4f939a046814")
944945
url, path = resource.download()
945946
remove(path)
946947
assert (
947948
url
948949
== "https://raw.githubusercontent.com/OCHA-DAP/hdx-python-api/main/tests/fixtures/test_data.csv"
949950
)
950951
assert basename(path) == "MyResource1.csv"
952+
tempdir = get_temp_dir()
953+
retriever = Retrieve(Download(user_agent="test"), tempdir, tempdir, tempdir)
954+
url, path = resource.download(retriever=retriever)
955+
remove(path)
956+
assert (
957+
url
958+
== "https://raw.githubusercontent.com/OCHA-DAP/hdx-python-api/main/tests/fixtures/test_data.csv"
959+
)
960+
951961
resource["url"] = ""
952962
with pytest.raises(HDXError):
953963
resource.download()
964+
resource2 = Resource.read_from_hdx("74b74ae1-df0c-4716-829f-4f939a046814")
954965
with pytest.raises(DownloadError):
955966
resource2.download()
956967

0 commit comments

Comments
 (0)