Skip to content

Commit 2f36b2d

Browse files
committed
feat: add support to configure redirects
1 parent 914035f commit 2f36b2d

File tree

4 files changed

+42
-5
lines changed

4 files changed

+42
-5
lines changed

README.rst

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1483,6 +1483,37 @@ Management API
14831483
asyncio.run(main())
14841484
14851485
1486+
Proxy and redirects
1487+
"""""""""""""""""""
1488+
1489+
You can configure the client to tunnel requests through an HTTP proxy.
1490+
The following proxy options are supported:
1491+
1492+
- ``proxy`` - Set this to configure the http proxy to be used, ex. ``http://localhost:3128``
1493+
- ``proxy_headers`` - A dictionary containing headers that will be sent to the proxy. Could be used for proxy authentication.
1494+
1495+
.. code-block:: python
1496+
1497+
from influxdb_client.client.influxdb_client_async import InfluxDBClientAsync
1498+
1499+
1500+
async with InfluxDBClientAsync(url="http://localhost:8086",
1501+
token="my-token",
1502+
org="my-org",
1503+
proxy="http://localhost:3128") as client:
1504+
1505+
.. note::
1506+
1507+
If your proxy notify the client with permanent redirect (``HTTP 301``) to **different host**.
1508+
The client removes ``Authorization`` header, because otherwise the contents of ``Authorization`` is sent to third parties
1509+
which is a security vulnerability.
1510+
1511+
Client automatically follows HTTP redirects. The default redirect policy is to follow up to ``10`` consecutive requests. The redirects can be configured via:
1512+
1513+
- ``allow_redirects`` - If set to ``False``, do not follow HTTP redirects. ``True`` by default.
1514+
- ``max_redirects`` - Maximum number of HTTP redirects to follow. ``10`` by default.
1515+
1516+
14861517
.. marker-asyncio-end
14871518
14881519
Local tests

influxdb_client/_async/api_client.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -63,14 +63,14 @@ class ApiClientAsync(object):
6363
_pool = None
6464

6565
def __init__(self, configuration=None, header_name=None, header_value=None,
66-
cookie=None, pool_threads=None, retries=False):
66+
cookie=None, pool_threads=None, **kwargs):
6767
"""Initialize generic API client."""
6868
if configuration is None:
6969
configuration = Configuration()
7070
self.configuration = configuration
7171
self.pool_threads = pool_threads
7272

73-
self.rest_client = rest.RESTClientObjectAsync(configuration)
73+
self.rest_client = rest.RESTClientObjectAsync(configuration, **kwargs)
7474
self.default_headers = {}
7575
if header_name is not None:
7676
self.default_headers[header_name] = header_value

influxdb_client/_async/rest.py

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -81,7 +81,7 @@ class RESTClientObjectAsync(object):
8181
Do not edit the class manually.
8282
"""
8383

84-
def __init__(self, configuration, pools_size=4, maxsize=None):
84+
def __init__(self, configuration, pools_size=4, maxsize=None, **kwargs):
8585
"""Initialize REST client."""
8686
# maxsize is number of requests to host that are allowed in parallel
8787
if maxsize is None:
@@ -104,6 +104,8 @@ def __init__(self, configuration, pools_size=4, maxsize=None):
104104

105105
self.proxy = configuration.proxy
106106
self.proxy_headers = configuration.proxy_headers
107+
self.allow_redirects = kwargs.get('allow_redirects', True)
108+
self.max_redirects = kwargs.get('max_redirects', 10)
107109

108110
# configure tracing
109111
trace_config = aiohttp.TraceConfig()
@@ -169,7 +171,9 @@ async def request(self, method, url, query_params=None, headers=None,
169171
args = {
170172
"method": method,
171173
"url": url,
172-
"headers": headers
174+
"headers": headers,
175+
"allow_redirects": self.allow_redirects,
176+
"max_redirects": self.max_redirects
173177
}
174178

175179
if self.proxy:

influxdb_client/client/influxdb_client_async.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,13 +38,15 @@ def __init__(self, url, token, org: str = None, debug=None, timeout=10_000, enab
3838
:key bool auth_basic: Set this to true to enable basic authentication when talking to a InfluxDB 1.8.x that
3939
does not use auth-enabled but is protected by a reverse proxy with basic authentication.
4040
(defaults to false, don't set to true when talking to InfluxDB 2)
41+
:key bool allow_redirects: If set to ``False``, do not follow HTTP redirects. ``True`` by default.
42+
:key int max_redirects: Maximum number of HTTP redirects to follow. ``10`` by default.
4143
:key list[str] profilers: list of enabled Flux profilers
4244
"""
4345
super().__init__(url=url, token=token, org=org, debug=debug, timeout=timeout, enable_gzip=enable_gzip, **kwargs)
4446

4547
from .._async.api_client import ApiClientAsync
4648
self.api_client = ApiClientAsync(configuration=self.conf, header_name=self.auth_header_name,
47-
header_value=self.auth_header_value, retries=self.retries)
49+
header_value=self.auth_header_value, retries=self.retries, **kwargs)
4850

4951
async def __aenter__(self) -> 'InfluxDBClientAsync':
5052
"""

0 commit comments

Comments
 (0)