Skip to content

Commit 77fdad5

Browse files
committed
fix: add functions for data uris
1 parent c1802fa commit 77fdad5

1 file changed

Lines changed: 30 additions & 0 deletions

File tree

src/query_farm_server_base/flight_handling.py

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import base64
12
import json
23
from collections.abc import Callable, Generator
34
from dataclasses import dataclass
@@ -77,6 +78,35 @@ def endpoint(
7778
)
7879

7980

81+
def dict_to_msgpack_data_uri(data: dict[str, Any]) -> str:
82+
"""
83+
Convert a dictionary to a data URI with MessagePack encoding.
84+
"""
85+
msgpack_bytes = msgpack.packb(data, use_bin_type=True)
86+
assert msgpack_bytes
87+
88+
# Encode as base64 for inclusion in the data URI
89+
b64_encoded = base64.b64encode(msgpack_bytes).decode("ascii")
90+
91+
# Construct the data URI
92+
return f"data:application/msgpack;base64,{b64_encoded}"
93+
94+
95+
def dict_to_msgpack_duckdb_call_data_uri(data: dict[str, Any]) -> str:
96+
"""
97+
Convert a dictionary to a data URI with MessagePack encoding for
98+
duckdb function calls.
99+
"""
100+
msgpack_bytes = msgpack.packb(data, use_bin_type=True)
101+
assert msgpack_bytes
102+
103+
# Encode as base64 for inclusion in the data URI
104+
b64_encoded = base64.b64encode(msgpack_bytes).decode("ascii")
105+
106+
# Construct the data URI
107+
return f"data:application/x-msgpack-duckdb-function-call;base64,{b64_encoded}"
108+
109+
80110
def decode_ticket_model(source: flight.Ticket, model_cls: type[AnyModel]) -> AnyModel:
81111
decode_fields: set[str] = set()
82112
for name, field in model_cls.model_fields.items():

0 commit comments

Comments
 (0)