@@ -159,33 +159,53 @@ def _format_request_data(
159
159
path : str ,
160
160
data : Optional [Dict [str , Any ]],
161
161
params : Optional [Dict [str , Any ]],
162
+ http_method : str ,
162
163
) -> Tuple [str , str , Optional [Dict [str , Any ]]]:
163
164
if path .startswith (f"{ self .api_endpoint } /{ self .api_version } " ):
164
165
url = path
165
166
else :
166
167
url = f"{ self .api_endpoint } /{ self .api_version } /{ path } "
167
168
169
+ params = params or {}
170
+ data = data or {}
171
+ params , data = self ._build_request_params_and_data (params , data , http_method )
172
+
173
+ querystring = generate_querystring (params )
174
+ if querystring :
175
+ url += "?" + querystring
176
+ params = None
177
+
168
178
payload = ""
169
179
if data is not None :
170
180
try :
171
181
payload = json .dumps (data )
172
182
except TypeError as err :
173
183
raise RequestSetupError (f"Error encoding data into JSON: { err } ." )
174
184
175
- if params is None :
176
- params = {}
177
- if self .testmode and "testmode" not in params :
178
- if not (self .api_key .startswith ("access_" ) or hasattr (self , "_oauth_client" )):
179
- raise RequestSetupError ("Configuring testmode only works with access_token or OAuth authorization" )
180
- params ["testmode" ] = "true"
181
-
182
- querystring = generate_querystring (params )
183
- if querystring :
184
- url += "?" + querystring
185
- params = None
186
-
187
185
return url , payload , params
188
186
187
+ def _is_valid_testmode (self ):
188
+ """Check if the testmode can be configured."""
189
+ if not self .api_key .startswith ("access_" ) and not hasattr (self , "_oauth_client" ):
190
+ raise RequestSetupError ("Configuring testmode only works with access_token or OAuth authorization" )
191
+
192
+ def _build_request_params_and_data (
193
+ self , params : Dict [str , Any ], data : Dict [str , Any ], http_method : str
194
+ ) -> Tuple [Dict [str , Any ], Dict [str , Any ]]:
195
+ if http_method == "GET" :
196
+ if self .testmode and "testmode" not in params :
197
+ self ._is_valid_testmode ()
198
+ params ["testmode" ] = "true"
199
+ else :
200
+ if self .testmode and "testmode" not in data :
201
+ self ._is_valid_testmode ()
202
+ data ["testmode" ] = "true"
203
+ if "testmode" in params :
204
+ self ._is_valid_testmode ()
205
+ testmode = params .pop ("testmode" )
206
+ data ["testmode" ] = testmode
207
+ return params , data
208
+
189
209
def _perform_http_call_apikey (
190
210
self ,
191
211
http_method : str ,
@@ -202,7 +222,7 @@ def _perform_http_call_apikey(
202
222
self ._client .verify = True
203
223
self ._setup_retry ()
204
224
205
- url , payload , params = self ._format_request_data (path , data , params )
225
+ url , payload , params = self ._format_request_data (path , data , params , http_method )
206
226
try :
207
227
headers = {
208
228
"Accept" : "application/json" ,
@@ -236,7 +256,7 @@ def _perform_http_call_oauth(
236
256
params : Optional [Dict [str , Any ]] = None ,
237
257
idempotency_key : str = "" ,
238
258
) -> requests .Response :
239
- url , payload , params = self ._format_request_data (path , data , params )
259
+ url , payload , params = self ._format_request_data (path , data , params , http_method )
240
260
try :
241
261
headers = {
242
262
"Accept" : "application/json" ,
0 commit comments