Skip to content

Commit

Permalink
refactor: fetch_json usage
Browse files Browse the repository at this point in the history
  • Loading branch information
sbrunato committed May 24, 2024
1 parent 5398500 commit 425e396
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 51 deletions.
17 changes: 13 additions & 4 deletions eodag/rest/stac.py
Original file line number Diff line number Diff line change
Expand Up @@ -42,12 +42,9 @@
)
from eodag.utils import (
DEFAULT_MISSION_START_DATE,
HTTP_REQ_TIMEOUT,
USER_AGENT,
deepcopy,
dict_items_recursive_apply,
format_dict_items,
get_ext_stac_collection,
guess_file_type,
jsonpath_parse_dict_items,
string_to_jsonpath,
Expand All @@ -57,8 +54,11 @@
from eodag.utils.exceptions import (
NoMatchingProductType,
NotAvailableError,
RequestError,
TimeOutError,
ValidationError,
)
from eodag.utils.requests import fetch_json

if TYPE_CHECKING:
from eodag.api.core import EODataAccessGateway
Expand Down Expand Up @@ -630,7 +630,16 @@ def fetch_external_stac_collections(cls, eodag_api: EODataAccessGateway) -> None
if not ext_stac_collection_path:
continue
logger.info(f"Fetching external STAC collection for {product_type['ID']}")
ext_stac_collection = get_ext_stac_collection(ext_stac_collection_path)

try:
ext_stac_collection = fetch_json(ext_stac_collection_path)
except (RequestError, TimeOutError) as e:
logger.debug(e)
logger.warning(
f"Could not read remote external STAC collection from {ext_stac_collection_path}",
)
ext_stac_collection = {}

cls.ext_stac_collections[product_type["ID"]] = ext_stac_collection

def __init__(
Expand Down
40 changes: 0 additions & 40 deletions eodag/utils/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -78,8 +78,6 @@
)
from urllib.request import url2pathname

import requests

if sys.version_info >= (3, 9):
from typing import Annotated, get_args, get_origin # noqa
else:
Expand Down Expand Up @@ -1474,41 +1472,3 @@ def get_ssl_context(ssl_verify: bool) -> ssl.SSLContext:
ctx.check_hostname = True
ctx.verify_mode = ssl.CERT_REQUIRED
return ctx


def get_ext_stac_collection(stac_uri: str) -> Dict[str, Any]:
"""Read external STAC collection
:param stac_uri: URI to local or remote collection
:type stac_uri: str
:returns: The external STAC collection
:rtype: dict
"""
logger.info("Fetching external STAC collection from %s", stac_uri)
if stac_uri.lower().startswith("http"):
# read from remote
try:
response = requests.get(
stac_uri, headers=USER_AGENT, timeout=HTTP_REQ_TIMEOUT
)
response.raise_for_status()
return response.json()
except requests.RequestException as e:
logger.debug(e)
logger.warning(
"Could not read remote external STAC collection from %s", stac_uri
)
return {}
elif stac_uri.lower().startswith("file"):
stac_uri = uri_to_path(stac_uri)

# read from local
try:
with open(stac_uri, "rb") as f:
return orjson.loads(f.read())
except (orjson.JSONDecodeError, FileNotFoundError) as e:
logger.debug(e)
logger.warning(
"Could not read local external STAC collection from %s", stac_uri
)
return {}
12 changes: 5 additions & 7 deletions tests/units/test_stac_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -265,18 +265,16 @@ def test_fetch_external_stac_collections(self):
product_type_conf["stacCollection"] = ext_stac_collection_path

with mock.patch(
"eodag.rest.stac.get_ext_stac_collection", autospec=True
) as mock_stac_get_ext_stac_collection:
mock_stac_get_ext_stac_collection.return_value = json.loads(external_json)

"eodag.rest.stac.fetch_json",
autospec=True,
return_value=json.loads(external_json),
) as mock_fetch_json:
# Check if the returned STAC collection contains updated data
StacCollection.fetch_external_stac_collections(self.rest_core.eodag_api)
stac_coll = self.rest_core.get_stac_collection_by_id(
url="", root="", collection_id="S2_MSI_L1C"
)
mock_stac_get_ext_stac_collection.assert_called_with(
ext_stac_collection_path
)
mock_fetch_json.assert_called_with(ext_stac_collection_path)
# New field
self.assertIn("new_field", stac_coll)
# Merge keywords
Expand Down

0 comments on commit 425e396

Please sign in to comment.