Skip to content

Commit 07cb831

Browse files
committed
fixing write points in unicode
1 parent 0fb664e commit 07cb831

12 files changed

+189
-94
lines changed

influxdb2/client/__init__.py

+2-26
Original file line numberDiff line numberDiff line change
@@ -1,30 +1,6 @@
11
from __future__ import absolute_import
22

3-
# flake8: noqa
3+
from influxdb2.client.influxdb_client import InfluxDBClient
44

5+
# flake8: noqa
56
# import apis into api package
6-
from influxdb2.service.authorizations_service import AuthorizationsService
7-
from influxdb2.service.buckets_service import BucketsService
8-
from influxdb2.service.cells_service import CellsService
9-
from influxdb2.service.checks_service import ChecksService
10-
from influxdb2.service.dashboards_service import DashboardsService
11-
from influxdb2.service.health_service import HealthService
12-
from influxdb2.service.labels_service import LabelsService
13-
from influxdb2.service.notification_endpoints_service import NotificationEndpointsService
14-
from influxdb2.service.notification_rules_service import NotificationRulesService
15-
from influxdb2.service.operation_logs_service import OperationLogsService
16-
from influxdb2.service.organizations_service import OrganizationsService
17-
from influxdb2.service.query_service import QueryService
18-
from influxdb2.service.ready_service import ReadyService
19-
from influxdb2.service.scraper_targets_service import ScraperTargetsService
20-
from influxdb2.service.secrets_service import SecretsService
21-
from influxdb2.service.setup_service import SetupService
22-
from influxdb2.service.sources_service import SourcesService
23-
from influxdb2.service.tasks_service import TasksService
24-
from influxdb2.service.telegrafs_service import TelegrafsService
25-
from influxdb2.service.templates_service import TemplatesService
26-
from influxdb2.service.users_service import UsersService
27-
from influxdb2.service.variables_service import VariablesService
28-
from influxdb2.service.views_service import ViewsService
29-
from influxdb2.service.write_service import WriteService
30-
from influxdb2.service.default_service import DefaultService

influxdb2/client/influxdb_client.py

+55-23
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
from __future__ import absolute_import
22

3-
import influxdb2
4-
from influxdb2 import Configuration
3+
from influxdb2 import Configuration, ApiClient, WriteService
54
from influxdb2.client.authorizations_api import AuthorizationsApi
65
from influxdb2.client.bucket_api import BucketsApi
76
from influxdb2.client.labels_api import LabelsApi
@@ -14,26 +13,20 @@
1413

1514
class InfluxDBClient(object):
1615

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:
2717
"""
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
2923
:param enable_gzip: Enable Gzip compress for http requests. Currently only the "Write" and "Query" endpoints
3024
supports the Gzip compression.
25+
:param org: organization name (used as a default in query and write API)
26+
3127
"""
3228
self.url = url
33-
self.auth_scheme = auth_scheme
3429
self.token = token
35-
self.username = username
36-
self.password = password
3730
self.timeout = timeout
3831
self.org = org
3932

@@ -46,17 +39,29 @@ def __init__(self,
4639
auth_header_name = "Authorization"
4740
auth_header_value = "Token " + auth_token
4841

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)
5144

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)
5452
return WriteApi(service=service, write_options=write_options)
5553

56-
def query_api(self):
54+
def query_api(self) -> QueryApi:
55+
"""
56+
Creates a Query API instance
57+
:return: Query api instance
58+
"""
5759
return QueryApi(self)
5860

5961
def close(self):
62+
"""
63+
Shutdowns the client
64+
"""
6065
self.__del__()
6166

6267
def __del__(self):
@@ -65,26 +70,49 @@ def __del__(self):
6570
self.api_client = None
6671

6772
def buckets_api(self) -> BucketsApi:
73+
"""
74+
Creates the Bucket API instance
75+
:return: buckets api
76+
"""
6877
return BucketsApi(self)
6978

7079
def authorizations_api(self) -> AuthorizationsApi:
80+
"""
81+
Creates the Authorizations API instance
82+
:return: authorizations api
83+
"""
7184
return AuthorizationsApi(self)
7285

7386
def users_api(self) -> UsersApi:
87+
"""
88+
Creates the Users api
89+
:return: users api
90+
"""
7491
return UsersApi(self)
7592

7693
def organizations_api(self) -> OrganizationsApi:
94+
"""
95+
Creates the Organizations api
96+
:return: organizations api
97+
"""
7798
return OrganizationsApi(self)
7899

79100
def tasks_api(self) -> TasksApi:
101+
"""
102+
Creates the Tasks api
103+
:return: tasks api
104+
"""
80105
return TasksApi(self)
81106

82107
def labels_api(self) -> LabelsApi:
108+
"""
109+
Creates the Labels api
110+
:return: labels api
111+
"""
83112
return LabelsApi(self)
84113

85114

86115
class _Configuration(Configuration):
87-
88116
def __init__(self):
89117
Configuration.__init__(self)
90118
self.enable_gzip = False
@@ -111,5 +139,9 @@ def update_request_body(self, path: str, body):
111139
# GZIP Request
112140
if path == '/write':
113141
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+
115147
return _body

influxdb2/client/labels_api.py

+35-7
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,33 @@
1-
from typing import List
1+
from typing import List, Dict
22

33
from influxdb2 import LabelsService, LabelCreateRequest, Label, LabelUpdate
44

55

66
class LabelsApi(object):
7+
"""
8+
The client of the InfluxDB 2.0 that implements Labels HTTP API endpoint.
9+
"""
710

811
def __init__(self, influxdb_client):
912
self._influxdb_client = influxdb_client
1013
self._service = LabelsService(influxdb_client.api_client)
1114

12-
def create_label(self, name, org_id, properties=None) -> Label:
15+
def create_label(self, name: str, org_id: str, properties: Dict[str, str] = None) -> Label:
16+
"""
17+
Creates a new label.
18+
:param name: label name
19+
:param org_id: organization id
20+
:param properties: optional label properties
21+
:return: created label
22+
"""
1323
label_request = LabelCreateRequest(org_id=org_id, name=name, properties=properties)
1424
return self._service.post_labels(label_create_request=label_request).label
1525

1626
def update_label(self, label: Label):
1727
"""
18-
Update a label
28+
Updates an existing label name and properties.
1929
:param label: label
20-
:return:
30+
:return: the updated label
2131
"""
2232
label_update = LabelUpdate()
2333
label_update.properties = label.properties
@@ -26,10 +36,9 @@ def update_label(self, label: Label):
2636

2737
def delete_label(self, label):
2838
"""
29-
Delete the label
39+
Deletes the label.
3040
:param label: label id or Label
3141
:type label str or Label
32-
:return:
3342
"""
3443
label_id = None
3544

@@ -42,18 +51,37 @@ def delete_label(self, label):
4251
return self._service.delete_labels_id(label_id=label_id)
4352

4453
def clone_label(self, cloned_name: str, label: Label) -> Label:
54+
"""
55+
Creates the new instance of the label as a copy existing label.
56+
:param cloned_name: new label name
57+
:param label: existing label
58+
:return:
59+
"""
4560
cloned_properties = None
4661
if label.properties is not None:
4762
cloned_properties = label.properties.copy()
4863

4964
return self.create_label(name=cloned_name, properties=cloned_properties, org_id=label.org_id)
5065

5166
def find_labels(self) -> List['Label']:
67+
"""
68+
Gets all available labels.
69+
:return: labels
70+
"""
5271
return self._service.get_labels().labels
5372

5473
def find_label_by_id(self, label_id: str):
74+
"""
75+
Retrieves the label by id
76+
:param label_id:
77+
:return: Label
78+
"""
5579
return self._service.get_labels_id(label_id=label_id).label
5680

5781
def find_label_by_org(self, org_id) -> List['Label']:
58-
82+
"""
83+
Gets the list of all labels for given organization
84+
:param org_id: organization id
85+
:return: list of labels
86+
"""
5987
return self._service.get_labels(org_id=org_id).labels

influxdb2/client/organizations_api.py

+7-4
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,9 @@
22

33

44
class OrganizationsApi(object):
5+
"""
6+
The client of the InfluxDB 2.0 that implements Organizations HTTP API endpoint.
7+
"""
58

69
def __init__(self, influxdb_client):
710
self._influxdb_client = influxdb_client
@@ -12,16 +15,16 @@ def me(self):
1215
user = self._users_service.get_me()
1316
return user
1417

15-
def find_organization(self, id):
16-
return self._organizations_service.get_orgs_id(org_id=id)
18+
def find_organization(self, org_id):
19+
return self._organizations_service.get_orgs_id(org_id=org_id)
1720

1821
def find_organizations(self):
1922
return self._organizations_service.get_orgs()
2023

21-
def create_organization(self, name=None, organization=None) -> Organization:
24+
def create_organization(self, name: str = None, organization: Organization = None) -> Organization:
2225
if organization is None:
2326
organization = Organization(name=name)
24-
return self._organizations_service.post_orgs(organization)
27+
return self._organizations_service.post_orgs(organization=organization)
2528

2629
def delete_organization(self, org_id: str):
2730
return self._organizations_service.delete_orgs_id(org_id=org_id)

influxdb2/client/query_api.py

+32-4
Original file line numberDiff line numberDiff line change
@@ -1,35 +1,63 @@
1-
from influxdb2 import Query, QueryService
21
import codecs
32
import csv
3+
from typing import List
44

55
from influxdb2 import Dialect
6+
from influxdb2 import Query, QueryService
67
from influxdb2.client.flux_csv_parser import FluxCsvParser, FluxResponseConsumerTable
8+
from influxdb2.client.flux_table import FluxTable
79

810

911
class QueryApi(object):
1012
default_dialect = Dialect(header=True, delimiter=",", comment_prefix="#",
1113
annotations=["datatype", "group", "default"], date_time_format="RFC3339")
1214

1315
def __init__(self, influxdb_client):
16+
"""
17+
Initialize query client
18+
:param influxdb_client: influxdb client
19+
"""
1420
self._influxdb_client = influxdb_client
1521
self._query_api = QueryService(influxdb_client.api_client)
1622

17-
def query_csv(self, query, org=None, dialect=default_dialect):
23+
def query_csv(self, query: str, org=None, dialect: Dialect = default_dialect):
24+
"""
25+
Executes the Flux query and return results as a CSV iterator.
26+
Each iteration returns a row of the CSV file
27+
:param query: a Flux query
28+
:param org: organization name (optional if already specified in InfluxDBClient)
29+
:param dialect: csv dialect format
30+
:return: returns CSV iterator
31+
"""
1832
if org is None:
1933
org = self._influxdb_client.org
2034
response = self._query_api.post_query(org=org, query=self._create_query(query, dialect), async_req=False,
2135
_preload_content=False)
2236
return csv.reader(codecs.iterdecode(response, 'utf-8'))
2337

24-
def query_raw(self, query, org=None, dialect=default_dialect):
38+
def query_raw(self, query: str, org=None, dialect=default_dialect):
39+
"""
40+
Synchronously executes the Flux query and return result as raw unprocessed result as a str
41+
:param query: a Flux query
42+
:param org: organization name (optional if already specified in InfluxDBClient)
43+
:param dialect: csv dialect format
44+
:return: str
45+
"""
2546
if org is None:
2647
org = self._influxdb_client.org
2748
result = self._query_api.post_query(org=org, query=self._create_query(query, dialect), async_req=False,
2849
_preload_content=False)
2950
return result
3051
# return codecs.iterdecode(result, 'utf-8')
3152

32-
def query(self, query, org=None, dialect=default_dialect):
53+
def query(self, query, org=None, dialect=default_dialect) -> List['FluxTable']:
54+
"""
55+
Synchronously executes the Flux query and return result as a List['FluxTable']
56+
:param query: the Flux query
57+
:param org: organization name (optional if already specified in InfluxDBClient)
58+
:param dialect: csv dialect format
59+
:return:
60+
"""
3361
if org is None:
3462
org = self._influxdb_client.org
3563
response = self._query_api.post_query(org=org, query=self._create_query(query, dialect), async_req=False,

influxdb2/client/write/point.py

+2-17
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66

77
import dateutil.parser
88
from pytz import UTC
9-
from six import iteritems, binary_type, PY2
9+
from six import iteritems
1010

1111
from influxdb2.domain.write_precision import WritePrecision
1212

@@ -104,7 +104,7 @@ def _append_time(time, write_precission):
104104

105105

106106
def _escape_tag(tag):
107-
return _get_unicode(str(tag)).replace("\\", "\\\\").replace(" ", "\\ ").replace(",", "\\,").replace("=", "\\=")
107+
return str(tag).replace("\\", "\\\\").replace(" ", "\\ ").replace(",", "\\,").replace("=", "\\=")
108108

109109

110110
def _escape_tag_value(value):
@@ -149,18 +149,3 @@ def _convert_timestamp(timestamp, precision=DEFAULT_WRITE_PRECISION):
149149
# return ns / 1e9 / 3600
150150

151151
raise ValueError(timestamp)
152-
153-
154-
def _get_unicode(data, force=False):
155-
"""Try to return a text aka unicode object from the given data."""
156-
if isinstance(data, binary_type):
157-
return data.decode('utf-8')
158-
elif data is None:
159-
return ''
160-
elif force:
161-
if PY2:
162-
return unicode(data)
163-
else:
164-
return str(data)
165-
else:
166-
return data

0 commit comments

Comments
 (0)