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
13 changes: 0 additions & 13 deletions looker_api/helpers.py
Original file line number Diff line number Diff line change
@@ -1,13 +0,0 @@
from datetime import date

def tidy_dates(params):
""" Convert any date objects to a date string """

date_keys = ("from_date", "to_date", "date")
for date_key in date_keys:
if date_key in params:
if isinstance(params[date_key], date):
date_obj = params[date_key]
params[date_key] = date_obj.strftime("%Y-%m-%d")

return params
51 changes: 11 additions & 40 deletions looker_api/looker_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,6 @@

import requests

from helpers import tidy_dates

ENDPOINTS_DATA = {
}


class LookerInvalidEndpoint(Exception):
pass
Expand Down Expand Up @@ -57,15 +52,15 @@ def _do_request(self, url, kwargs):

status_code = r.status_code
if status_code == 400:
raise StatRequestError("Bad request")
raise LookerRequestError("Bad request")
elif status_code == 401:
raise StatRequestError("Unauthorized API key")
raise LookerRequestError("Unauthorized API key")
elif status_code == 403:
raise StatRequestError("Usage Limit Exceeded")
raise LookerRequestError("Usage Limit Exceeded")
elif status_code == 404:
raise StatRequestError("Not Found")
raise LookerRequestError("Not Found")
elif status_code == 500:
raise StatRequestError("Internal Server Error")
raise LookerRequestError("Internal Server Error")

response_data = r.json()

Expand All @@ -84,20 +79,20 @@ def _login(self):

status_code = r.status_code
if status_code == 400:
raise StatRequestError("Bad request")
raise LookerRequestError("Bad request")
elif status_code == 401:
raise StatRequestError("Unauthorized API key")
raise LookerRequestError("Unauthorized API key")
elif status_code == 403:
raise StatRequestError("Usage Limit Exceeded")
raise LookerRequestError("Usage Limit Exceeded")
elif status_code == 404:
raise StatRequestError("Not Found")
raise LookerRequestError("Not Found")
elif status_code == 500:
raise StatRequestError("Internal Server Error")
raise LookerRequestError("Internal Server Error")

response_data = r.json()

if 'access_token' not in response_data or 'expires_in' not in response_data:
raise StatResponseError(response_data)
raise LookerResponseError(response_data)

self.access_expiration = time.time() + (int(response_data['expires_in']) - 5)
self.access_token = response_data['access_token']
Expand All @@ -108,27 +103,3 @@ def run_look(self, look_id, rtn_format, **kwargs):
url = self._make_api_request_url(endpoint,rtn_format)

return self._do_request(url, kwargs)

def request(self, endpoint, **kwargs):
""" Make a request to the getstat.com API

endpoint should correspond to an endpoint listed in the documentation
kawrgs should be a dictionary of query parameters for the request
"""

if endpoint not in ENDPOINTS_DATA.keys():
raise StatInvalidEndpoint("The endpoint {endpoint} does not exist".format(endpoint))

allowed_parameters = ENDPOINTS_DATA[endpoint]
illegal_paramters = [key for key in kwargs.keys()
if key not in allowed_parameters]
if illegal_paramters:
raise InvalidParameters("The parameter(s) {parameters} are not legal"
" for the endpoint `{endpoint}`".format(
parameters=illegal_paramters,
endpoint=endpoint))

url = self._make_api_request_url(endpoint, "/json")
kwargs = tidy_dates(kwargs)

return self._do_request(url, kwargs)
5 changes: 5 additions & 0 deletions requirements.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
certifi==2018.4.16
chardet==3.0.4
idna==2.6
requests==2.18.4
urllib3==1.22