7
7
import requests
8
8
session = requests .Session ()
9
9
10
+ class InfluxDBClientError (Exception ):
11
+ "Raised when an error occures in the Request"
12
+ def __init__ (self , message , code ):
13
+ self .message = message
14
+ self .code = code
10
15
11
16
class InfluxDBClient (object ):
12
17
"""
13
18
InfluxDB Client
14
19
"""
15
20
16
- def __init__ (
17
- self ,
18
- host = 'localhost' ,
19
- port = 8086 ,
20
- username = 'root' ,
21
- password = 'root' ,
22
- database = None ,
23
- ssl = False ,
24
- verify_ssl = False ,
25
- use_udp = False ,
26
- udp_port = 4444 ):
27
-
21
+ def __init__ (self ,
22
+ host = 'localhost' ,
23
+ port = 8086 ,
24
+ username = 'root' ,
25
+ password = 'root' ,
26
+ database = None ,
27
+ ssl = False ,
28
+ verify_ssl = False ,
29
+ timeout = 0 ,
30
+ use_udp = False ,
31
+ udp_port = 4444 ):
28
32
"""
29
33
Initialize client
30
34
"""
@@ -33,6 +37,7 @@ def __init__(
33
37
self ._username = username
34
38
self ._password = password
35
39
self ._database = database
40
+ self ._timeout = timeout
36
41
37
42
self ._verify_ssl = verify_ssl
38
43
@@ -105,14 +110,16 @@ def request(self, url, method='GET', params=None, data=None,
105
110
params = params ,
106
111
data = data ,
107
112
headers = self ._headers ,
108
- verify = self ._verify_ssl
113
+ verify = self ._verify_ssl ,
114
+ timeout = self ._timeout
109
115
)
110
116
111
117
if response .status_code == status_code :
112
118
return response
119
+
113
120
else :
114
- raise Exception (
115
- "{0}: {1}" . format ( response . status_code , response . content ))
121
+ error = InfluxDBClientError ( "{0}: {1}" . format ( response . status_code , response . content ), response . status_code )
122
+ raise error
116
123
117
124
# Writing Data
118
125
#
@@ -273,7 +280,13 @@ def query(self, query, time_precision='s', chunked=False):
273
280
status_code = 200
274
281
)
275
282
276
- return json .loads (response .content )
283
+ try :
284
+ res = json .loads (response .content )
285
+ except TypeError :
286
+ # must decode in python 3
287
+ res = json .loads (response .content .decode ('utf8' ))
288
+
289
+ return res
277
290
278
291
# Creating and Dropping Databases
279
292
#
0 commit comments