Skip to content
Open
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
4 changes: 3 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ share/python-wheels/
.installed.cfg
*.egg
MANIFEST
poetry.lock

# PyInstaller
# Usually these files are written by a python script from a template
Expand Down Expand Up @@ -106,6 +107,7 @@ celerybeat.pid

# Environments
.env
.env.test
.venv
env/
venv/
Expand Down Expand Up @@ -138,4 +140,4 @@ dmypy.json
cython_debug/


/.vscode
/.vscode
18 changes: 18 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -223,3 +223,21 @@ ee.download('LT51960471995178MPS00', output_dir='./data')

ee.logout()
```


## Local development

To create a local development version of the library:

([Poetry](https://python-poetry.org/) recommended: `pip install poetry`)

```bash
git clone [email protected]:barneyjackson/landsatxplore.git .
poetry install
```

Create a copy of `example.env` named `.env` and add your credentials.

### Tests

Run `env $(cat .env) poetry run pytest tests/`
2 changes: 2 additions & 0 deletions example.env
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
LANDSATXPLORE_USERNAME=
LANDSATXPLORE_PASSWORD=
24 changes: 22 additions & 2 deletions landsatxplore/api.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
class API(object):
"""EarthExplorer API."""

def __init__(self, username, password):
def __init__(self, username, password = None, token = None):
"""EarthExplorer API.

Parameters
Expand All @@ -32,7 +32,11 @@ def __init__(self, username, password):
"""
self.url = API_URL
self.session = requests.Session()
self.login(username, password)
if token:
self.login_with_token(username, token)
else:
print("Please move to token login, the password authencation will be unavailable starting February 2025")
self.login(username, password)

@staticmethod
def raise_api_error(response):
Expand Down Expand Up @@ -103,6 +107,22 @@ def login(self, username, password):
self.raise_api_error(r)
self.session.headers["X-Auth-Token"] = r.json().get("data")

def login_with_token(self, username, token):
"""Get an API key.

Parameters
----------
username : str
EarthExplorer username.
password : str
EarthExplorer password.
"""
login_url = urljoin(self.url, "login-token")
payload = {"username": username, "token": token}
r = self.session.post(login_url, json.dumps(payload))
self.raise_api_error(r)
self.session.headers["X-Auth-Token"] = r.json().get("data")

def logout(self):
"""Logout from USGS M2M API."""
self.request("logout")
Expand Down
Loading