Skip to content

Commit 92ba774

Browse files
committed
Use a Session per InfluxDBClient instance
1 parent 6dfc326 commit 92ba774

File tree

2 files changed

+17
-19
lines changed

2 files changed

+17
-19
lines changed

influxdb/client.py

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,6 @@
1414
except NameError:
1515
xrange = range
1616

17-
session = requests.Session()
18-
1917

2018
class InfluxDBClientError(Exception):
2119
"""Raised when an error occurs in the request"""
@@ -85,6 +83,7 @@ def __init__(self,
8583

8684
self.use_udp = use_udp
8785
self.udp_port = udp_port
86+
self._session = requests.Session()
8887
if use_udp:
8988
self.udp_socket = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
9089

@@ -199,7 +198,7 @@ def request(self, url, method='GET', params=None, data=None,
199198
# TODO (aviau): Make this configurable.
200199
for i in range(0, 3):
201200
try:
202-
response = session.request(
201+
response = self._session.request(
203202
method=method,
204203
url=url,
205204
params=params,

tests/influxdb/client_test.py

Lines changed: 15 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,6 @@
2727
import mock
2828

2929
from influxdb import InfluxDBClient
30-
from influxdb.client import session
3130

3231

3332
def _build_response_object(status_code=200, content=""):
@@ -37,7 +36,7 @@ def _build_response_object(status_code=200, content=""):
3736
return resp
3837

3938

40-
def _mocked_session(method="GET", status_code=200, content=""):
39+
def _mocked_session(cli, method="GET", status_code=200, content=""):
4140

4241
method = method.upper()
4342

@@ -66,7 +65,7 @@ def request(*args, **kwargs):
6665
return _build_response_object(status_code=status_code, content=c)
6766

6867
mocked = patch.object(
69-
session,
68+
cli._session,
7069
'request',
7170
side_effect=request
7271
)
@@ -164,8 +163,8 @@ def test_write_points(self):
164163

165164
@unittest.skip('Not implemented for 0.9')
166165
def test_write_points_batch(self):
167-
with _mocked_session('post', 200, self.dummy_points):
168-
cli = InfluxDBClient('host', 8086, 'username', 'password', 'db')
166+
cli = InfluxDBClient('host', 8086, 'username', 'password', 'db')
167+
with _mocked_session(cli, 'post', 200, self.dummy_points):
169168
assert cli.write_points(
170169
data=self.dummy_points,
171170
batch_size=2
@@ -209,8 +208,8 @@ def test_write_bad_precision_udp(self):
209208

210209
@raises(Exception)
211210
def test_write_points_fails(self):
212-
with _mocked_session('post', 500):
213-
cli = InfluxDBClient('host', 8086, 'username', 'password', 'db')
211+
cli = InfluxDBClient('host', 8086, 'username', 'password', 'db')
212+
with _mocked_session(cli, 'post', 500):
214213
cli.write_points([])
215214

216215
def test_write_points_with_precision(self):
@@ -248,8 +247,8 @@ def test_write_points_bad_precision(self):
248247

249248
@raises(Exception)
250249
def test_write_points_with_precision_fails(self):
251-
with _mocked_session('post', 500):
252-
cli = InfluxDBClient('host', 8086, 'username', 'password', 'db')
250+
cli = InfluxDBClient('host', 8086, 'username', 'password', 'db')
251+
with _mocked_session(cli, 'post', 500):
253252
cli.write_points_with_precision([])
254253

255254
def test_query(self):
@@ -309,7 +308,7 @@ def test_query_chunked(self):
309308

310309
@raises(Exception)
311310
def test_query_fail(self):
312-
with _mocked_session('get', 401):
311+
with _mocked_session(self.cli, 'get', 401):
313312
self.cli.query('select column_one from foo;')
314313

315314
def test_create_database(self):
@@ -327,7 +326,7 @@ def test_create_database(self):
327326

328327
@raises(Exception)
329328
def test_create_database_fails(self):
330-
with _mocked_session('post', 401):
329+
with _mocked_session(self.cli, 'post', 401):
331330
self.cli.create_database('new_db')
332331

333332
def test_drop_database(self):
@@ -345,25 +344,25 @@ def test_drop_database(self):
345344

346345
@raises(Exception)
347346
def test_drop_database_fails(self):
348-
with _mocked_session('delete', 401):
349-
cli = InfluxDBClient('host', 8086, 'username', 'password', 'db')
347+
cli = InfluxDBClient('host', 8086, 'username', 'password', 'db')
348+
with _mocked_session(cli, 'delete', 401):
350349
cli.drop_database('old_db')
351350

352351
def test_get_list_database(self):
353352
data = {'results': [{'series': [
354353
{'name': 'databases', 'columns': ['name'],
355354
'values': [['mydb'], ['myotherdb']]}]}]}
356355

357-
with _mocked_session('get', 200, json.dumps(data)):
356+
with _mocked_session(self.cli, 'get', 200, json.dumps(data)):
358357
self.assertListEqual(
359358
self.cli.get_list_database(),
360359
['mydb', 'myotherdb']
361360
)
362361

363362
@raises(Exception)
364363
def test_get_list_database_fails(self):
365-
with _mocked_session('get', 401):
366-
cli = InfluxDBClient('host', 8086, 'username', 'password')
364+
cli = InfluxDBClient('host', 8086, 'username', 'password')
365+
with _mocked_session(cli, 'get', 401):
367366
cli.get_list_database()
368367

369368
def test_get_list_series(self):

0 commit comments

Comments
 (0)