@@ -163,13 +163,17 @@ def _format_request_data(
163
163
path : str ,
164
164
data : Optional [Dict [str , Any ]],
165
165
params : Optional [Dict [str , Any ]],
166
+ http_method : str ,
166
167
) -> Tuple [str , str , Optional [Dict [str , Any ]]]:
167
168
if path .startswith (f"{ self .api_endpoint } /{ self .api_version } " ):
168
169
url = path
169
170
else :
170
171
url = f"{ self .api_endpoint } /{ self .api_version } /{ path } "
171
172
172
173
payload = ""
174
+
175
+ data , params = self ._get_testmode (data , params , http_method )
176
+
173
177
if data is not None :
174
178
try :
175
179
payload = json .dumps (data )
@@ -178,10 +182,6 @@ def _format_request_data(
178
182
179
183
if params is None :
180
184
params = {}
181
- if self .testmode and "testmode" not in params :
182
- if not (self .api_key .startswith ("access_" ) or hasattr (self , "_oauth_client" )):
183
- raise RequestSetupError ("Configuring testmode only works with access_token or OAuth authorization" )
184
- params ["testmode" ] = "true"
185
185
186
186
querystring = generate_querystring (params )
187
187
if querystring :
@@ -190,6 +190,24 @@ def _format_request_data(
190
190
191
191
return url , payload , params
192
192
193
+ def _get_testmode (self , data , params , http_method ):
194
+ if self .testmode or (params and "testmode" in params ):
195
+ if not (self .api_key .startswith ("access_" ) or hasattr (self , "_oauth_client" )):
196
+ raise RequestSetupError ("Configuring testmode only works with access_token or OAuth authorization" )
197
+
198
+ # Add to params if we're dealing with a GET request, for any other request add to data.
199
+ # If testmode is passed in the params, we're always overriding self.testmode. If
200
+ # self.testmode is True, simply pass in "true".
201
+ if http_method == "GET" :
202
+ params ["testmode" ] = params .get ("testmode" ) or "true"
203
+ elif not data or "testmode" not in data :
204
+ data ["testmode" ] = params .get ("testmode" ) or "true"
205
+
206
+ # Delete from the params since it's not a valid parameter when the request is not GET
207
+ params .pop ("testmode" , None )
208
+
209
+ return data , params
210
+
193
211
def _perform_http_call_apikey (
194
212
self ,
195
213
http_method : str ,
@@ -206,7 +224,7 @@ def _perform_http_call_apikey(
206
224
self ._client .verify = True
207
225
self ._setup_retry ()
208
226
209
- url , payload , params = self ._format_request_data (path , data , params )
227
+ url , payload , params = self ._format_request_data (path , data , params , http_method )
210
228
try :
211
229
headers = {
212
230
"Accept" : "application/json" ,
@@ -240,7 +258,7 @@ def _perform_http_call_oauth(
240
258
params : Optional [Dict [str , Any ]] = None ,
241
259
idempotency_key : str = "" ,
242
260
) -> requests .Response :
243
- url , payload , params = self ._format_request_data (path , data , params )
261
+ url , payload , params = self ._format_request_data (path , data , params , http_method )
244
262
try :
245
263
headers = {
246
264
"Accept" : "application/json" ,
0 commit comments