-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
07ae5b2
commit 37d3632
Showing
12 changed files
with
119 additions
and
15 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -9,7 +9,6 @@ jobs: | |
strategy: | ||
matrix: | ||
python-version: | ||
- "3.7" | ||
- "3.8" | ||
- "3.9" | ||
- "3.10" | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,4 @@ | ||
from . import _version | ||
from .blocks import EoBlock # noqa | ||
from .earthdata import EarthdataCredentials # noqa | ||
|
||
__version__ = _version.get_versions()["version"] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
"""Module handling NASA Earthdata credentials""" | ||
|
||
import os | ||
|
||
import earthaccess | ||
from prefect.blocks.core import Block | ||
from pydantic import Field, SecretStr | ||
|
||
|
||
class EarthdataCredentials(Block): | ||
""" | ||
Block used to manage authentication with NASA Earthdata. | ||
NASA Earthdata authentication is handled via the `earthaccess` module. | ||
Refer to the [earthaccess docs](https://nsidc.github.io/earthaccess/) | ||
for more info about the possible credential configurations. | ||
Example: | ||
Load stored Earthdata credentials: | ||
```python | ||
from prefect_eo import EarthdataCredentials | ||
ed_credentials_block = EarthdataCredentials.load("BLOCK_NAME") | ||
``` | ||
""" # noqa E501 | ||
|
||
_logo_url = "https://yt3.googleusercontent.com/ytc/AGIKgqPjIUeAw3_hrkHWZgixdwD5jc-hTWweoCA6bJMhUg=s176-c-k-c0x00ffffff-no-rj" # noqa | ||
_block_type_name = "NASA Earthdata Credentials" | ||
_documentation_url = "https://nsidc.github.io/earthaccess/" # noqa | ||
|
||
earthdata_username: str = Field( | ||
default=..., | ||
description="The Earthdata username of a specific account.", | ||
title="Earthdata username", | ||
) | ||
earthdata_password: SecretStr = Field( | ||
default=..., | ||
description="The Earthdata password of a specific account.", | ||
title="Earthdata password", | ||
) | ||
|
||
def login(self) -> earthaccess.Auth: | ||
""" | ||
Returns an authenticated session with NASA Earthdata | ||
Example: | ||
```python | ||
earthdata_credentials_block = EarthdataCredentials( | ||
earthdata_username = "username", | ||
earthdata_password = "password" | ||
) | ||
earthdata_auth = earthdata_credentials_block.login() | ||
``` | ||
""" | ||
|
||
"""""" | ||
|
||
os.environ["EARTHDATA_USERNAME"] = self.earthdata_username | ||
os.environ["EARTHDATA_PASSWORD"] = self.earthdata_password.get_secret_value() | ||
return earthaccess.login(strategy="environment") |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1,2 @@ | ||
prefect>=2.0.0 | ||
earthaccess |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
import pytest | ||
import requests_mock | ||
from earthaccess import Auth | ||
|
||
from prefect_eo.earthdata import EarthdataCredentials | ||
|
||
|
||
@pytest.fixture | ||
def mock_earthdata_responses(): | ||
with requests_mock.Mocker() as m: | ||
json_response = [ | ||
{"access_token": "EDL-token-1", "expiration_date": "12/15/2023"}, | ||
{"access_token": "EDL-token-2", "expiration_date": "12/16/2023"}, | ||
] | ||
m.get( | ||
"https://urs.earthdata.nasa.gov/api/users/tokens", | ||
json=json_response, | ||
status_code=200, | ||
) | ||
m.get( | ||
"https://urs.earthdata.nasa.gov/profile", | ||
json={"uid": "test_username"}, | ||
status_code=200, | ||
) | ||
m.get( | ||
"https://urs.earthdata.nasa.gov/api/users/user?client_id=ntD0YGC_SM3Bjs-Tnxd7bg", # noqa E501 | ||
json={"uid": "test_username"}, | ||
status_code=200, | ||
) | ||
yield m | ||
|
||
|
||
def test_earthdata_credentials_login(mock_earthdata_responses): # noqa | ||
""" | ||
Asserts that instantiated EarthdataCredentials block creates an | ||
authenticated session. | ||
""" | ||
|
||
# Set up mock user input | ||
mock_username = "user" | ||
mock_password = "password" | ||
|
||
# Instantiate EarthdataCredentials block | ||
earthdata_credentials_block = EarthdataCredentials( | ||
earthdata_username=mock_username, earthdata_password=mock_password | ||
) | ||
earthdata_auth = earthdata_credentials_block.login() | ||
|
||
assert isinstance(earthdata_auth, Auth) | ||
assert earthdata_auth.authenticated |