Skip to content

Commit a6b369e

Browse files
committed
Marshalling: Refactor
1 parent 20e735f commit a6b369e

File tree

2 files changed

+14
-10
lines changed

2 files changed

+14
-10
lines changed

src/crate/client/http.py

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -113,6 +113,14 @@ def cratedb_json_encoder(obj):
113113
return obj
114114

115115

116+
def json_dumps(obj):
117+
return orjson.dumps(
118+
obj,
119+
default=cratedb_json_encoder,
120+
option=(orjson.OPT_PASSTHROUGH_DATETIME | orjson.OPT_SERIALIZE_NUMPY),
121+
)
122+
123+
116124
class Server:
117125
def __init__(self, server, **pool_kw):
118126
socket_options = _get_socket_opts(
@@ -340,11 +348,7 @@ def _create_sql_payload(stmt, args, bulk_args):
340348
data["args"] = args
341349
if bulk_args:
342350
data["bulk_args"] = bulk_args
343-
return orjson.dumps(
344-
data,
345-
default=cratedb_json_encoder,
346-
option=(orjson.OPT_PASSTHROUGH_DATETIME | orjson.OPT_SERIALIZE_NUMPY),
347-
)
351+
return json_dumps(data)
348352

349353

350354
def _get_socket_opts(

tests/client/test_http.py

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@
5151
Client,
5252
_get_socket_opts,
5353
_remove_certs_for_non_https,
54-
cratedb_json_encoder,
54+
json_dumps,
5555
)
5656

5757
REQUEST = "crate.client.http.Server.request"
@@ -724,10 +724,10 @@ def test_username(self):
724724
class TestCrateJsonEncoder(TestCase):
725725
def test_naive_datetime(self):
726726
data = dt.datetime.fromisoformat("2023-06-26T09:24:00.123")
727-
result = cratedb_json_encoder(data)
728-
self.assertEqual(result, 1687771440123)
727+
result = json_dumps(data)
728+
self.assertEqual(result, b"1687771440123")
729729

730730
def test_aware_datetime(self):
731731
data = dt.datetime.fromisoformat("2023-06-26T09:24:00.123+02:00")
732-
result = cratedb_json_encoder(data)
733-
self.assertEqual(result, 1687764240123)
732+
result = json_dumps(data)
733+
self.assertEqual(result, b"1687764240123")

0 commit comments

Comments
 (0)