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

Commit

Permalink
Allow connecting to influxdb running on a path on the server (#556)
Browse files Browse the repository at this point in the history
* Allow connecting to influxdb running on a path on the server

Make it possible to connect to the databases on a path on servers.
https://someserver.com/myinfluxdb instead of the root of the server.

* Test and fix for None path
  • Loading branch information
gladhorn authored and xginn8 committed May 8, 2018
1 parent ed4561b commit f3c6acf
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 2 deletions.
19 changes: 17 additions & 2 deletions influxdb/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,8 @@ class InfluxDBClient(object):
:type udp_port: int
:param proxies: HTTP(S) proxy to use for Requests, defaults to {}
:type proxies: dict
:param path: path of InfluxDB on the server to connect, defaults to ''
:type path: str
"""

def __init__(self,
Expand All @@ -75,6 +77,7 @@ def __init__(self,
udp_port=4444,
proxies=None,
pool_size=10,
path='',
):
"""Construct a new InfluxDBClient object."""
self.__host = host
Expand All @@ -98,6 +101,13 @@ def __init__(self,
if use_udp:
self.udp_socket = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)

if not path:
self.__path = ''
elif path[0] == '/':
self.__path = path
else:
self.__path = '/' + path

self._scheme = "http"

if ssl is True:
Expand All @@ -110,10 +120,11 @@ def __init__(self,
else:
self._proxies = proxies

self.__baseurl = "{0}://{1}:{2}".format(
self.__baseurl = "{0}://{1}:{2}{3}".format(
self._scheme,
self._host,
self._port)
self._port,
self._path)

self._headers = {
'Content-Type': 'application/json',
Expand All @@ -132,6 +143,10 @@ def _host(self):
def _port(self):
return self.__port

@property
def _path(self):
return self.__path

@property
def _udp_port(self):
return self.__udp_port
Expand Down
18 changes: 18 additions & 0 deletions influxdb/tests/client_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,24 @@ def test_scheme(self):
)
self.assertEqual('https://host:8086', cli._baseurl)

cli = InfluxDBClient(
'host', 8086, 'username', 'password', 'database', ssl=True,
path="somepath"
)
self.assertEqual('https://host:8086/somepath', cli._baseurl)

cli = InfluxDBClient(
'host', 8086, 'username', 'password', 'database', ssl=True,
path=None
)
self.assertEqual('https://host:8086', cli._baseurl)

cli = InfluxDBClient(
'host', 8086, 'username', 'password', 'database', ssl=True,
path="/somepath"
)
self.assertEqual('https://host:8086/somepath', cli._baseurl)

def test_dsn(self):
"""Set up the test datasource name for TestInfluxDBClient object."""
cli = InfluxDBClient.from_dsn('influxdb://192.168.0.1:1886')
Expand Down

0 comments on commit f3c6acf

Please sign in to comment.