Skip to content

Commit 5f7cff0

Browse files
committed
Allow custom requests.Session
By allowing the user to pass a custom requests.Session object, it becomes possible to manipulate the underlying HTTP connection, such as mock responses or transparently cache them.
1 parent 3dd0e1f commit 5f7cff0

File tree

2 files changed

+15
-2
lines changed

2 files changed

+15
-2
lines changed

README.rst

+13
Original file line numberDiff line numberDiff line change
@@ -80,6 +80,19 @@ When you are using `nginx` as proxy server for ClickHouse server connection stri
8080
8181
Where ``8124`` is proxy port.
8282

83+
If you need control over the underlying HTTP connection, pass a `requests.Session
84+
<https://requests.readthedocs.io/en/master/user/advanced/#session-objects>`_ instance
85+
to ``create_engine()``, like so:
86+
87+
.. code-block:: python
88+
89+
from sqlalchemy import create_engine
90+
from requests import Session
91+
92+
uri = 'clickhouse://default:@localhost/test'
93+
94+
engine = create_engine(uri, connect_args={'http_session': Session()})
95+
8396
8497
Native
8598
------

clickhouse_sqlalchemy/drivers/http/transport.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -85,8 +85,8 @@ def __init__(
8585
if ddl_timeout is not None:
8686
self.ch_settings['distributed_ddl_task_timeout'] = int(ddl_timeout)
8787

88-
# Keep connection open between queries.
89-
self.http = requests.Session()
88+
# By default, keep connection open between queries.
89+
self.http = kwargs.pop('http_session', requests.Session())
9090

9191
super(RequestsTransport, self).__init__()
9292

0 commit comments

Comments
 (0)