Skip to content

Commit b28da78

Browse files
Merge pull request #363 from mollie/342-testmode-as-querystring-and-body
Add testmode to data or params based on HTTP method
2 parents 69ccca6 + 530b862 commit b28da78

File tree

2 files changed

+40
-6
lines changed

2 files changed

+40
-6
lines changed

mollie/api/client.py

Lines changed: 24 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -163,13 +163,17 @@ def _format_request_data(
163163
path: str,
164164
data: Optional[Dict[str, Any]],
165165
params: Optional[Dict[str, Any]],
166+
http_method: str,
166167
) -> Tuple[str, str, Optional[Dict[str, Any]]]:
167168
if path.startswith(f"{self.api_endpoint}/{self.api_version}"):
168169
url = path
169170
else:
170171
url = f"{self.api_endpoint}/{self.api_version}/{path}"
171172

172173
payload = ""
174+
175+
data, params = self._get_testmode(data, params, http_method)
176+
173177
if data is not None:
174178
try:
175179
payload = json.dumps(data)
@@ -178,10 +182,6 @@ def _format_request_data(
178182

179183
if params is None:
180184
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"
185185

186186
querystring = generate_querystring(params)
187187
if querystring:
@@ -190,6 +190,24 @@ def _format_request_data(
190190

191191
return url, payload, params
192192

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+
193211
def _perform_http_call_apikey(
194212
self,
195213
http_method: str,
@@ -206,7 +224,7 @@ def _perform_http_call_apikey(
206224
self._client.verify = True
207225
self._setup_retry()
208226

209-
url, payload, params = self._format_request_data(path, data, params)
227+
url, payload, params = self._format_request_data(path, data, params, http_method)
210228
try:
211229
headers = {
212230
"Accept": "application/json",
@@ -240,7 +258,7 @@ def _perform_http_call_oauth(
240258
params: Optional[Dict[str, Any]] = None,
241259
idempotency_key: str = "",
242260
) -> 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)
244262
try:
245263
headers = {
246264
"Accept": "application/json",

tests/test_api_client.py

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -530,3 +530,19 @@ def test_update_customer_bad_request(client, response):
530530
)
531531

532532
assert exc.value.idempotency_key == "test_idempotency_key"
533+
534+
535+
@pytest.mark.parametrize(
536+
"testmode,params,http_method,expected",
537+
[
538+
(True, {}, "GET", ({}, {"testmode": "true"})),
539+
(True, {}, "POST", ({"testmode": "true"}, {})),
540+
(False, {"testmode": "true"}, "GET", ({}, {"testmode": "true"})),
541+
(False, {"testmode": "true"}, "POST", ({"testmode": "true"}, {})),
542+
(False, {"invalid": "something"}, "POST", ({}, {"invalid": "something"})),
543+
],
544+
)
545+
def test__get_testmode_sets_data_or_params_correctly(oauth_client, testmode, params, http_method, expected):
546+
oauth_client.testmode = testmode
547+
return_value = oauth_client._get_testmode({}, params, http_method)
548+
assert return_value == expected

0 commit comments

Comments
 (0)