Skip to content

Commit 053c3c0

Browse files
Merge pull request #3 from databox/pushapi-v3
Support Databox API version 3
2 parents b73c654 + 8858a9b commit 053c3c0

File tree

4 files changed

+93
-17
lines changed

4 files changed

+93
-17
lines changed

CHANGELOG.md

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
# Change Log
2+
Following guidelines of http://keepachangelog.com/
3+
4+
## [Unreleased]
5+
6+
## [2.0.0] - Aug 19, 2016
7+
- implement `GET /metrickeys`
8+
- update `GET /lastpushes`
9+
- implement `DELETE /data`
10+
- update `user-agent` and `Accept` request headers
11+
- update README
12+
13+
## [0.1.6] - Jan 24, 2016
14+
- Changes related to last_push
15+
- Fix for broken example.py
16+
- Fix for broken tests
17+
- Fix for broken test suite
18+
19+
## [0.1.5] - Jan 4, 2016
20+
- First public release of Databox for Python.
21+
22+
## [0.1.4] - Jul 14, 2015
23+
- Support for additional attributes
24+
- Last push support number of pushes to retrieve
25+
- Updated test suite
26+
27+
## [0.1.1] - Jun 4, 2015
28+
- Client code was rewritten
29+
- Test suite was added
30+
- Examples ware added
31+
- Documentation was updated
32+
- Travis CI was introduced
33+
34+
## [0.1] - May 20, 2015
35+
- Initial release
36+
37+
[Unreleased]: https://github.com/databox/databox-go/compare/2.0.0...master
38+
[2.0.0]: https://github.com/databox/databox-python/compare/0.1.6...2.0.0
39+
[0.1.6]: https://github.com/databox/databox-python/compare/0.1.5...0.1.6
40+
[0.1.5]: https://github.com/databox/databox-python/compare/0.1.4...0.1.5
41+
[0.1.4]: https://github.com/databox/databox-python/compare/0.1.1...0.1.4
42+
[0.1.1]: https://github.com/databox/databox-python/compare/0.1...0.1.1
43+
[0.1]: https://github.com/databox/databox-python/tree/0.1

databox test/test_push.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55

66

77
def mock_push_json(data=None, path='/'):
8-
return {'status': 'ok'}
8+
return {'id': '2837643'}
99

1010

1111
class TestPush(unittest.TestCase):
@@ -68,7 +68,7 @@ def test_last_push(self):
6868

6969
def test_last_push_with_number(self):
7070
self.client._get_json = lambda data=None, path='/': path
71-
assert self.client.last_push(3) == '/lastpushes/3'
71+
assert self.client.last_push(3) == '/lastpushes?limit=3'
7272

7373

7474
def test_short(self):

databox/__init__.py

Lines changed: 39 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -3,12 +3,12 @@
33
from os import getenv
44
from json import dumps as json_dumps
55

6-
__version__ = "0.1.6"
6+
__version__ = "2.0.0"
77

88

99
class Client(object):
1010
push_token = None
11-
push_host = 'https://push2new.databox.com'
11+
push_host = 'https://push.databox.com'
1212
last_push_content = None
1313

1414
class MissingToken(Exception):
@@ -25,8 +25,6 @@ def __init__(self, token=None):
2525
if self.push_token is None:
2626
raise self.MissingToken('Push token is missing!')
2727

28-
self.push_host = getenv('DATABOX_PUSH_HOST', self.push_host)
29-
3028
def process_kpi(self, **args):
3129
key = args.get('key', None)
3230
if key is None:
@@ -42,6 +40,10 @@ def process_kpi(self, **args):
4240
if date is not None:
4341
item['date'] = date
4442

43+
unit = args.get('unit', None)
44+
if unit is not None:
45+
item['unit'] = unit
46+
4547
attributes = args.get('attributes', None)
4648
if attributes is not None:
4749
item = dict(item.items() + attributes.items())
@@ -57,7 +59,8 @@ def _push_json(self, data=None, path="/"):
5759
auth=HTTPBasicAuth(self.push_token, ''),
5860
headers={
5961
'Content-Type': 'application/json',
60-
'User-Agent': "Databox/" + __version__ + " (Python)"
62+
'User-Agent': 'databox-python/' + __version__,
63+
'Accept': 'application/vnd.databox.v' + __version__.split('.')[0] + '+json'
6164
},
6265
data=data
6366
)
@@ -70,37 +73,61 @@ def _get_json(self, path):
7073
auth=HTTPBasicAuth(self.push_token, ''),
7174
headers={
7275
'Content-Type': 'application/json',
73-
'User-Agent': "Databox/" + __version__ + " (Python)"
76+
'User-Agent': 'databox-python/' + __version__,
77+
'Accept': 'application/vnd.databox.v' + __version__.split('.')[0] + '+json'
78+
}
79+
)
80+
81+
return response.json()
82+
83+
def _delete_json(self, path):
84+
response = requests.delete(
85+
self.push_host + path,
86+
auth=HTTPBasicAuth(self.push_token, ''),
87+
headers={
88+
'Content-Type': 'application/json',
89+
'User-Agent': 'databox-python/' + __version__,
90+
'Accept': 'application/vnd.databox.v' + __version__.split('.')[0] + '+json'
7491
}
7592
)
7693

7794
return response.json()
7895

79-
def push(self, key, value, date=None, attributes=None):
96+
def push(self, key, value, date=None, attributes=None, unit=None):
8097
self.last_push_content = self._push_json({
8198
'data': [self.process_kpi(
8299
key=key,
83100
value=value,
84101
date=date,
102+
unit=unit,
85103
attributes=attributes
86104
)]
87105
})
88106

89-
return self.last_push_content['status'] == 'ok'
107+
return 'id' in self.last_push_content
90108

91109
def insert_all(self, rows):
92110
self.last_push_content = self._push_json({
93111
'data': [self.process_kpi(**row) for row in rows]
94112
})
95113

96-
return self.last_push_content['status'] == 'ok'
114+
return 'id' in self.last_push_content
97115

98116
def last_push(self, number=1):
99-
return self._get_json(path='/lastpushes/{n}'.format(**{'n': number}))
117+
return self._get_json(path='/lastpushes?limit={n}'.format(**{'n': number}))
118+
119+
def get_push(self, sha):
120+
return self._get_json(path='/lastpushes?id=' + sha)
121+
122+
def metrics(self):
123+
return self._get_json(path='/metrickeys')
124+
125+
def purge(self):
126+
return self._delete_json(path='/data')
100127

101128

102-
def push(key, value, date=None, token=None):
103-
return Client(token).push(key, value, date)
129+
def push(key, value, date=None, attributes=None, unit=None, token=None):
130+
return Client(token).push(key, value, date, attributes, unit)
104131

105132

106133
def insert_all(rows=[], token=None):

example.py

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,8 @@
1313
from databox import Client
1414

1515
client = Client(TOKEN)
16-
# client.push('sales.total', 1447.0)
17-
#client.push('orders.total', 32, date='2015-01-01 09:00:00')
16+
17+
# client.push('orders.total', 32, date='2015-01-01 09:00:00')
1818

1919
# key = 'temp.ljx'
2020
# rows = []
@@ -28,16 +28,22 @@
2828
#
2929
#
3030

31+
push = client.push('transaction', 1447.4)
32+
3133
print client.insert_all([
3234
{'key': 'temp.boston', 'value': 51},
3335
{'key': 'temp.boston', 'value': 49, 'date': '2015-01-01 17:00:00'},
3436
{'key': 'sales.total', 'value': 3000, 'attributes': {
3537
'name': "Oto",
3638
'price': 199
3739
}},
40+
{'key': 'transaction', 'value': 45.6, 'unit': 'USD'}
3841
])
3942

4043
print "--------"
4144

42-
print client.last_push(3)
45+
lastPushes = client.last_push(3)
4346

47+
print client.get_push(lastPushes[0]['response']['body']['id'])
48+
print client.metrics()
49+
print client.purge()

0 commit comments

Comments
 (0)