Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

NASA Earthdata credentials block #5

Merged
merged 2 commits into from
Jun 29, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
1 change: 0 additions & 1 deletion docs/gen_examples_catalog.py
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,6 @@ def get_code_examples(obj: Union[ModuleType, Callable]) -> Set[str]:

code_examples_grouping = defaultdict(set)
for _, module_name, ispkg in iter_modules(prefect_eo.__path__):

module_nesting = f"{COLLECTION_SLUG}.{module_name}"
module_obj = load_module(module_nesting)

Expand Down
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"]
35 changes: 0 additions & 35 deletions prefect_eo/blocks.py

This file was deleted.

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")
26 changes: 0 additions & 26 deletions prefect_eo/flows.py

This file was deleted.

24 changes: 0 additions & 24 deletions prefect_eo/tasks.py

This file was deleted.

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
Empty file added tests/__init__.py
Empty file.
4 changes: 4 additions & 0 deletions tests/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,10 @@
from prefect.testing.utilities import prefect_test_harness


def pytest_configure(config):
config.addinivalue_line("markers", "flaky: mark test as flaky")


@pytest.fixture(scope="session", autouse=True)
def prefect_db():
"""
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
6 changes: 0 additions & 6 deletions tests/test_flows.py

This file was deleted.

24 changes: 0 additions & 24 deletions tests/test_tasks.py

This file was deleted.