Example #1
0
File: api.py Project: kapadia/usgs
def login(username, password, save=True):
    """
    Log in, creating a temporary API key and optionally storing it for later use.
    
    :param str username: Username of the USGS account to log in with.
    :param str password: Password of the USGS account to log in with.
    :param bool save: If true, the API key will be stored in a local file (~/.usgs)
        until `api.logout` is called to remove it. The stored key will be used by
        other functions to authenticate requests whenever an API key is not explicitly
        provided.
    """
    url = '{}/login'.format(USGS_API)
    payload = payloads.login(username, password)

    session = _create_session(api_key=None)
    created = datetime.now().isoformat()

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

    _check_for_usgs_error(response)

    api_key = response["data"]

    if api_key is None:
        raise USGSError(response.get("errorMessage", "Authentication failed"))

    if save:
        with open(TMPFILE, "w") as f:
            json.dump({"apiKey": api_key, "created": created}, f)

    return response
Example #2
0
def login(username, password, save=True):

    url = '{}/login'.format(USGS_API)
    payload = {"jsonRequest": payloads.login(username, password)}

    r = requests.post(url, payload)
    if r.status_code is not 200:
        raise USGSError(r.text)

    response = r.json()
    api_key = response["data"]

    if api_key is None:
        raise USGSError(response["error"])

    if save:
        with open(TMPFILE, "w") as f:
            f.write(api_key)

    return response
Example #3
0
File: api.py Project: EOFarm/usgs
def login(username, password, save=True):
    url = '{}/login'.format(USGS_API)
    payload = payloads.login(username, password)

    session = _create_session(api_key=None)
    created = datetime.now().isoformat()
    r = session.post(url, payload)
    if r.status_code != 200:
        raise USGSError(r.text)

    response = r.json()
    api_key = response["data"]

    if api_key is None:
        raise USGSError(response.get("errorMessage", "Authentication failed"))

    if save:
        with open(TMPFILE, "w") as f:
            json.dump({"apiKey": api_key, "created": created}, f)

    return response
Example #4
0
File: api.py Project: mapbox/usgs
def login(username, password, save=True, catalogId='EE'):

    url = '{}/login'.format(USGS_API)
    payload = {
        "jsonRequest": payloads.login(username, password, catalogId=catalogId)
    }

    r = requests.post(url, payload)
    if r.status_code is not 200:
        raise USGSError(r.text)

    response = r.json()
    api_key = response["data"]

    if api_key is None:
        raise USGSError(response["error"])

    if save:
        with open(TMPFILE, "w") as f:
            f.write(api_key)

    return response
Example #5
0
 def test_login(self):
     expected = """{"username": "******", "password": "******"}"""
     payload = payloads.login("username", "password")
     assert compare_json(payload, expected)
Example #6
0
    def test_login(self):

        expected = """{"username": "******", "password": "******", "authType": "", "catalogId": "EE"}"""

        payload = payloads.login("username", "password")
        assert compare_json(payload, expected)
Example #7
0
    def test_login(self):

        expected = """{"username": "******", "password": "******", "authType": "", "catalogId": "EE"}"""

        payload = payloads.login("username", "password")
        assert compare_json(payload, expected)