Skip to content

Commit

Permalink
Merge pull request #3 from databox/pushapi-v3
Browse files Browse the repository at this point in the history
Support Databox API version 3
  • Loading branch information
VladaPetrovic authored Aug 30, 2016
2 parents b73c654 + 8858a9b commit 053c3c0
Show file tree
Hide file tree
Showing 4 changed files with 93 additions and 17 deletions.
43 changes: 43 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
# Change Log
Following guidelines of http://keepachangelog.com/

## [Unreleased]

## [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
- 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/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
[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
4 changes: 2 additions & 2 deletions databox test/test_push.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@


def mock_push_json(data=None, path='/'):
return {'status': 'ok'}
return {'id': '2837643'}


class TestPush(unittest.TestCase):
Expand Down Expand Up @@ -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):
Expand Down
51 changes: 39 additions & 12 deletions databox/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand All @@ -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:
Expand All @@ -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())
Expand All @@ -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
)
Expand All @@ -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):
Expand Down
12 changes: 9 additions & 3 deletions example.py
Original file line number Diff line number Diff line change
Expand Up @@ -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 = []
Expand All @@ -28,16 +28,22 @@
#
#

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'},
{'key': 'sales.total', 'value': 3000, 'attributes': {
'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()

0 comments on commit 053c3c0

Please sign in to comment.