11import io
22import json
33import logging
4- from typing import Any , Collection , Dict , List , Optional , Tuple , Type , Union
4+ from typing import Any , Callable , Collection , Dict , List , Optional , Tuple , Type , Union
55
66import requests
77from graphql import DocumentNode , ExecutionResult , print_ast
@@ -47,6 +47,8 @@ def __init__(
4747 method : str = "POST" ,
4848 retry_backoff_factor : float = 0.1 ,
4949 retry_status_forcelist : Collection [int ] = _default_retry_codes ,
50+ json_serialize : Callable = json .dumps ,
51+ json_deserialize : Callable = json .loads ,
5052 ** kwargs : Any ,
5153 ):
5254 """Initialize the transport with the given request parameters.
@@ -73,6 +75,10 @@ def __init__(
7375 should force a retry on. A retry is initiated if the request method is
7476 in allowed_methods and the response status code is in status_forcelist.
7577 (Default: [429, 500, 502, 503, 504])
78+ :param json_serialize: Json serializer callable.
79+ By default json.dumps() function
80+ :param json_deserialize: Json deserializer callable.
81+ By default json.loads() function
7682 :param kwargs: Optional arguments that ``request`` takes.
7783 These can be seen at the `requests`_ source code or the official `docs`_
7884
@@ -90,6 +96,8 @@ def __init__(
9096 self .method = method
9197 self .retry_backoff_factor = retry_backoff_factor
9298 self .retry_status_forcelist = retry_status_forcelist
99+ self .json_serialize : Callable = json_serialize
100+ self .json_deserialize : Callable = json_deserialize
93101 self .kwargs = kwargs
94102
95103 self .session = None
@@ -174,7 +182,7 @@ def execute( # type: ignore
174182 payload ["variables" ] = nulled_variable_values
175183
176184 # Add the payload to the operations field
177- operations_str = json . dumps (payload )
185+ operations_str = self . json_serialize (payload )
178186 log .debug ("operations %s" , operations_str )
179187
180188 # Generate the file map
@@ -188,7 +196,7 @@ def execute( # type: ignore
188196 file_streams = {str (i ): files [path ] for i , path in enumerate (files )}
189197
190198 # Add the file map field
191- file_map_str = json . dumps (file_map )
199+ file_map_str = self . json_serialize (file_map )
192200 log .debug ("file_map %s" , file_map_str )
193201
194202 fields = {"operations" : operations_str , "map" : file_map_str }
@@ -224,7 +232,7 @@ def execute( # type: ignore
224232
225233 # Log the payload
226234 if log .isEnabledFor (logging .INFO ):
227- log .info (">>> %s" , json . dumps (payload ))
235+ log .info (">>> %s" , self . json_serialize (payload ))
228236
229237 # Pass kwargs to requests post method
230238 post_args .update (self .kwargs )
@@ -257,7 +265,10 @@ def raise_response_error(resp: requests.Response, reason: str):
257265 )
258266
259267 try :
260- result = response .json ()
268+ if self .json_deserialize == json .loads :
269+ result = response .json ()
270+ else :
271+ result = self .json_deserialize (response .text )
261272
262273 if log .isEnabledFor (logging .INFO ):
263274 log .info ("<<< %s" , response .text )
@@ -396,7 +407,7 @@ def _build_batch_post_args(
396407
397408 # Log the payload
398409 if log .isEnabledFor (logging .INFO ):
399- log .info (">>> %s" , json . dumps (post_args [data_key ]))
410+ log .info (">>> %s" , self . json_serialize (post_args [data_key ]))
400411
401412 # Pass kwargs to requests post method
402413 post_args .update (self .kwargs )
0 commit comments