From c4bf6605818c1e4f5d9f4827118702baa885b5b3 Mon Sep 17 00:00:00 2001 From: Cameron Yick Date: Thu, 24 Aug 2017 21:13:52 -0400 Subject: [PATCH] Create base HTTP Class --- setup.py | 1 + tiingo/restclient.py | 43 +++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 44 insertions(+) create mode 100644 tiingo/restclient.py diff --git a/setup.py b/setup.py index c9351b7..e4f0494 100644 --- a/setup.py +++ b/setup.py @@ -13,6 +13,7 @@ with open('HISTORY.rst') as history_file: requirements = [ # TODO: put package requirements here + 'requests' ] setup_requirements = [ diff --git a/tiingo/restclient.py b/tiingo/restclient.py new file mode 100644 index 0000000..3298b0d --- /dev/null +++ b/tiingo/restclient.py @@ -0,0 +1,43 @@ +# -*- coding: utf-8 -*- +import requests +from requests.exceptions import HTTPError + + +class RestClientError(Exception): + "Wrapper around HTTP Errors" + pass + + +class RestClient: + + def __init__(self, base_url, config): + """Base class for interacting with RESTful APIs + + 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'): + self._session = requests.Session() + else: + self._session = requests + + def __repr__(self): + return ''.format(self._base_url) + + def _request(self, method, url, **kwargs): + """Make HTTP request and return response object""" + resp = self._session.request(method, + '{}/{}'.format(self._base_url, url), + headers=self._headers, + **kwargs) + + try: + resp.raise_for_status() + except HTTPError as e: + raise RestClientError(e) + + return resp