File tree Expand file tree Collapse file tree 2 files changed +33
-3
lines changed Expand file tree Collapse file tree 2 files changed +33
-3
lines changed Original file line number Diff line number Diff line change @@ -357,6 +357,15 @@ def close(self) -> None:
357
357
self ._idle_sessions .clear ()
358
358
359
359
360
+ # Customizable JSONEncoder to support additional types.
361
+ class SnowflakeRestfulJsonEncoder (json .JSONEncoder ):
362
+ def default (self , o ):
363
+ if isinstance (o , uuid .UUID ):
364
+ return str (o )
365
+
366
+ return super ().default (o )
367
+
368
+
360
369
class SnowflakeRestful :
361
370
"""Snowflake Restful class."""
362
371
@@ -503,7 +512,7 @@ def request(
503
512
return self ._post_request (
504
513
url ,
505
514
headers ,
506
- json .dumps (body ),
515
+ json .dumps (body , cls = SnowflakeRestfulJsonEncoder ),
507
516
token = self .token ,
508
517
_no_results = _no_results ,
509
518
timeout = timeout ,
@@ -565,7 +574,7 @@ def _token_request(self, request_type):
565
574
ret = self ._post_request (
566
575
url ,
567
576
headers ,
568
- json .dumps (body ),
577
+ json .dumps (body , cls = SnowflakeRestfulJsonEncoder ),
569
578
token = header_token ,
570
579
)
571
580
if ret .get ("success" ) and ret .get ("data" , {}).get ("sessionToken" ):
@@ -663,7 +672,7 @@ def delete_session(self, retry: bool = False) -> None:
663
672
ret = self ._post_request (
664
673
url ,
665
674
headers ,
666
- json .dumps (body ),
675
+ json .dumps (body , cls = SnowflakeRestfulJsonEncoder ),
667
676
token = self .token ,
668
677
timeout = 5 ,
669
678
no_retry = True ,
Original file line number Diff line number Diff line change 4
4
#
5
5
6
6
import io
7
+ import json
7
8
import unittest .mock
9
+ import uuid
8
10
from test .unit .mock_utils import mock_connection
9
11
10
12
import pytest
11
13
14
+ from src .snowflake .connector .network import SnowflakeRestfulJsonEncoder
15
+
12
16
try :
13
17
from snowflake .connector import Error , InterfaceError
14
18
from snowflake .connector .network import SnowflakeRestful
@@ -67,3 +71,20 @@ def test_fetch():
67
71
# if no retry is set to False, the function raises an InterfaceError
68
72
with pytest .raises (InterfaceError ) as exc :
69
73
assert rest .fetch (** default_parameters , no_retry = False )
74
+
75
+
76
+ @pytest .mark .parametrize (
77
+ "u" ,
78
+ [
79
+ uuid .uuid1 (),
80
+ uuid .uuid3 (uuid .NAMESPACE_URL , "www.snowflake.com" ),
81
+ uuid .uuid4 (),
82
+ uuid .uuid5 (uuid .NAMESPACE_URL , "www.snowflake.com" ),
83
+ ],
84
+ )
85
+ def test_json_serialize_uuid (u ):
86
+ expected = f'{{"u": "{ u } ", "a": 42}}'
87
+
88
+ assert (json .dumps (u , cls = SnowflakeRestfulJsonEncoder )) == f'"{ u } "'
89
+
90
+ assert json .dumps ({"u" : u , "a" : 42 }, cls = SnowflakeRestfulJsonEncoder ) == expected
You can’t perform that action at this time.
0 commit comments