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

Commit 0fd3aa0

Browse files
committed
feat: support cookie
1 parent a034ad8 commit 0fd3aa0

File tree

3 files changed

+28
-3
lines changed

3 files changed

+28
-3
lines changed

databend_py/connection.py

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,9 @@
44
import time
55
import uuid
66

7+
from http.cookiejar import Cookie
78
from requests.auth import HTTPBasicAuth
9+
from requests.cookies import RequestsCookieJar
810

911
import environs
1012
import requests
@@ -75,6 +77,17 @@ def get_error(response):
7577
return ServerException(response["error"]["message"], response["error"]["code"])
7678

7779

80+
class GlobalCookieJar(RequestsCookieJar):
81+
82+
def __init__(self):
83+
super().__init__()
84+
85+
def set_cookie(self, cookie: Cookie, *args, **kwargs):
86+
cookie.domain = ""
87+
cookie.path = "/"
88+
super().set_cookie(cookie, *args, **kwargs)
89+
90+
7891
class Connection(object):
7992
# Databend http handler doc: https://databend.rs/doc/reference/api/rest
8093

@@ -120,6 +133,10 @@ def __init__(
120133
self.context = Context()
121134
self.requests_session = requests.Session()
122135
self.schema = "http"
136+
cookie_jar = GlobalCookieJar()
137+
cookie_jar.set("cookie_enabled", "true")
138+
self.requests_session.cookies = cookie_jar
139+
self.schema = 'http'
123140
if self.secure:
124141
self.schema = "https"
125142
e = environs.Env()

tests/test_client.py

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -293,6 +293,14 @@ def test_cast_bool(self):
293293
_, data = client.execute("select 'False'::boolean union select 'True'::boolean")
294294
self.assertEqual(data, [(True,), (False,)])
295295

296+
def test_temp_table(self):
297+
client = Client.from_url(self.databend_url)
298+
client.execute("create temp table t1(a int)")
299+
client.execute("insert into t1 values (1)")
300+
_, data = client.execute("select * from t1")
301+
self.assertEqual(data, [(1,)])
302+
client.execute("drop table t1")
303+
296304

297305
if __name__ == "__main__":
298306
unittest.main()

tests/test_simple.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,15 +16,15 @@ def __setattr__(self, key, value):
1616

1717

1818
class TestDict(unittest.TestCase):
19-
databend_url = None # 使用类属性来存储 databend_url
19+
databend_url = None
2020

2121
@classmethod
2222
def setUpClass(cls):
23-
cls.databend_url = "test_url" # 在类级别设置 databend_url
23+
cls.databend_url = "test_url"
2424

2525
def test_init(self):
2626
d = Dict(a=1, b="test")
27-
self.assertEqual(self.databend_url, "test_url") # 使用类属性
27+
self.assertEqual(self.databend_url, "test_url")
2828
self.assertEqual(d.a, 1)
2929
self.assertEqual(d.b, "test")
3030
self.assertTrue(isinstance(d, dict))

0 commit comments

Comments
 (0)