@@ -126,6 +126,28 @@ def build_query_param(**kwargs):
126126 return params
127127
128128
129+ async def build_query_param_async (** kwargs ):
130+ """
131+ Async version that offloads CPU-bound JSON serialization to thread pool.
132+
133+ Constructs a dictionary of query parameters from keyword arguments.
134+
135+ This function handles various data types:
136+ - JSON-serializable objects with a `to_json` method will be serialized using that method.
137+ - Booleans are converted to lowercase strings.
138+ - Lists are converted to comma-separated strings with URL-encoded values.
139+ - Other types (strings, integers, dictionaries) are handled appropriately.
140+
141+ Args:
142+ **kwargs: Arbitrary keyword arguments representing potential query parameters.
143+
144+ Returns:
145+ dict: A dictionary where keys are parameter names and values are URL-ready strings.
146+ """
147+ # Use sync version in thread pool to avoid blocking event loop
148+ return await asyncio .to_thread (build_query_param , ** kwargs )
149+
150+
129151def build_body_dict (** kwargs ):
130152 """
131153 Constructs a dictionary for the body of a request, handling nested structures.
@@ -153,6 +175,24 @@ def handle_value(value):
153175 return data
154176
155177
178+ async def build_body_dict_async (** kwargs ):
179+ """
180+ Async version that offloads CPU-bound to_dict() calls to thread pool.
181+
182+ Constructs a dictionary for the body of a request, handling nested structures.
183+ If an object has a `to_dict` method, it calls this method to serialize the object.
184+ It handles nested dictionaries and lists recursively.
185+
186+ Args:
187+ **kwargs: Keyword arguments representing keys and values to be included in the body dictionary.
188+
189+ Returns:
190+ dict: A dictionary with keys corresponding to kwargs keys and values processed, potentially recursively.
191+ """
192+ # Use sync version in thread pool to avoid blocking event loop
193+ return await asyncio .to_thread (build_body_dict , ** kwargs )
194+
195+
156196def configure_logging (level = None , handler = None , format = None ):
157197 """
158198 Configure logging for the Stream library.
@@ -232,7 +272,9 @@ async def sync_to_async(func, *args, **kwargs):
232272 "encode_datetime" ,
233273 "datetime_from_unix_ns" ,
234274 "build_query_param" ,
275+ "build_query_param_async" ,
235276 "build_body_dict" ,
277+ "build_body_dict_async" ,
236278 "validate_and_clean_url" ,
237279 "configure_logging" ,
238280 "UTC" ,
0 commit comments