Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Adding Python 3 compatibility and time series endpoint #12

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
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
94 changes: 72 additions & 22 deletions openexchangerates/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,32 +2,35 @@

import requests

__version__ = '0.1.0'
__author__ = 'Metglobal'
__license__ = 'MIT'
__copyright__ = 'Copyright 2013 Metglobal'
__version__ = "0.1.0"
__author__ = "Metglobal"
__license__ = "MIT"
__copyright__ = "Copyright 2013 Metglobal"


class OpenExchangeRatesClientException(requests.exceptions.RequestException):
"""Base client exception wraps all kinds of ``requests`` lib exceptions"""

pass


class OpenExchangeRatesClient(object):
"""This class is a client implementation for openexchangerate.org service

"""
BASE_URL = 'http://openexchangerates.org/api'
ENDPOINT_LATEST = BASE_URL + '/latest.json'
ENDPOINT_CURRENCIES = BASE_URL + '/currencies.json'
ENDPOINT_HISTORICAL = BASE_URL + '/historical/%s.json'

BASE_URL = "http://openexchangerates.org/api"
ENDPOINT_LATEST = BASE_URL + "/latest.json"
ENDPOINT_CURRENCIES = BASE_URL + "/currencies.json"
ENDPOINT_HISTORICAL = BASE_URL + "/historical/%s.json"
ENDPOINT_TIME_SERIES = BASE_URL + "/time-series.json"

def __init__(self, api_key):
"""Convenient constructor"""
self.client = requests.Session()
self.client.params.update({'app_id': api_key})
self.client.params.update({"app_id": api_key})

def latest(self, base='USD'):
def latest(self, base="USD"):
"""Fetches latest exchange rate data from service

:Example Data:
Expand All @@ -47,12 +50,11 @@ def latest(self, base='USD'):
}
"""
try:
resp = self.client.get(self.ENDPOINT_LATEST, params={'base': base})
resp = self.client.get(self.ENDPOINT_LATEST, params={"base": base})
resp.raise_for_status()
except requests.exceptions.RequestException, e:
except requests.exceptions.RequestException as e:
raise OpenExchangeRatesClientException(e)
return resp.json(parse_int=decimal.Decimal,
parse_float=decimal.Decimal)
return resp.json(parse_int=decimal.Decimal, parse_float=decimal.Decimal)

def currencies(self):
"""Fetches current currency data of the service
Expand All @@ -75,12 +77,12 @@ def currencies(self):
"""
try:
resp = self.client.get(self.ENDPOINT_CURRENCIES)
except requests.exceptions.RequestException, e:
except requests.exceptions.RequestException as e:
raise OpenExchangeRatesClientException(e)

return resp.json()

def historical(self, date, base='USD'):
def historical(self, date, base="USD"):
"""Fetches historical exchange rate data from service

:Example Data:
Expand All @@ -100,11 +102,59 @@ def historical(self, date, base='USD'):
}
"""
try:
resp = self.client.get(self.ENDPOINT_HISTORICAL %
date.strftime("%Y-%m-%d"),
params={'base': base})
resp = self.client.get(
self.ENDPOINT_HISTORICAL % "{0:%Y-%m-%d}".format(date),
params={"base": base},
)
resp.raise_for_status()
except requests.exceptions.RequestException as e:
raise OpenExchangeRatesClientException(e)
return resp.json(parse_int=decimal.Decimal, parse_float=decimal.Decimal)

def time_series(self, start, end, base="EUR"):
"""Fetches historical exchange rate data from service

:Example Data:
{
disclaimer: "<Disclaimer data>",
license: "<License data>",
"start_date": "2018-09-01",
"end_date": "2018-09-04",
"base": "USD",
"rates": {
"2018-09-01": {
"BRL": 4.055308,
"EUR": 0.8612,
"GBP": 0.771545
},
"2018-09-02": {
"BRL": 4.055175,
"EUR": 0.862199,
"GBP": 0.773893
},
"2018-09-03": {
"BRL": 4.156288,
"EUR": 0.860993,
"GBP": 0.777115
},
"2018-09-04": {
"BRL": 4.1565,
"EUR": 0.863017,
"GBP": 0.777736
}
}
}
"""
try:
resp = self.client.get(
self.ENDPOINT_TIME_SERIES,
params={
"start": "{0:%Y-%m-%d}".format(start),
"end": "{0:%Y-%m-%d}".format(end),
"base": base,
},
)
resp.raise_for_status()
except requests.exceptions.RequestException, e:
except requests.exceptions.RequestException as e:
raise OpenExchangeRatesClientException(e)
return resp.json(parse_int=decimal.Decimal,
parse_float=decimal.Decimal)
return resp.json(parse_int=decimal.Decimal, parse_float=decimal.Decimal)