1
1
from __future__ import absolute_import
2
2
3
- import influxdb2
4
- from influxdb2 import Configuration
3
+ from influxdb2 import Configuration , ApiClient , WriteService
5
4
from influxdb2 .client .authorizations_api import AuthorizationsApi
6
5
from influxdb2 .client .bucket_api import BucketsApi
7
6
from influxdb2 .client .labels_api import LabelsApi
14
13
15
14
class InfluxDBClient (object ):
16
15
17
- def __init__ (self ,
18
- url ,
19
- token ,
20
- auth_scheme = 'token' ,
21
- username = None ,
22
- password = None ,
23
- debug = None ,
24
- timeout = 10000 ,
25
- enable_gzip = False ,
26
- org = None ) -> None :
16
+ def __init__ (self , url , token , debug = None , timeout = 10000 , enable_gzip = False , org : str = None ) -> None :
27
17
"""
28
-
18
+ Creates a new client instance
19
+ :param url: InfluxDB server API url (ex. http://localhost:9999/api/v2)
20
+ :param token: auth token
21
+ :param debug: enable verbose logging of http requests
22
+ :param timeout: default http client timeout
29
23
:param enable_gzip: Enable Gzip compress for http requests. Currently only the "Write" and "Query" endpoints
30
24
supports the Gzip compression.
25
+ :param org: organization name (used as a default in query and write API)
26
+
31
27
"""
32
28
self .url = url
33
- self .auth_scheme = auth_scheme
34
29
self .token = token
35
- self .username = username
36
- self .password = password
37
30
self .timeout = timeout
38
31
self .org = org
39
32
@@ -46,17 +39,29 @@ def __init__(self,
46
39
auth_header_name = "Authorization"
47
40
auth_header_value = "Token " + auth_token
48
41
49
- self .api_client = influxdb2 . ApiClient (configuration = conf , header_name = auth_header_name ,
50
- header_value = auth_header_value )
42
+ self .api_client = ApiClient (configuration = conf , header_name = auth_header_name ,
43
+ header_value = auth_header_value )
51
44
52
- def write_api (self , write_options = WriteOptions ()):
53
- service = influxdb2 .service .write_service .WriteService (self .api_client )
45
+ def write_api (self , write_options = WriteOptions ()) -> WriteApi :
46
+ """
47
+ Creates a Write API instance
48
+ :param write_options: write api configuration
49
+ :return: write api instance
50
+ """
51
+ service = WriteService (self .api_client )
54
52
return WriteApi (service = service , write_options = write_options )
55
53
56
- def query_api (self ):
54
+ def query_api (self ) -> QueryApi :
55
+ """
56
+ Creates a Query API instance
57
+ :return: Query api instance
58
+ """
57
59
return QueryApi (self )
58
60
59
61
def close (self ):
62
+ """
63
+ Shutdowns the client
64
+ """
60
65
self .__del__ ()
61
66
62
67
def __del__ (self ):
@@ -65,26 +70,49 @@ def __del__(self):
65
70
self .api_client = None
66
71
67
72
def buckets_api (self ) -> BucketsApi :
73
+ """
74
+ Creates the Bucket API instance
75
+ :return: buckets api
76
+ """
68
77
return BucketsApi (self )
69
78
70
79
def authorizations_api (self ) -> AuthorizationsApi :
80
+ """
81
+ Creates the Authorizations API instance
82
+ :return: authorizations api
83
+ """
71
84
return AuthorizationsApi (self )
72
85
73
86
def users_api (self ) -> UsersApi :
87
+ """
88
+ Creates the Users api
89
+ :return: users api
90
+ """
74
91
return UsersApi (self )
75
92
76
93
def organizations_api (self ) -> OrganizationsApi :
94
+ """
95
+ Creates the Organizations api
96
+ :return: organizations api
97
+ """
77
98
return OrganizationsApi (self )
78
99
79
100
def tasks_api (self ) -> TasksApi :
101
+ """
102
+ Creates the Tasks api
103
+ :return: tasks api
104
+ """
80
105
return TasksApi (self )
81
106
82
107
def labels_api (self ) -> LabelsApi :
108
+ """
109
+ Creates the Labels api
110
+ :return: labels api
111
+ """
83
112
return LabelsApi (self )
84
113
85
114
86
115
class _Configuration (Configuration ):
87
-
88
116
def __init__ (self ):
89
117
Configuration .__init__ (self )
90
118
self .enable_gzip = False
@@ -111,5 +139,9 @@ def update_request_body(self, path: str, body):
111
139
# GZIP Request
112
140
if path == '/write' :
113
141
import gzip
114
- return gzip .compress (bytes (_body , "utf-8" ))
142
+ if isinstance (_body , bytes ):
143
+ return gzip .compress (data = _body )
144
+ else :
145
+ return gzip .compress (bytes (_body , "utf-8" ))
146
+
115
147
return _body
0 commit comments