Skip to content

Commit

Permalink
Add EarthdataCredentials block
Browse files Browse the repository at this point in the history
  • Loading branch information
giorgiobasile committed Jun 29, 2023
1 parent 07ae5b2 commit 37d3632
Show file tree
Hide file tree
Showing 12 changed files with 119 additions and 15 deletions.
1 change: 0 additions & 1 deletion .github/workflows/tests.yml
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ jobs:
strategy:
matrix:
python-version:
- "3.7"
- "3.8"
- "3.9"
- "3.10"
Expand Down
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0

### Added

- Added NASA Earthdata credentials block - [#4](https://github.com/giorgiobasile/prefect-eo/issues/4)

### Changed

### Deprecated
Expand Down
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ Install `prefect-eo` with `pip`:
pip install prefect-eo
```

Requires an installation of Python 3.7+.
Requires an installation of Python 3.8+.

We recommend using a Python virtual environment manager such as pipenv, conda or virtualenv.

Expand Down
2 changes: 1 addition & 1 deletion docs/flows.md → docs/earthdata.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,4 @@ description:
notes: This documentation page is generated from source file docstrings.
---

::: prefect_eo.flows
::: prefect_eo.earthdata
6 changes: 0 additions & 6 deletions docs/tasks.md

This file was deleted.

4 changes: 2 additions & 2 deletions mkdocs.yml
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ nav:
- Blocks Catalog: blocks_catalog.md
- Examples Catalog: examples_catalog.md
- API Reference:
- Tasks: tasks.md
- Flows: flows.md
- Earthdata: earthdata.md



2 changes: 1 addition & 1 deletion prefect_eo/__init__.py
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"]
59 changes: 59 additions & 0 deletions prefect_eo/earthdata.py
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")
2 changes: 1 addition & 1 deletion requirements-dev.txt
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,8 @@ mkdocstrings[python]
isort
pre-commit
pytest-asyncio
mock; python_version < '3.8'
mkdocs-gen-files
interrogate
coverage
pillow
requests_mock
1 change: 1 addition & 0 deletions requirements.txt
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
prefect>=2.0.0
earthaccess
3 changes: 1 addition & 2 deletions setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@
version=versioneer.get_version(),
cmdclass=versioneer.get_cmdclass(),
packages=find_packages(exclude=("tests", "docs")),
python_requires=">=3.7",
python_requires=">=3.8",
install_requires=install_requires,
extras_require={"dev": dev_requires},
entry_points={
Expand All @@ -38,7 +38,6 @@
"Intended Audience :: System Administrators",
"License :: OSI Approved :: Apache Software License",
"Programming Language :: Python :: 3 :: Only",
"Programming Language :: Python :: 3.7",
"Programming Language :: Python :: 3.8",
"Programming Language :: Python :: 3.9",
"Programming Language :: Python :: 3.10",
Expand Down
50 changes: 50 additions & 0 deletions tests/test_earthdata.py
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

0 comments on commit 37d3632

Please sign in to comment.