diff --git a/earthaccess/__init__.py b/earthaccess/__init__.py index ef4fdfb8..ef3715d0 100644 --- a/earthaccess/__init__.py +++ b/earthaccess/__init__.py @@ -2,6 +2,7 @@ from typing import Any from .api import ( + auth_environ, collection_query, download, get_fsspec_https_session, @@ -34,6 +35,7 @@ "DataCollections", "Auth", "Store", + "auth_environ", ] __auth__ = Auth() diff --git a/earthaccess/api.py b/earthaccess/api.py index a10ee0e2..4ef8598d 100644 --- a/earthaccess/api.py +++ b/earthaccess/api.py @@ -333,3 +333,12 @@ 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 71745ff5..c449c358 100644 --- a/tests/integration/test_api.py +++ b/tests/integration/test_api.py @@ -80,3 +80,17 @@ def test_earthaccess_api_can_download_granules(tmp_path, selection): files = earthaccess.download(result, str(tmp_path)) assertions.assertIsInstance(files, list) assert all(os.path.exists(f) for f in files) + + +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()