diff --git a/earthaccess/__init__.py b/earthaccess/__init__.py index ef4fdfb8..05e2192c 100644 --- a/earthaccess/__init__.py +++ b/earthaccess/__init__.py @@ -13,6 +13,7 @@ open, search_data, search_datasets, + auth_environ, ) from .auth import Auth from .search import DataCollections, DataGranules @@ -34,6 +35,7 @@ "DataCollections", "Auth", "Store", + "auth_environ", ] __auth__ = Auth() diff --git a/earthaccess/api.py b/earthaccess/api.py index f0264454..9a5594b3 100644 --- a/earthaccess/api.py +++ b/earthaccess/api.py @@ -330,3 +330,9 @@ def get_edl_token() -> str: """ token = earthaccess.__auth__.token return token + +def auth_environ() -> Dict[str, str]: + auth = earthaccess.__auth__ + if not auth.authenticated: + raise RuntimeError("`auth_environ()` requires you to first authenticate with `earthaccess.login()`") + return {"EARTHDATA_USERNAME": auth.username, "EARTHDATA_PASSWORD": auth.password} diff --git a/tests/integration/test_api.py b/tests/integration/test_api.py index d4848d60..0eb6a59b 100644 --- a/tests/integration/test_api.py +++ b/tests/integration/test_api.py @@ -82,3 +82,12 @@ def test_earthaccess_api_can_download_granules(): files = earthaccess.download(results, local_path=local_path) assertions.assertIsInstance(files, list) shutil.rmtree(local_path) + +def test_auth_environ(): + environ = earthaccess.auth_environ() + assert environ == {"EARTHDATA_USERNAME": os.environ["EARTHDATA_USERNAME"], "EARTHDATA_PASSWORD": os.environ["EARTHDATA_PASSWORD"]} + +def test_auth_environ_raises(monkeypatch): + monkeypatch.setattr(earthaccess.__auth__, "authenticated", False) + with pytest.raises(RuntimeError, match="authenticate"): + earthaccess.auth_environ() \ No newline at end of file