Skip to content

Commit

Permalink
Merge pull request #316 from jrbourbeau/auth-env-util
Browse files Browse the repository at this point in the history
Add utility for getting authentication environment variables
  • Loading branch information
betolink authored Oct 15, 2023
2 parents 250848d + e5ed74c commit 8483173
Show file tree
Hide file tree
Showing 3 changed files with 25 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 @@ -2,6 +2,7 @@
from typing import Any

from .api import (
auth_environ,
collection_query,
download,
get_fsspec_https_session,
Expand Down Expand Up @@ -34,6 +35,7 @@
"DataCollections",
"Auth",
"Store",
"auth_environ",
]

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

0 comments on commit 8483173

Please sign in to comment.