kapadia / usgs

Client library for interfacing with USGS datasets
http://kapadia.github.io/usgs/
ISC License
109 stars 38 forks source link

TypeError on wrong username and password #35

Closed j08lue closed 7 years ago

j08lue commented 7 years ago

usgs version 0.2.0 on Windows 10, Miniconda, Python 3 throws a TypeError when username and/or password are incorrect:

import usgs.api
api_key = usgs.api.login('hello', 'abc')
---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
<ipython-input-2-e66707556ded> in <module>()
----> 1 api_key = usgs.api.login('hello', 'abc')

~\AppData\Local\Continuum\Miniconda3\envs\usgs\lib\site-packages\usgs\api.py in login(username, password, save)
    188     if save:
    189         with open(TMPFILE, "w") as f:
--> 190             f.write(api_key)
    191 
    192     return response

TypeError: write() argument must be str, not None

Seems like the server responds 200 despite the wrong credentials and returns a valid json response, just with None in data:

import usgs.api
import usgs.payloads
import requests

username = 'abc'
password = 'hello'

url = r'https://earthexplorer.usgs.gov/inventory/json/login'

payload = {
        "jsonRequest": usgs.payloads.login(username, password)
    }

r = requests.post(url, payload)
response = r.json()

where response becomes:

{'access_level': 'guest',
 'api_version': '1.3.0',
 'data': None,
 'error': 'Invalid Login',
 'errorCode': 'AUTH_INVALID',
 'executionTime': 0.19619297981262}

Will you change the code to analyze the response or do you want me to submit a PR? I suggest checking whether data is None and, if the case, raising an USGSError with the error field of the response.

kapadia commented 7 years ago

@j08lue thanks again for catching this and the PR. I've release 0.2.1 to pypi, feel free to use that: https://pypi.python.org/pypi/usgs/0.2.1