diff --git a/nomics/__init__.py b/nomics/__init__.py index 77752e6..5edcd8d 100644 --- a/nomics/__init__.py +++ b/nomics/__init__.py @@ -7,7 +7,8 @@ Currencies, ExchangeRates, Markets, - Volume + Volume, + Usage ) class Nomics: @@ -19,6 +20,7 @@ def __init__(self, key): self.ExchangeRates = ExchangeRates(self) self.Markets = Markets(self) self.Volume = Volume(self) + self.Usage = Usage(self) def get_url(self, endpoint): return "https://api.nomics.com/v1/{}?key={}".format(endpoint, self.key) diff --git a/nomics/api/__init__.py b/nomics/api/__init__.py index 636a662..8e700e1 100644 --- a/nomics/api/__init__.py +++ b/nomics/api/__init__.py @@ -2,4 +2,5 @@ from .currencies import Currencies from .exchange_rates import ExchangeRates from .markets import Markets -from .volume import Volume \ No newline at end of file +from .volume import Volume +from .usage import Usage \ No newline at end of file diff --git a/nomics/api/currencies.py b/nomics/api/currencies.py index a4abf20..185de18 100644 --- a/nomics/api/currencies.py +++ b/nomics/api/currencies.py @@ -2,10 +2,22 @@ from .api import API + class Currencies(API): - def get_currencies(self, ids, interval = None, convert = None, status = None, filter = None, sort = None, - include_transparency = False, per_page = None, page = None): - ''' + def get_currencies( + self, + ids, + interval=None, + convert=None, + status=None, + filter=None, + platform_currency=None, + sort=None, + include_transparency=False, + per_page=None, + page=None, + ): + """ Returns price, volume, market cap, and rank for all currencies :param str ids: Comma separated list of Nomics Currency IDs @@ -29,6 +41,8 @@ def get_currencies(self, ids, interval = None, convert = None, status = None, fi active. Available options: "any" "new" + :param str platform-currency: Filter the results by parent platform. + :param str sort: How to sort the returned currencies. rank sorts by rank ascending and first_priced_at sorts by when each currency was first priced by Nomics descending. @@ -41,98 +55,72 @@ def get_currencies(self, ids, interval = None, convert = None, status = None, fi :param int page: Which page of items to get. Only applicable when per-page is also supplied. - ''' + """ if type(ids) != str: raise ValueError("ids must be a comma separated string. E.g. ids=BTC,ETH,XRP") if interval and type(interval) != str: - raise ValueError("interval must be a comma separated string. E.g. 1d,7d,30d,365d,ytd") - + raise ValueError( + "interval must be a comma separated string. E.g. 1d,7d,30d,365d,ytd" + ) - url = self.client.get_url('currencies/ticker') + url = self.client.get_url("currencies/ticker") params = { - 'ids': ids, - 'interval': interval, - 'convert': convert, - 'status': status, - 'filter': filter, - 'sort': sort, - 'include-transparency': include_transparency or None, - 'per-page': per_page, - 'page': page + "ids": ids, + "interval": interval, + "convert": convert, + "status": status, + "filter": filter, + "platform-currency": platform_currency, + "sort": sort, + "include-transparency": include_transparency or None, + "per-page": per_page, + "page": page, } - resp = requests.get(url, params = params) + resp = requests.get(url, params=params) if resp.status_code == 200: return resp.json() else: return resp.text - def get_metadata(self, ids = None, attributes = None): - ''' + def get_metadata(self, ids=None, attributes=None): + """ Returns all the currencies and their metadata that Nomics supports - :param [str] ids: Comma separated list of Nomics Currency IDs + :param [str] ids: Comma separated list of Nomics Currency IDs to filter result rows. Optional :param [str] attributes: Comma separated list of currency attributes to filter result columns Optional - ''' - - url = self.client.get_url('currencies') - params = { - 'ids': ids, - 'attributes': attributes - } - - resp = requests.get(url, params = params) - - if resp.status_code == 200: - return resp.json() - else: - return resp.text - - def get_sparkline(self, start, end = None): - ''' - Returns prices for all currencies within a customizable time interval suitable for sparkline charts. + """ - :param str start: Start time of the interval in RFC3339 format + url = self.client.get_url("currencies") + params = {"ids": ids, "attributes": attributes} - :param str end: End time of the interval in RFC3339 format. If not provided, the current time is used. - ''' - - url = self.client.get_url('currencies/sparkline') - params = { - 'start': start, - 'end': end - } - - resp = requests.get(url, params = params) + resp = requests.get(url, params=params) if resp.status_code == 200: return resp.json() else: return resp.text - def get_supplies_interval(self, start, end = None): - ''' + def get_supplies_interval(self, start, end=None): + """ Returns the open and close suplly information for all currencies between a customizable time interval :param str start: Start time of the interval in RFC3339 format :param str end: End time of the interval in RFC3339 format. If not provided, the current time is used. - ''' + """ - url = self.client.get_url('supplies/interval') - params = { - 'start': start, - 'end': end - } + url = self.client.get_url("supplies/interval") + params = {"start": start, "end": end} - resp = requests.get(url, params = params) + resp = requests.get(url, params=params) if resp.status_code == 200: return resp.json() else: - return resp.text \ No newline at end of file + return resp.text diff --git a/nomics/api/usage.py b/nomics/api/usage.py new file mode 100644 index 0000000..df71ac9 --- /dev/null +++ b/nomics/api/usage.py @@ -0,0 +1,51 @@ +import requests + +from .api import API + + +class Usage(API): + def API_History(self, start=None, end=None): + """ + Returns historical API usage data for metered API keys. + + :param [str] start: Start time in IS8601 format. + Optional + + :param [str] end: End time in IS8601 format. At most 30 days past start. + Optional + + """ + + url = self.client.get_url("meta/usage") + if (start and end) == None: + params = { + "start": start, + "end": end, + } + else: + params = { + } + + resp = requests.get(url, params=params) + + if resp.status_code == 200: + return resp.json() + else: + return resp.text + + def API_Summary(self): + """ + Returns summary of the API calls made during the current billing period. + + """ + + url = self.client.get_url("meta/overage") + params = { + } + + resp = requests.get(url, params=params) + + if resp.status_code == 200: + return resp.json() + else: + return resp.text diff --git a/tests/test_currencies.py b/tests/test_currencies.py index c5ff4a8..d0c595f 100644 --- a/tests/test_currencies.py +++ b/tests/test_currencies.py @@ -21,11 +21,6 @@ def test_get_metadata(nomics): assert isinstance(data, list) assert len(data) > 0 -def test_get_sparkline(nomics): - data = nomics.Currencies.get_sparkline(start = "2020-04-01T00:00:00Z") - assert isinstance(data, list) - assert len(data) > 0 - def test_get_supplies_interval(nomics): data = nomics.Currencies.get_supplies_interval(start = "2018-04-14T00:00:00Z") assert isinstance(data, list) diff --git a/tests/test_usage.py b/tests/test_usage.py new file mode 100644 index 0000000..8063f0f --- /dev/null +++ b/tests/test_usage.py @@ -0,0 +1,19 @@ +import logging +import pytest + +from config import NOMICS_API_KEY +from nomics import Nomics + +@pytest.fixture +def nomics(): + return Nomics(NOMICS_API_KEY) + +def test_API_History(nomics): + data = nomics.Usage.API_History(start = "2022-04-14T00:00:00Z", end = "2022-04-20T00:00:00Z") + assert isinstance(data, list) + assert len(data) > 0 + +def test_API_Summary(nomics): + data = nomics.Usage.API_Summary() + assert isinstance(data, dict) + assert len(data) > 0 \ No newline at end of file