Skip to content
This repository was archived by the owner on Oct 29, 2024. It is now read-only.

Commit c49c79a

Browse files
authored
feat: add support for custom authorization token
1 parent fc0235e commit c49c79a

File tree

5 files changed

+112
-2
lines changed

5 files changed

+112
-2
lines changed

docs/source/examples.rst

+6
Original file line numberDiff line numberDiff line change
@@ -31,3 +31,9 @@ Tutorials - UDP
3131

3232
.. literalinclude:: ../../examples/tutorial_udp.py
3333
:language: python
34+
35+
Tutorials - Authorization by Token
36+
===============
37+
38+
.. literalinclude:: ../../examples/tutorial_authorization.py
39+
:language: python

examples/tutorial_authorization.py

+32
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
# -*- coding: utf-8 -*-
2+
"""Tutorial how to authorize InfluxDB client by custom Authorization token."""
3+
4+
import argparse
5+
from influxdb import InfluxDBClient
6+
7+
8+
def main(token='my-token'):
9+
"""Instantiate a connection to the InfluxDB."""
10+
client = InfluxDBClient(username=None, password=None,
11+
headers={"Authorization": token})
12+
13+
print("Use authorization token: " + token)
14+
15+
version = client.ping()
16+
print("Successfully connected to InfluxDB: " + version)
17+
pass
18+
19+
20+
def parse_args():
21+
"""Parse the args from main."""
22+
parser = argparse.ArgumentParser(
23+
description='example code to play with InfluxDB')
24+
parser.add_argument('--token', type=str, required=False,
25+
default='my-token',
26+
help='Authorization token for the proxy that is ahead the InfluxDB.')
27+
return parser.parse_args()
28+
29+
30+
if __name__ == '__main__':
31+
args = parse_args()
32+
main(token=args.token)

influxdb/client.py

+2-1
Original file line numberDiff line numberDiff line change
@@ -328,10 +328,11 @@ def request(self, url, method='GET', params=None, data=None, stream=False,
328328
_try = 0
329329
while retry:
330330
try:
331+
auth = (self._username, self._password)
331332
response = self._session.request(
332333
method=method,
333334
url=url,
334-
auth=(self._username, self._password),
335+
auth=auth if None not in auth else None,
335336
params=params,
336337
data=data,
337338
stream=stream,

influxdb/tests/client_test.py

+71
Original file line numberDiff line numberDiff line change
@@ -1427,6 +1427,77 @@ def test_chunked_response(self):
14271427
'values': [['qps'], ['uptime'], ['df'], ['mount']]
14281428
}]}).__repr__())
14291429

1430+
def test_auth_default(self):
1431+
"""Test auth with default settings."""
1432+
with requests_mock.Mocker() as m:
1433+
m.register_uri(
1434+
requests_mock.GET,
1435+
"http://localhost:8086/ping",
1436+
status_code=204,
1437+
headers={'X-Influxdb-Version': '1.2.3'}
1438+
)
1439+
1440+
cli = InfluxDBClient()
1441+
cli.ping()
1442+
1443+
self.assertEqual(m.last_request.headers["Authorization"],
1444+
"Basic cm9vdDpyb290")
1445+
1446+
def test_auth_username_password(self):
1447+
"""Test auth with custom username and password."""
1448+
with requests_mock.Mocker() as m:
1449+
m.register_uri(
1450+
requests_mock.GET,
1451+
"http://localhost:8086/ping",
1452+
status_code=204,
1453+
headers={'X-Influxdb-Version': '1.2.3'}
1454+
)
1455+
1456+
cli = InfluxDBClient(username='my-username',
1457+
password='my-password')
1458+
cli.ping()
1459+
1460+
self.assertEqual(m.last_request.headers["Authorization"],
1461+
"Basic bXktdXNlcm5hbWU6bXktcGFzc3dvcmQ=")
1462+
1463+
def test_auth_username_password_none(self):
1464+
"""Test auth with not defined username or password."""
1465+
with requests_mock.Mocker() as m:
1466+
m.register_uri(
1467+
requests_mock.GET,
1468+
"http://localhost:8086/ping",
1469+
status_code=204,
1470+
headers={'X-Influxdb-Version': '1.2.3'}
1471+
)
1472+
1473+
cli = InfluxDBClient(username=None, password=None)
1474+
cli.ping()
1475+
self.assertFalse('Authorization' in m.last_request.headers)
1476+
1477+
cli = InfluxDBClient(username=None)
1478+
cli.ping()
1479+
self.assertFalse('Authorization' in m.last_request.headers)
1480+
1481+
cli = InfluxDBClient(password=None)
1482+
cli.ping()
1483+
self.assertFalse('Authorization' in m.last_request.headers)
1484+
1485+
def test_auth_token(self):
1486+
"""Test auth with custom authorization header."""
1487+
with requests_mock.Mocker() as m:
1488+
m.register_uri(
1489+
requests_mock.GET,
1490+
"http://localhost:8086/ping",
1491+
status_code=204,
1492+
headers={'X-Influxdb-Version': '1.2.3'}
1493+
)
1494+
1495+
cli = InfluxDBClient(username=None, password=None,
1496+
headers={"Authorization": "my-token"})
1497+
cli.ping()
1498+
self.assertEqual(m.last_request.headers["Authorization"],
1499+
"my-token")
1500+
14301501

14311502
class FakeClient(InfluxDBClient):
14321503
"""Set up a fake client instance of InfluxDBClient."""

tox.ini

+1-1
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ commands = pydocstyle --count -ve examples influxdb
3131
[testenv:coverage]
3232
deps = -r{toxinidir}/requirements.txt
3333
-r{toxinidir}/test-requirements.txt
34-
pandas
34+
pandas==0.24.2
3535
coverage
3636
numpy
3737
commands = nosetests -v --with-coverage --cover-html --cover-package=influxdb

0 commit comments

Comments
 (0)