From 64c7fc0dd2eac6aa31b47215622e8c7f550ed3c4 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Vlada=20Petrovi=C4=87?= Date: Thu, 9 Jun 2016 13:46:07 +0200 Subject: [PATCH 1/3] add changelog --- CHANGELOG.md | 36 ++++++++++++++++++++++++++++++++++++ 1 file changed, 36 insertions(+) create mode 100644 CHANGELOG.md diff --git a/CHANGELOG.md b/CHANGELOG.md new file mode 100644 index 0000000..34b6eaf --- /dev/null +++ b/CHANGELOG.md @@ -0,0 +1,36 @@ +# Change Log +Following guidelines of http://keepachangelog.com/ + +## [Unreleased] +- update to support Databox API version 3 + +## [0.1.6] - Jan 24, 2016 +- Changes related to last_push +- Fix for broken example.py +- Fix for broken tests +- Fix for broken test suite + +## [0.1.5] - Jan 4, 2016 +- First public release of Databox for Python. + +## [0.1.4] - Jul 14, 2015 +- Support for additional attributes +- Last push support number of pushes to retrieve +- Updated test suite + +## [0.1.1] - Jun 4, 2015 +- Client code was rewritten +- Test suite was added +- Examples ware added +- Documentation was updated +- Travis CI was introduced + +## [0.1] - May 20, 2015 +- Initial release + +[Unreleased]: https://github.com/databox/databox-go/compare/0.1.6...master +[0.1.6]: https://github.com/databox/databox-python/compare/0.1.5...0.1.6 +[0.1.5]: https://github.com/databox/databox-python/compare/0.1.4...0.1.5 +[0.1.4]: https://github.com/databox/databox-python/compare/0.1.1...0.1.4 +[0.1.1]: https://github.com/databox/databox-python/compare/0.1...0.1.1 +[0.1]: https://github.com/databox/databox-python/tree/0.1 From c9016888b3fcd2a0ed59c4e3e8b864332c673d3c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Vlada=20Petrovi=C4=87?= Date: Tue, 30 Aug 2016 09:05:49 +0200 Subject: [PATCH 2/3] updates - request headers - method get_push() - method metrics() - method purge() - add unit support --- databox/__init__.py | 51 ++++++++++++++++++++++++++++++++++----------- 1 file changed, 39 insertions(+), 12 deletions(-) diff --git a/databox/__init__.py b/databox/__init__.py index 3ec3436..8680bc7 100644 --- a/databox/__init__.py +++ b/databox/__init__.py @@ -3,12 +3,12 @@ from os import getenv from json import dumps as json_dumps -__version__ = "0.1.6" +__version__ = "2.0.0" class Client(object): push_token = None - push_host = 'https://push2new.databox.com' + push_host = 'https://push.databox.com' last_push_content = None class MissingToken(Exception): @@ -25,8 +25,6 @@ def __init__(self, token=None): if self.push_token is None: raise self.MissingToken('Push token is missing!') - self.push_host = getenv('DATABOX_PUSH_HOST', self.push_host) - def process_kpi(self, **args): key = args.get('key', None) if key is None: @@ -42,6 +40,10 @@ def process_kpi(self, **args): if date is not None: item['date'] = date + unit = args.get('unit', None) + if unit is not None: + item['unit'] = unit + attributes = args.get('attributes', None) if attributes is not None: item = dict(item.items() + attributes.items()) @@ -57,7 +59,8 @@ def _push_json(self, data=None, path="/"): auth=HTTPBasicAuth(self.push_token, ''), headers={ 'Content-Type': 'application/json', - 'User-Agent': "Databox/" + __version__ + " (Python)" + 'User-Agent': 'databox-python/' + __version__, + 'Accept': 'application/vnd.databox.v' + __version__.split('.')[0] + '+json' }, data=data ) @@ -70,37 +73,61 @@ def _get_json(self, path): auth=HTTPBasicAuth(self.push_token, ''), headers={ 'Content-Type': 'application/json', - 'User-Agent': "Databox/" + __version__ + " (Python)" + 'User-Agent': 'databox-python/' + __version__, + 'Accept': 'application/vnd.databox.v' + __version__.split('.')[0] + '+json' + } + ) + + return response.json() + + def _delete_json(self, path): + response = requests.delete( + self.push_host + path, + auth=HTTPBasicAuth(self.push_token, ''), + headers={ + 'Content-Type': 'application/json', + 'User-Agent': 'databox-python/' + __version__, + 'Accept': 'application/vnd.databox.v' + __version__.split('.')[0] + '+json' } ) return response.json() - def push(self, key, value, date=None, attributes=None): + def push(self, key, value, date=None, attributes=None, unit=None): self.last_push_content = self._push_json({ 'data': [self.process_kpi( key=key, value=value, date=date, + unit=unit, attributes=attributes )] }) - return self.last_push_content['status'] == 'ok' + return 'id' in self.last_push_content def insert_all(self, rows): self.last_push_content = self._push_json({ 'data': [self.process_kpi(**row) for row in rows] }) - return self.last_push_content['status'] == 'ok' + return 'id' in self.last_push_content def last_push(self, number=1): - return self._get_json(path='/lastpushes/{n}'.format(**{'n': number})) + return self._get_json(path='/lastpushes?limit={n}'.format(**{'n': number})) + + def get_push(self, sha): + return self._get_json(path='/lastpushes?id=' + sha) + + def metrics(self): + return self._get_json(path='/metrickeys') + + def purge(self): + return self._delete_json(path='/data') -def push(key, value, date=None, token=None): - return Client(token).push(key, value, date) +def push(key, value, date=None, attributes=None, unit=None, token=None): + return Client(token).push(key, value, date, attributes, unit) def insert_all(rows=[], token=None): From 8858a9b174d818b56f80cf2f42e65eff373fb287 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Vlada=20Petrovi=C4=87?= Date: Tue, 30 Aug 2016 09:06:04 +0200 Subject: [PATCH 3/3] tests, example, changelog --- CHANGELOG.md | 11 +++++++++-- databox test/test_push.py | 4 ++-- example.py | 12 +++++++++--- 3 files changed, 20 insertions(+), 7 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 34b6eaf..6aaaf7b 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -2,7 +2,13 @@ Following guidelines of http://keepachangelog.com/ ## [Unreleased] -- update to support Databox API version 3 + +## [2.0.0] - Aug 19, 2016 +- implement `GET /metrickeys` +- update `GET /lastpushes` +- implement `DELETE /data` +- update `user-agent` and `Accept` request headers +- update README ## [0.1.6] - Jan 24, 2016 - Changes related to last_push @@ -28,7 +34,8 @@ Following guidelines of http://keepachangelog.com/ ## [0.1] - May 20, 2015 - Initial release -[Unreleased]: https://github.com/databox/databox-go/compare/0.1.6...master +[Unreleased]: https://github.com/databox/databox-go/compare/2.0.0...master +[2.0.0]: https://github.com/databox/databox-python/compare/0.1.6...2.0.0 [0.1.6]: https://github.com/databox/databox-python/compare/0.1.5...0.1.6 [0.1.5]: https://github.com/databox/databox-python/compare/0.1.4...0.1.5 [0.1.4]: https://github.com/databox/databox-python/compare/0.1.1...0.1.4 diff --git a/databox test/test_push.py b/databox test/test_push.py index 05955dd..597f835 100644 --- a/databox test/test_push.py +++ b/databox test/test_push.py @@ -5,7 +5,7 @@ def mock_push_json(data=None, path='/'): - return {'status': 'ok'} + return {'id': '2837643'} class TestPush(unittest.TestCase): @@ -68,7 +68,7 @@ def test_last_push(self): def test_last_push_with_number(self): self.client._get_json = lambda data=None, path='/': path - assert self.client.last_push(3) == '/lastpushes/3' + assert self.client.last_push(3) == '/lastpushes?limit=3' def test_short(self): diff --git a/example.py b/example.py index be1e6f8..1688d8f 100755 --- a/example.py +++ b/example.py @@ -13,8 +13,8 @@ from databox import Client client = Client(TOKEN) -# client.push('sales.total', 1447.0) -#client.push('orders.total', 32, date='2015-01-01 09:00:00') + +# client.push('orders.total', 32, date='2015-01-01 09:00:00') # key = 'temp.ljx' # rows = [] @@ -28,6 +28,8 @@ # # +push = client.push('transaction', 1447.4) + print client.insert_all([ {'key': 'temp.boston', 'value': 51}, {'key': 'temp.boston', 'value': 49, 'date': '2015-01-01 17:00:00'}, @@ -35,9 +37,13 @@ 'name': "Oto", 'price': 199 }}, + {'key': 'transaction', 'value': 45.6, 'unit': 'USD'} ]) print "--------" -print client.last_push(3) +lastPushes = client.last_push(3) +print client.get_push(lastPushes[0]['response']['body']['id']) +print client.metrics() +print client.purge()