From 99882552278f3690cb6428529ddeff9075561515 Mon Sep 17 00:00:00 2001 From: Cameron Yick Date: Thu, 24 Aug 2017 22:00:03 -0400 Subject: [PATCH] Initialize TiingoClient Class --- tiingo/__init__.py | 2 ++ tiingo/api.py | 47 ++++++++++++++++++++++++++++++++++++++++++++ tiingo/restclient.py | 8 ++++---- tiingo/tiingo.py | 3 --- 4 files changed, 53 insertions(+), 7 deletions(-) create mode 100644 tiingo/api.py delete mode 100644 tiingo/tiingo.py diff --git a/tiingo/__init__.py b/tiingo/__init__.py index a1afc92..987f69c 100644 --- a/tiingo/__init__.py +++ b/tiingo/__init__.py @@ -5,3 +5,5 @@ __author__ = """Cameron Yick""" __email__ = 'cameron.yick@enigma.com' __version__ = '0.1.0' + +from tiingo.api import TiingoClient diff --git a/tiingo/api.py b/tiingo/api.py new file mode 100644 index 0000000..85d9c9c --- /dev/null +++ b/tiingo/api.py @@ -0,0 +1,47 @@ +# -*- coding: utf-8 -*- + +import os +from tiingo.restclient import RestClient + + +class TiingoClient(RestClient): + """Class for managing interactions with the Tiingo Platform + + Supply API Key via Environment Variable TIINGO_API_KEY + or via the Config Object + """ + + def __init__(self, *args, **kwargs): + super(TiingoClient, self).__init__(*args, **kwargs) + self._base_url = "https://api.tiingo.com" + + try: + api_key = self._config['api_key'] + except KeyError: + api_key = os.environ.get('TIINGO_API_KEY') + + assert(api_key) + + self._headers = { + 'Authorization': "Token {}".format(api_key), + 'Content-Type': 'application/json', + 'User-Agent': 'tiingo-python-client' + } + + def __repr__(self): + return ''.format(self._base_url) + + # Define routes + def get_ticker_metadata(self, ticker): + """Return metadata for 1 ticker. + """ + url = "tiingo/daily/{}".format(ticker) + response = self._request('GET', url) + return response.json() + + + def get_latest_price(self, ticker): + raise NotImplementedError + + def get_historical_price(self, ticker, startDate, endDate): + raise NotImplementedError \ No newline at end of file diff --git a/tiingo/restclient.py b/tiingo/restclient.py index 3298b0d..fde4584 100644 --- a/tiingo/restclient.py +++ b/tiingo/restclient.py @@ -8,16 +8,16 @@ class RestClientError(Exception): pass -class RestClient: +class RestClient(object): - def __init__(self, base_url, config): + def __init__(self, config={}): """Base class for interacting with RESTful APIs + Child class MUST have a ._base_url property! + Args: - base (string): base URL of REST API config (dict): Arbitrary configuration options """ - self._base_url = base_url self._config = config if config.get('session'): diff --git a/tiingo/tiingo.py b/tiingo/tiingo.py deleted file mode 100644 index 7fbbae4..0000000 --- a/tiingo/tiingo.py +++ /dev/null @@ -1,3 +0,0 @@ -# -*- coding: utf-8 -*- - -"""Main module."""