Skip to content

Commit fd68efd

Browse files
Merge pull request #355 from rubenhesselink/add-testmode-to-body-instead-of-query-param
Make sure testmode is added to the body on all http_methods except GET
2 parents bbb8448 + a61f68e commit fd68efd

File tree

2 files changed

+53
-14
lines changed

2 files changed

+53
-14
lines changed

mollie/api/client.py

Lines changed: 34 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -159,33 +159,53 @@ def _format_request_data(
159159
path: str,
160160
data: Optional[Dict[str, Any]],
161161
params: Optional[Dict[str, Any]],
162+
http_method: str,
162163
) -> Tuple[str, str, Optional[Dict[str, Any]]]:
163164
if path.startswith(f"{self.api_endpoint}/{self.api_version}"):
164165
url = path
165166
else:
166167
url = f"{self.api_endpoint}/{self.api_version}/{path}"
167168

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+
168178
payload = ""
169179
if data is not None:
170180
try:
171181
payload = json.dumps(data)
172182
except TypeError as err:
173183
raise RequestSetupError(f"Error encoding data into JSON: {err}.")
174184

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-
187185
return url, payload, params
188186

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+
189209
def _perform_http_call_apikey(
190210
self,
191211
http_method: str,
@@ -202,7 +222,7 @@ def _perform_http_call_apikey(
202222
self._client.verify = True
203223
self._setup_retry()
204224

205-
url, payload, params = self._format_request_data(path, data, params)
225+
url, payload, params = self._format_request_data(path, data, params, http_method)
206226
try:
207227
headers = {
208228
"Accept": "application/json",
@@ -236,7 +256,7 @@ def _perform_http_call_oauth(
236256
params: Optional[Dict[str, Any]] = None,
237257
idempotency_key: str = "",
238258
) -> 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)
240260
try:
241261
headers = {
242262
"Accept": "application/json",

tests/test_api_client.py

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -530,3 +530,22 @@ 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+
"params, data, http_method, testmode, expected_return_value",
537+
[
538+
({}, {}, "GET", True, ({"testmode": "true"}, {})),
539+
({}, {}, "GET", False, ({}, {})),
540+
({}, {}, "POST", True, ({}, {"testmode": "true"})),
541+
({"testmode": "true"}, {}, "POST", True, ({}, {"testmode": "true"})),
542+
({}, {"testmode": "true"}, "POST", True, ({}, {"testmode": "true"})),
543+
({}, {}, "POST", False, ({}, {})),
544+
({"testmode": "true"}, {}, "POST", False, ({}, {"testmode": "true"})),
545+
({}, {"testmode": "true"}, "POST", False, ({}, {"testmode": "true"})),
546+
],
547+
)
548+
def test_build_request_params_and_data(oauth_client, params, data, http_method, testmode, expected_return_value):
549+
oauth_client.testmode = testmode
550+
return_value = oauth_client._build_request_params_and_data(params, data, http_method)
551+
assert return_value == expected_return_value

0 commit comments

Comments
 (0)