From 3b48f406e8b5d800204fb1c666735bcd636409fe Mon Sep 17 00:00:00 2001 From: James Bourbeau Date: Wed, 15 Nov 2023 12:10:35 -0600 Subject: [PATCH 1/4] Handle lowercase daac and provider inputs --- earthaccess/api.py | 16 ++++++++++++---- 1 file changed, 12 insertions(+), 4 deletions(-) diff --git a/earthaccess/api.py b/earthaccess/api.py index 4ef8598d..a54cbec4 100644 --- a/earthaccess/api.py +++ b/earthaccess/api.py @@ -12,6 +12,12 @@ from .utils import _validation as validate +def _normalize_location(location: Union[str, None]) -> Union[str, None]: + if location is not None: + location = location.upper() + return location + + def search_datasets( count: int = -1, **kwargs: Any ) -> List[earthaccess.results.DataCollection]: @@ -170,6 +176,7 @@ def download( Returns: List of downloaded files """ + provider = _normalize_location(provider) if isinstance(granules, DataGranule): granules = [granules] try: @@ -194,6 +201,7 @@ def open( Returns: a list of s3fs "file pointers" to s3 files. """ + provider = _normalize_location(provider) results = earthaccess.__store__.open(granules=granules, provider=provider) return results @@ -215,10 +223,8 @@ def get_s3_credentials( Returns: a dictionary with S3 credentials for the DAAC or provider """ - if daac is not None: - daac = daac.upper() - if provider is not None: - provider = provider.upper() + daac = _normalize_location(daac) + provider = _normalize_location(provider) if results is not None: endpoint = results[0].get_s3_credentials_endpoint() return earthaccess.__auth__.get_s3_credentials(endpoint=endpoint) @@ -315,6 +321,8 @@ def get_s3fs_session( Returns: class s3fs.S3FileSystem: an authenticated s3fs session valid for 1 hour """ + daac = _normalize_location(daac) + provider = _normalize_location(provider) if results is not None: endpoint = results[0].get_s3_credentials_endpoint() if endpoint is not None: From f186096c739341a580187f9555b3ebf96fc3c41b Mon Sep 17 00:00:00 2001 From: James Bourbeau Date: Wed, 15 Nov 2023 12:40:27 -0600 Subject: [PATCH 2/4] Tests --- tests/integration/test_auth.py | 22 ++++++++++++++++++++++ 1 file changed, 22 insertions(+) diff --git a/tests/integration/test_auth.py b/tests/integration/test_auth.py index a61f3513..d1bfae1e 100644 --- a/tests/integration/test_auth.py +++ b/tests/integration/test_auth.py @@ -6,6 +6,7 @@ import earthaccess import pytest +import s3fs logger = logging.getLogger(__name__) assertions = unittest.TestCase("__init__") @@ -94,3 +95,24 @@ def test_auth_can_fetch_s3_credentials(): print( f"An error occured while trying to fetch S3 credentials for {daac['short-name']}: {e}" ) + + +@pytest.mark.parametrize("location", ({"daac": "podaac"}, {"provider": "pocloud"})) +def test_get_s3_credentials_lowercase_location(location): + activate_environment() + earthaccess.login(strategy="environment") + creds = earthaccess.get_s3_credentials(**location) + assert creds + assert all( + creds[key] + for key in ["accessKeyId", "secretAccessKey", "sessionToken", "expiration"] + ) + + +@pytest.mark.parametrize("location", ({"daac": "podaac"}, {"provider": "pocloud"})) +def test_get_s3fs_session_lowercase_location(location): + activate_environment() + earthaccess.login(strategy="environment") + fs = earthaccess.get_s3fs_session(**location) + assert isinstance(fs, s3fs.S3FileSystem) + assert all(fs.storage_options[key] for key in ["key", "secret", "token"]) From 46814f76cd94db005a86f3c3778479c43f682209 Mon Sep 17 00:00:00 2001 From: James Bourbeau Date: Thu, 16 Nov 2023 09:58:00 -0600 Subject: [PATCH 3/4] Docstring --- earthaccess/api.py | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/earthaccess/api.py b/earthaccess/api.py index a54cbec4..8e137305 100644 --- a/earthaccess/api.py +++ b/earthaccess/api.py @@ -13,6 +13,13 @@ def _normalize_location(location: Union[str, None]) -> Union[str, None]: + """Handle user-provided `daac` and `provider` values + + These values must have a capital letter as the first character + followed by capital letters, numbers, or an underscore. Here we + uppercase all strings to handle the case when users provide + lowercase values (e.g. "pocloud" instead of "POCLOUD"). + """ if location is not None: location = location.upper() return location From 880f45e10a82e8c1b361c49c6cc90e274e79b59b Mon Sep 17 00:00:00 2001 From: James Bourbeau Date: Wed, 29 Nov 2023 09:53:57 -0600 Subject: [PATCH 4/4] Link --- earthaccess/api.py | 2 ++ 1 file changed, 2 insertions(+) diff --git a/earthaccess/api.py b/earthaccess/api.py index 8e137305..843aba65 100644 --- a/earthaccess/api.py +++ b/earthaccess/api.py @@ -19,6 +19,8 @@ def _normalize_location(location: Union[str, None]) -> Union[str, None]: followed by capital letters, numbers, or an underscore. Here we uppercase all strings to handle the case when users provide lowercase values (e.g. "pocloud" instead of "POCLOUD"). + + https://wiki.earthdata.nasa.gov/display/ED/CMR+Data+Partner+User+Guide?src=contextnavpagetreemode """ if location is not None: location = location.upper()