Skip to content

Commit

Permalink
feat: configurable download timeout (#1124)
Browse files Browse the repository at this point in the history
  • Loading branch information
sbrunato authored and alambare committed May 2, 2024
1 parent bc1953a commit a29e7a7
Show file tree
Hide file tree
Showing 4 changed files with 37 additions and 21 deletions.
5 changes: 3 additions & 2 deletions eodag/plugins/download/aws.py
Original file line number Diff line number Diff line change
Expand Up @@ -440,6 +440,7 @@ def _configure_safe_build(self, build_safe: bool, product: EOProduct):
product.product_type, {}
)
ssl_verify = getattr(self.config, "ssl_verify", True)
timeout = getattr(self.config, "timeout", HTTP_REQ_TIMEOUT)

if build_safe and "fetch_metadata" in product_conf.keys():
fetch_format = product_conf["fetch_metadata"]["fetch_format"]
Expand All @@ -452,11 +453,11 @@ def _configure_safe_build(self, build_safe: bool, product: EOProduct):
resp = requests.get(
fetch_url,
headers=USER_AGENT,
timeout=HTTP_REQ_TIMEOUT,
timeout=timeout,
verify=ssl_verify,
)
except requests.exceptions.Timeout as exc:
raise TimeOutError(exc, timeout=HTTP_REQ_TIMEOUT) from exc
raise TimeOutError(exc, timeout=timeout) from exc
update_metadata = mtd_cfg_as_conversion_and_querypath(update_metadata)
if fetch_format == "json":
json_resp = resp.json()
Expand Down
13 changes: 9 additions & 4 deletions eodag/plugins/download/http.py
Original file line number Diff line number Diff line change
Expand Up @@ -165,6 +165,7 @@ def orderDownload(

order_method = getattr(self.config, "order_method", "GET").upper()
ssl_verify = getattr(self.config, "ssl_verify", True)
timeout = getattr(self.config, "timeout", HTTP_REQ_TIMEOUT)
OrderKwargs = TypedDict(
"OrderKwargs", {"json": Dict[str, Union[Any, List[str]]]}, total=False
)
Expand All @@ -188,7 +189,7 @@ def orderDownload(
method=order_method,
url=order_url,
auth=auth,
timeout=HTTP_REQ_TIMEOUT,
timeout=timeout,
headers=headers,
verify=ssl_verify,
**order_kwargs,
Expand All @@ -212,7 +213,7 @@ def orderDownload(
self._check_auth_exception(e)
return self.order_response_process(response, product)
except requests.exceptions.Timeout as exc:
raise TimeOutError(exc, timeout=HTTP_REQ_TIMEOUT) from exc
raise TimeOutError(exc, timeout=timeout) from exc

def order_response_process(
self, response: Response, product: EOProduct
Expand Down Expand Up @@ -281,6 +282,8 @@ def orderDownloadStatus(
status_config = getattr(self.config, "order_status", {})
success_code: Optional[int] = status_config.get("success", {}).get("http_code")

timeout = getattr(self.config, "timeout", HTTP_REQ_TIMEOUT)

def _request(
url: str,
method: str = "GET",
Expand Down Expand Up @@ -341,6 +344,7 @@ def _request(
status_request_method,
status_request.get("headers"),
json_data,
timeout,
)
json_response = response.json()
if not isinstance(json_response, dict):
Expand Down Expand Up @@ -405,7 +409,7 @@ def _request(
if config_on_success.get("need_search"):
logger.debug(f"Search for new location: {product.properties['searchLink']}")
try:
response = _request(product.properties["searchLink"])
response = _request(product.properties["searchLink"], timeout=timeout)
except RequestException as e:
logger.warning(
"%s order status could not be checked, request returned %s",
Expand Down Expand Up @@ -1215,6 +1219,7 @@ def _get_asset_sizes(
) -> int:
total_size = 0

timeout = getattr(self.config, "timeout", HTTP_REQ_TIMEOUT)
ssl_verify = getattr(self.config, "ssl_verify", True)
# loop for assets size & filename
for asset in assets_values:
Expand All @@ -1224,7 +1229,7 @@ def _get_asset_sizes(
asset["href"],
auth=auth,
headers=USER_AGENT,
timeout=HTTP_REQ_TIMEOUT,
timeout=timeout,
).headers

if not getattr(asset, "size", 0):
Expand Down
3 changes: 2 additions & 1 deletion eodag/plugins/download/s3rest.py
Original file line number Diff line number Diff line change
Expand Up @@ -177,12 +177,13 @@ def download_request(
logger.debug("Retrieving product content from %s", nodes_list_url)

ssl_verify = getattr(self.config, "ssl_verify", True)
timeout = getattr(self.config, "timeout", HTTP_REQ_TIMEOUT)

bucket_contents = requests.get(
nodes_list_url,
auth=auth,
headers=USER_AGENT,
timeout=HTTP_REQ_TIMEOUT,
timeout=timeout,
verify=ssl_verify,
)
try:
Expand Down
37 changes: 23 additions & 14 deletions tests/units/test_download_plugins.py
Original file line number Diff line number Diff line change
Expand Up @@ -1105,20 +1105,29 @@ def test_plugins_download_http_order_get(self, mock_request):
self.product.properties["orderLink"] = "http://somewhere/order"
self.product.properties["storageStatus"] = OFFLINE_STATUS

auth_plugin = self.get_auth_plugin(self.product.provider)
auth_plugin.config.credentials = {"username": "foo", "password": "bar"}
auth = auth_plugin.authenticate()

plugin.orderDownload(self.product, auth=auth)

mock_request.assert_called_once_with(
method="GET",
url=self.product.properties["orderLink"],
auth=auth,
headers=USER_AGENT,
timeout=HTTP_REQ_TIMEOUT,
verify=True,
)
# customized timeout
timeout_backup = getattr(plugin.config, "timeout", None)
plugin.config.timeout = 10
try:
auth_plugin = self.get_auth_plugin(self.product.provider)
auth_plugin.config.credentials = {"username": "foo", "password": "bar"}
auth = auth_plugin.authenticate()

plugin.orderDownload(self.product, auth=auth)

mock_request.assert_called_once_with(
method="GET",
url=self.product.properties["orderLink"],
auth=auth,
headers=USER_AGENT,
timeout=10,
verify=True,
)
finally:
if timeout_backup:
plugin.config.timeout = timeout_backup
else:
del plugin.config.timeout

@mock.patch("eodag.plugins.download.http.requests.request", autospec=True)
def test_plugins_download_http_order_post(self, mock_request):
Expand Down

0 comments on commit a29e7a7

Please sign in to comment.