Skip to content

Commit f86d0d7

Browse files
committed
Issue #560 Add on_response_headers to DataCube.download() et al.
1 parent 5e17e18 commit f86d0d7

File tree

7 files changed

+57
-5
lines changed

7 files changed

+57
-5
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
1111

1212
- `sar_backscatter`: try to retrieve coefficient options from backend ([#693](https://github.com/Open-EO/openeo-python-client/issues/693))
1313
- Improve error message when OIDC provider is unavailable ([#751](https://github.com/Open-EO/openeo-python-client/issues/751))
14+
- Added `on_response_headers` argument to `DataCube.download()` and related to handle (e.g. `print`) the response headers ([#560](https://github.com/Open-EO/openeo-python-client/issues/560))
1415

1516
### Changed
1617

openeo/rest/_testing.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -179,6 +179,7 @@ def _handle_post_result(self, request, context):
179179
elif isinstance(result, str):
180180
result = result.encode("utf-8")
181181
assert isinstance(result, bytes)
182+
context.headers["OpenEO-Identifier"] = f"r-{len(self.sync_requests):03d}"
182183
return result
183184

184185
def _handle_post_jobs(self, request, context):

openeo/rest/connection.py

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
Iterable,
2020
Iterator,
2121
List,
22+
Mapping,
2223
Optional,
2324
Sequence,
2425
Set,
@@ -1568,6 +1569,7 @@ def download(
15681569
chunk_size: int = DEFAULT_DOWNLOAD_CHUNK_SIZE,
15691570
additional: Optional[dict] = None,
15701571
job_options: Optional[dict] = None,
1572+
on_response_headers: Optional[Callable[[Mapping], None]] = None,
15711573
) -> Union[None, bytes]:
15721574
"""
15731575
Send the underlying process graph to the backend
@@ -1586,13 +1588,17 @@ def download(
15861588
:param additional: (optional) additional (top-level) properties to set in the request body
15871589
:param job_options: (optional) dictionary of job options to pass to the backend
15881590
(under top-level property "job_options")
1591+
:param on_response_headers: (optional) callback to handle/show the response headers
15891592
15901593
:return: if ``outputfile`` was not specified:
15911594
a :py:class:`bytes` object containing the raw data.
15921595
Otherwise, ``None`` is returned.
15931596
1594-
.. versionadded:: 0.36.0
1597+
.. versionchanged:: 0.36.0
15951598
Added arguments ``additional`` and ``job_options``.
1599+
1600+
.. versionchanged:: 0.40
1601+
Added argument ``on_response_headers``.
15961602
"""
15971603
pg_with_metadata = self._build_request_with_process_graph(
15981604
process_graph=graph, additional=additional, job_options=job_options
@@ -1605,6 +1611,8 @@ def download(
16051611
stream=True,
16061612
timeout=timeout or DEFAULT_TIMEOUT_SYNCHRONOUS_EXECUTE,
16071613
)
1614+
if on_response_headers:
1615+
on_response_headers(response.headers)
16081616

16091617
if outputfile is not None:
16101618
target = Path(outputfile)

openeo/rest/datacube.py

Lines changed: 23 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,18 @@
1717
import urllib.parse
1818
import warnings
1919
from builtins import staticmethod
20-
from typing import Any, Callable, Dict, Iterable, List, Optional, Sequence, Tuple, Union
20+
from typing import (
21+
Any,
22+
Callable,
23+
Dict,
24+
Iterable,
25+
List,
26+
Mapping,
27+
Optional,
28+
Sequence,
29+
Tuple,
30+
Union,
31+
)
2132

2233
import numpy as np
2334
import requests
@@ -2386,6 +2397,7 @@ def download(
23862397
auto_add_save_result: bool = True,
23872398
additional: Optional[dict] = None,
23882399
job_options: Optional[dict] = None,
2400+
on_response_headers: Optional[Callable[[Mapping], None]] = None,
23892401
) -> Union[None, bytes]:
23902402
"""
23912403
Send the underlying process graph to the backend
@@ -2403,6 +2415,7 @@ def download(
24032415
:param additional: (optional) additional (top-level) properties to set in the request body
24042416
:param job_options: (optional) dictionary of job options to pass to the backend
24052417
(under top-level property "job_options")
2418+
:param on_response_headers: (optional) callback to handle/show the response headers
24062419
24072420
:return: if ``outputfile`` was not specified:
24082421
a :py:class:`bytes` object containing the raw data.
@@ -2413,14 +2426,22 @@ def download(
24132426
24142427
.. versionchanged:: 0.36.0
24152428
Added arguments ``additional`` and ``job_options``.
2429+
2430+
.. versionchanged:: 0.40
2431+
Added argument ``on_response_headers``.
24162432
"""
24172433
# TODO #278 centralize download/create_job/execute_job logic in DataCube, VectorCube, MlModel, ...
24182434
if auto_add_save_result:
24192435
res = self._auto_save_result(format=format, outputfile=outputfile, options=options)
24202436
else:
24212437
res = self
24222438
return self._connection.download(
2423-
res.flat_graph(), outputfile=outputfile, validate=validate, additional=additional, job_options=job_options
2439+
res.flat_graph(),
2440+
outputfile=outputfile,
2441+
validate=validate,
2442+
additional=additional,
2443+
job_options=job_options,
2444+
on_response_headers=on_response_headers,
24242445
)
24252446

24262447
def validate(self) -> List[dict]:

openeo/rest/stac_resource.py

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
import typing
44
from pathlib import Path
5-
from typing import Optional, Union
5+
from typing import Callable, Mapping, Optional, Union
66

77
from openeo.internal.documentation import openeo_process
88
from openeo.internal.graph_building import PGNode
@@ -87,6 +87,7 @@ def download(
8787
validate: Optional[bool] = None,
8888
additional: Optional[dict] = None,
8989
job_options: Optional[dict] = None,
90+
on_response_headers: Optional[Callable[[Mapping], None]] = None,
9091
):
9192
"""
9293
Send the underlying process graph to the backend
@@ -101,17 +102,22 @@ def download(
101102
:param additional: (optional) additional (top-level) properties to set in the request body
102103
:param job_options: (optional) dictionary of job options to pass to the backend
103104
(under top-level property "job_options")
105+
:param on_response_headers: (optional) callback to handle/show the response headers
104106
105107
:return: if ``outputfile`` was not specified:
106108
a :py:class:`bytes` object containing the raw data.
107109
Otherwise, ``None`` is returned.
110+
111+
.. versionchanged:: 0.40
112+
Added argument ``on_response_headers``.
108113
"""
109114
return self._connection.download(
110115
graph=self.flat_graph(),
111116
outputfile=outputfile,
112117
validate=validate,
113118
additional=additional,
114119
job_options=job_options,
120+
on_response_headers=on_response_headers,
115121
)
116122

117123
def create_job(

openeo/rest/vectorcube.py

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
import logging
55
import pathlib
66
import typing
7-
from typing import Callable, List, Optional, Tuple, Union
7+
from typing import Callable, List, Mapping, Optional, Tuple, Union
88

99
import shapely.geometry.base
1010

@@ -250,6 +250,7 @@ def download(
250250
auto_add_save_result: bool = True,
251251
additional: Optional[dict] = None,
252252
job_options: Optional[dict] = None,
253+
on_response_headers: Optional[Callable[[Mapping], None]] = None,
253254
) -> Union[None, bytes]:
254255
"""
255256
Send the underlying process graph to the backend
@@ -267,6 +268,7 @@ def download(
267268
:param additional: (optional) additional (top-level) properties to set in the request body
268269
:param job_options: (optional) dictionary of job options to pass to the backend
269270
(under top-level property "job_options")
271+
:param on_response_headers: (optional) callback to handle/show the response headers
270272
271273
:return: if ``outputfile`` was not specified:
272274
a :py:class:`bytes` object containing the raw data.
@@ -280,6 +282,9 @@ def download(
280282
281283
.. versionchanged:: 0.39.0
282284
Added arguments ``additional`` and ``job_options``.
285+
286+
.. versionchanged:: 0.40
287+
Added argument ``on_response_headers``.
283288
"""
284289
# TODO #278 centralize download/create_job/execute_job logic in DataCube, VectorCube, MlModel, ...
285290
if auto_add_save_result:

tests/rest/test_connection.py

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3612,6 +3612,16 @@ def test_download_with_additional_and_job_options(dummy_backend, tmp_path):
36123612
}
36133613

36143614

3615+
def test_download_on_response_headers(dummy_backend, tmp_path):
3616+
results = []
3617+
dummy_backend.connection.download(
3618+
{"foo1": {"process_id": "foo"}},
3619+
tmp_path / "result.data",
3620+
on_response_headers=results.append,
3621+
)
3622+
assert results == [{"OpenEO-Identifier": "r-001"}]
3623+
3624+
36153625
@pytest.mark.parametrize(
36163626
"pg",
36173627
[

0 commit comments

Comments
 (0)