Skip to content

Commit 7ed4743

Browse files
committed
Merge branch 'requests_session' of github.com:william-p/influxdb-python
Conflicts: influxdb/client.py
2 parents 93e819a + 1a9ced3 commit 7ed4743

File tree

2 files changed

+26
-24
lines changed

2 files changed

+26
-24
lines changed

influxdb/client.py

Lines changed: 15 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
from six.moves.urllib.parse import urlencode
88

99
import requests
10+
session = requests.Session()
1011

1112

1213
class InfluxDBClient(object):
@@ -63,7 +64,7 @@ def write_points(self, data):
6364
"""
6465
Write to multiple time series names
6566
"""
66-
response = requests.post(
67+
response = session.post(
6768
"{0}/db/{1}/series?u={2}&p={3}".format(
6869
self._baseurl,
6970
self._database,
@@ -88,7 +89,7 @@ def write_points_with_precision(self, data, time_precision='s'):
8889

8990
url_format = "{0}/db/{1}/series?u={2}&p={3}&time_precision={4}"
9091

91-
response = requests.post(url_format.format(
92+
response = session.post(url_format.format(
9293
self._baseurl,
9394
self._database,
9495
self._username,
@@ -176,7 +177,7 @@ def query(self, query, time_precision='s', chunked=False):
176177
url_format = "{0}/db/{1}/series?{2}&u={3}&p={4}"
177178
url_format += "&time_precision={5}&chunked={6}"
178179

179-
response = requests.get(url_format.format(
180+
response = session.get(url_format.format(
180181
self._baseurl,
181182
self._database,
182183
encoded_query,
@@ -208,7 +209,7 @@ def create_database(self, database):
208209
database: string
209210
database name
210211
"""
211-
response = requests.post("{0}/db?u={1}&p={2}".format(
212+
response = session.post("{0}/db?u={1}&p={2}".format(
212213
self._baseurl,
213214
self._username,
214215
self._password),
@@ -230,7 +231,7 @@ def delete_database(self, database):
230231
database: string
231232
database name
232233
"""
233-
response = requests.delete("{0}/db/{1}?u={2}&p={3}".format(
234+
response = session.delete("{0}/db/{1}?u={2}&p={3}".format(
234235
self._baseurl,
235236
database,
236237
self._username,
@@ -277,7 +278,7 @@ def get_list_cluster_admins(self):
277278
"""
278279
Get list of cluster admins
279280
"""
280-
response = requests.get(
281+
response = session.get(
281282
"{0}/cluster_admins?u={1}&p={2}".format(
282283
self._baseurl,
283284
self._username,
@@ -293,7 +294,7 @@ def add_cluster_admin(self, new_username, new_password):
293294
"""
294295
Add cluster admin
295296
"""
296-
response = requests.post(
297+
response = session.post(
297298
"{0}/cluster_admins?u={1}&p={2}".format(
298299
self._baseurl,
299300
self._username,
@@ -313,7 +314,7 @@ def update_cluster_admin_password(self, username, new_password):
313314
"""
314315
Update cluster admin password
315316
"""
316-
response = requests.post(
317+
response = session.post(
317318
"{0}/cluster_admins/{1}?u={2}&p={3}".format(
318319
self._baseurl,
319320
username,
@@ -333,7 +334,7 @@ def delete_cluster_admin(self, username):
333334
"""
334335
Delete cluster admin
335336
"""
336-
response = requests.delete("{0}/cluster_admins/{1}?u={2}&p={3}".format(
337+
response = session.delete("{0}/cluster_admins/{1}?u={2}&p={3}".format(
337338
self._baseurl,
338339
username,
339340
self._username,
@@ -358,7 +359,7 @@ def unset_database_admin(self, username):
358359
return self.alter_database_admin(username, False)
359360

360361
def alter_database_admin(self, username, is_admin):
361-
response = requests.post(
362+
response = session.post(
362363
"{0}/db/{1}/users/{2}?u={3}&p={4}".format(
363364
self._baseurl,
364365
self._database,
@@ -435,7 +436,7 @@ def get_database_users(self):
435436
"""
436437
Get list of database users
437438
"""
438-
response = requests.get(
439+
response = session.get(
439440
"{0}/db/{1}/users?u={2}&p={3}".format(
440441
self._baseurl,
441442
self._database,
@@ -452,7 +453,7 @@ def add_database_user(self, new_username, new_password):
452453
"""
453454
Add database user
454455
"""
455-
response = requests.post(
456+
response = session.post(
456457
"{0}/db/{1}/users?u={2}&p={3}".format(
457458
self._baseurl,
458459
self._database,
@@ -473,7 +474,7 @@ def update_database_user_password(self, username, new_password):
473474
"""
474475
Update password
475476
"""
476-
response = requests.post(
477+
response = session.post(
477478
"{0}/db/{1}/users/{2}?u={3}&p={4}".format(
478479
self._baseurl,
479480
self._database,
@@ -496,7 +497,7 @@ def delete_database_user(self, username):
496497
"""
497498
Delete database user
498499
"""
499-
response = requests.delete(
500+
response = session.delete(
500501
"{0}/db/{1}/users/{2}?u={3}&p={4}".format(
501502
self._baseurl,
502503
self._database,

tests/influxdb/client_test.py

Lines changed: 11 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
from mock import patch
1010

1111
from influxdb import InfluxDBClient
12+
from influxdb.client import session
1213

1314

1415
def _build_response_object(status_code=200, content=""):
@@ -42,14 +43,14 @@ def test_write_points(self):
4243
}
4344
]
4445

45-
with patch.object(requests, 'post') as mocked_post:
46+
with patch.object(session, 'post') as mocked_post:
4647
mocked_post.return_value = _build_response_object(status_code=200)
4748
cli = InfluxDBClient('host', 8086, 'username', 'password', 'db')
4849
assert cli.write_points(data) is True
4950

5051
@raises(Exception)
5152
def test_write_points_fails(self):
52-
with patch.object(requests, 'post') as mocked_post:
53+
with patch.object(session, 'post') as mocked_post:
5354
mocked_post.return_value = _build_response_object(status_code=500)
5455
cli = InfluxDBClient('host', 8086, 'username', 'password', 'db')
5556
cli.write_points([])
@@ -66,14 +67,14 @@ def test_write_points_with_precision(self):
6667
}
6768
]
6869

69-
with patch.object(requests, 'post') as mocked_post:
70+
with patch.object(session, 'post') as mocked_post:
7071
mocked_post.return_value = _build_response_object(status_code=200)
7172
cli = InfluxDBClient('host', 8086, 'username', 'password', 'db')
7273
assert cli.write_points_with_precision(data) is True
7374

7475
@raises(Exception)
7576
def test_write_points_with_precision_fails(self):
76-
with patch.object(requests, 'post') as mocked_post:
77+
with patch.object(session, 'post') as mocked_post:
7778
mocked_post.return_value = _build_response_object(status_code=500)
7879
cli = InfluxDBClient('host', 8086, 'username', 'password', 'db')
7980
cli.write_points_with_precision([])
@@ -100,7 +101,7 @@ def test_remove_scheduled_delete(self):
100101

101102
def test_query(self):
102103
expected = """[{"name":"foo","columns":["time","sequence_number","column_one"],"points":[[1383876043,16,"2"],[1383876043,15,"1"],[1383876035,14,"2"],[1383876035,13,"1"]]}]"""
103-
with patch.object(requests, 'get') as mocked_get:
104+
with patch.object(session, 'get') as mocked_get:
104105
mocked_get.return_value = _build_response_object(
105106
status_code=200,
106107
content=expected)
@@ -110,33 +111,33 @@ def test_query(self):
110111

111112
@raises(Exception)
112113
def test_query_fail(self):
113-
with patch.object(requests, 'get') as mocked_get:
114+
with patch.object(session, 'get') as mocked_get:
114115
mocked_get.return_value = _build_response_object(status_code=401)
115116
cli = InfluxDBClient('host', 8086, 'username', 'password', 'db')
116117
cli.query('select column_one from foo;')
117118

118119
def test_create_database(self):
119-
with patch.object(requests, 'post') as mocked_post:
120+
with patch.object(session, 'post') as mocked_post:
120121
mocked_post.return_value = _build_response_object(status_code=201)
121122
cli = InfluxDBClient('host', 8086, 'username', 'password', 'db')
122123
assert cli.create_database('new_db') is True
123124

124125
@raises(Exception)
125126
def test_creata_database_fails(self):
126-
with patch.object(requests, 'post') as mocked_post:
127+
with patch.object(session, 'post') as mocked_post:
127128
mocked_post.return_value = _build_response_object(status_code=401)
128129
cli = InfluxDBClient('host', 8086, 'username', 'password', 'db')
129130
cli.create_database('new_db')
130131

131132
def test_delete_database(self):
132-
with patch.object(requests, 'delete') as mocked_post:
133+
with patch.object(session, 'delete') as mocked_post:
133134
mocked_post.return_value = _build_response_object(status_code=204)
134135
cli = InfluxDBClient('host', 8086, 'username', 'password', 'db')
135136
assert cli.delete_database('old_db') is True
136137

137138
@raises(Exception)
138139
def test_delete_database_fails(self):
139-
with patch.object(requests, 'delete') as mocked_post:
140+
with patch.object(session, 'delete') as mocked_post:
140141
mocked_post.return_value = _build_response_object(status_code=401)
141142
cli = InfluxDBClient('host', 8086, 'username', 'password', 'db')
142143
cli.delete_database('old_db')

0 commit comments

Comments
 (0)