33from os import getenv
44from json import dumps as json_dumps
55
6- __version__ = "0.1.6 "
6+ __version__ = "2.0.0 "
77
88
99class 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
106133def insert_all (rows = [], token = None ):
0 commit comments