1
1
import io
2
2
import json
3
3
import 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
5
5
6
6
import requests
7
7
from graphql import DocumentNode , ExecutionResult , print_ast
@@ -47,6 +47,8 @@ def __init__(
47
47
method : str = "POST" ,
48
48
retry_backoff_factor : float = 0.1 ,
49
49
retry_status_forcelist : Collection [int ] = _default_retry_codes ,
50
+ json_serialize : Callable = json .dumps ,
51
+ json_deserialize : Callable = json .loads ,
50
52
** kwargs : Any ,
51
53
):
52
54
"""Initialize the transport with the given request parameters.
@@ -73,6 +75,10 @@ def __init__(
73
75
should force a retry on. A retry is initiated if the request method is
74
76
in allowed_methods and the response status code is in status_forcelist.
75
77
(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
76
82
:param kwargs: Optional arguments that ``request`` takes.
77
83
These can be seen at the `requests`_ source code or the official `docs`_
78
84
@@ -90,6 +96,8 @@ def __init__(
90
96
self .method = method
91
97
self .retry_backoff_factor = retry_backoff_factor
92
98
self .retry_status_forcelist = retry_status_forcelist
99
+ self .json_serialize : Callable = json_serialize
100
+ self .json_deserialize : Callable = json_deserialize
93
101
self .kwargs = kwargs
94
102
95
103
self .session = None
@@ -174,7 +182,7 @@ def execute( # type: ignore
174
182
payload ["variables" ] = nulled_variable_values
175
183
176
184
# Add the payload to the operations field
177
- operations_str = json . dumps (payload )
185
+ operations_str = self . json_serialize (payload )
178
186
log .debug ("operations %s" , operations_str )
179
187
180
188
# Generate the file map
@@ -188,7 +196,7 @@ def execute( # type: ignore
188
196
file_streams = {str (i ): files [path ] for i , path in enumerate (files )}
189
197
190
198
# Add the file map field
191
- file_map_str = json . dumps (file_map )
199
+ file_map_str = self . json_serialize (file_map )
192
200
log .debug ("file_map %s" , file_map_str )
193
201
194
202
fields = {"operations" : operations_str , "map" : file_map_str }
@@ -224,7 +232,7 @@ def execute( # type: ignore
224
232
225
233
# Log the payload
226
234
if log .isEnabledFor (logging .INFO ):
227
- log .info (">>> %s" , json . dumps (payload ))
235
+ log .info (">>> %s" , self . json_serialize (payload ))
228
236
229
237
# Pass kwargs to requests post method
230
238
post_args .update (self .kwargs )
@@ -257,7 +265,10 @@ def raise_response_error(resp: requests.Response, reason: str):
257
265
)
258
266
259
267
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 )
261
272
262
273
if log .isEnabledFor (logging .INFO ):
263
274
log .info ("<<< %s" , response .text )
@@ -396,7 +407,7 @@ def _build_batch_post_args(
396
407
397
408
# Log the payload
398
409
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 ]))
400
411
401
412
# Pass kwargs to requests post method
402
413
post_args .update (self .kwargs )
0 commit comments