Skip to content
This repository was archived by the owner on Jan 22, 2025. It is now read-only.

Commit adc4750

Browse files
authored
Merge pull request #50 from datafuselabs/feat/keep-query-id-header
feat: query with X-Databend-Query-Id header
2 parents 958239f + d8771d1 commit adc4750

File tree

2 files changed

+15
-1
lines changed

2 files changed

+15
-1
lines changed

databend_py/connection.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,8 @@
1515

1616
headers = {'Content-Type': 'application/json', 'User-Agent': sdk_info(), 'Accept': 'application/json',
1717
'X-DATABEND-ROUTE': 'warehouse'}
18+
XDatabendQueryIDHeader = "X-Databend-Query-Id"
19+
QueryID = "id"
1820

1921

2022
class ServerInfo(object):
@@ -146,10 +148,13 @@ def query(self, statement):
146148
else:
147149
self.client_session = self.default_session()
148150
query_sql['session'] = self.client_session
151+
if XDatabendQueryIDHeader in self.additional_headers:
152+
del self.additional_headers[XDatabendQueryIDHeader]
149153
log.logger.debug(f"http headers {self.make_headers()}")
150154
try:
151155
resp_dict = self.do_query(url, query_sql)
152156
self.client_session = resp_dict.get("session", self.default_session())
157+
self.additional_headers = {XDatabendQueryIDHeader: resp_dict.get(QueryID)}
153158
return resp_dict
154159
except Exception as err:
155160
log.logger.error(

tests/test_client.py

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -176,7 +176,7 @@ def test_cookies(self):
176176
client = Client.from_url(url_with_persist_cookies)
177177
client.execute("select 1")
178178
# self.assertIsNotNone(client.connection.cookies)
179-
179+
180180
def test_null_to_none(self):
181181
client = Client.from_url(self.databend_url)
182182
_, data = client.execute("select NULL as test")
@@ -190,6 +190,15 @@ def test_null_to_none(self):
190190
_, data = client.execute("select NULL as test")
191191
self.assertIsNone(data[0][0])
192192

193+
def test_set_query_id_header(self):
194+
client = Client.from_url(self.databend_url)
195+
client.execute("select 1")
196+
print(client.connection.additional_headers)
197+
execute_query_id1 = client.connection.additional_headers["X-Databend-Query-Id"]
198+
self.assertEqual("X-Databend-Query-Id" in client.connection.additional_headers, True)
199+
client.execute("select 2")
200+
self.assertNotEqual(execute_query_id1, client.connection.additional_headers["X-Databend-Query-Id"])
201+
193202

194203
if __name__ == '__main__':
195204
print("start test......")

0 commit comments

Comments
 (0)