From 274b6f6422d4457ecf0562f8ca40ef43bcdfa1a1 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Bor=20Gonz=C3=A1lez-Usach?= Date: Tue, 3 Jul 2018 12:40:21 +0200 Subject: [PATCH] Use default DB in InfluxDBClient.write. Centralize default parameters in a function, and reuse these defaults in InfluxDBClient._write_points and .query. --- influxdb/client.py | 42 +++++++++++++++++++++++++----------------- 1 file changed, 25 insertions(+), 17 deletions(-) diff --git a/influxdb/client.py b/influxdb/client.py index 62d5a025..1dc3f3b8 100644 --- a/influxdb/client.py +++ b/influxdb/client.py @@ -306,10 +306,10 @@ def write(self, data, params=None, expected_response_code=204, headers = self._headers headers['Content-Type'] = 'application/octet-stream' - if params: - precision = params.get('precision') - else: - precision = None + final_params = self._get_default_params() + final_params.update(params or {}) + + precision = final_params.get('precision') if protocol == 'json': data = make_lines(data, precision).encode('utf-8') @@ -319,15 +319,20 @@ def write(self, data, params=None, expected_response_code=204, data = ('\n'.join(data) + '\n').encode('utf-8') self.request( - url="write", + url='write', method='POST', - params=params, + params=final_params, data=data, expected_response_code=expected_response_code, headers=headers ) return True + def _get_default_params(self): + return { + 'db': self._database, + } + @staticmethod def _read_chunked_response(response, raise_errors=True): result_set = {} @@ -387,24 +392,26 @@ def query(self, :returns: the queried data :rtype: :class:`~.ResultSet` """ - if params is None: - params = {} + final_params = self._get_default_params() + final_params.update(params or {}) + + final_params['q'] = query - params['q'] = query - params['db'] = database or self._database + if database is not None: + final_params['db'] = database if epoch is not None: - params['epoch'] = epoch + final_params['epoch'] = epoch if chunked: - params['chunked'] = 'true' + final_params['chunked'] = 'true' if chunk_size > 0: - params['chunk_size'] = chunk_size + final_params['chunk_size'] = chunk_size response = self.request( url="query", method='GET', - params=params, + params=final_params, data=None, expected_response_code=expected_response_code ) @@ -522,9 +529,10 @@ def _write_points(self, else: data = points - params = { - 'db': database or self._database - } + params = self._get_default_params() + + if database is not None: + params['db'] = database if time_precision is not None: params['precision'] = time_precision