Skip to content

Commit

Permalink
Add utility for getting authentication environment variables
Browse files Browse the repository at this point in the history
  • Loading branch information
jrbourbeau committed Oct 13, 2023
1 parent 25a38ea commit fbaad1e
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 0 deletions.
2 changes: 2 additions & 0 deletions earthaccess/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
open,
search_data,
search_datasets,
auth_environ,
)
from .auth import Auth
from .search import DataCollections, DataGranules
Expand All @@ -34,6 +35,7 @@
"DataCollections",
"Auth",
"Store",
"auth_environ",
]

__auth__ = Auth()
Expand Down
6 changes: 6 additions & 0 deletions earthaccess/api.py
Original file line number Diff line number Diff line change
Expand Up @@ -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}
9 changes: 9 additions & 0 deletions tests/integration/test_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -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()

0 comments on commit fbaad1e

Please sign in to comment.