Skip to content

Commit a3ea8b5

Browse files
authored
Merge pull request #361 from mollie/revert-355-add-testmode-to-body-instead-of-query-param
Revert "Make sure testmode is added to the body on all http_methods except GET"
2 parents 30359a0 + a4cb430 commit a3ea8b5

File tree

2 files changed

+14
-53
lines changed

2 files changed

+14
-53
lines changed

mollie/api/client.py

Lines changed: 14 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -163,52 +163,32 @@ def _format_request_data(
163163
path: str,
164164
data: Optional[Dict[str, Any]],
165165
params: Optional[Dict[str, Any]],
166-
http_method: str,
167166
) -> Tuple[str, str, Optional[Dict[str, Any]]]:
168167
if path.startswith(f"{self.api_endpoint}/{self.api_version}"):
169168
url = path
170169
else:
171170
url = f"{self.api_endpoint}/{self.api_version}/{path}"
172171

173-
params = params or {}
174-
data = data or {}
175-
params, data = self._build_request_params_and_data(params, data, http_method)
176-
177-
querystring = generate_querystring(params)
178-
if querystring:
179-
url += "?" + querystring
180-
params = None
181-
182172
payload = ""
183173
if data is not None:
184174
try:
185175
payload = json.dumps(data)
186176
except TypeError as err:
187177
raise RequestSetupError(f"Error encoding data into JSON: {err}.")
188178

189-
return url, payload, params
179+
if params is None:
180+
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"
190185

191-
def _is_valid_testmode(self):
192-
"""Check if the testmode can be configured."""
193-
if not self.api_key.startswith("access_") and not hasattr(self, "_oauth_client"):
194-
raise RequestSetupError("Configuring testmode only works with access_token or OAuth authorization")
195-
196-
def _build_request_params_and_data(
197-
self, params: Dict[str, Any], data: Dict[str, Any], http_method: str
198-
) -> Tuple[Dict[str, Any], Dict[str, Any]]:
199-
if http_method == "GET":
200-
if self.testmode and "testmode" not in params:
201-
self._is_valid_testmode()
202-
params["testmode"] = "true"
203-
else:
204-
if self.testmode and "testmode" not in data:
205-
self._is_valid_testmode()
206-
data["testmode"] = "true"
207-
if "testmode" in params:
208-
self._is_valid_testmode()
209-
testmode = params.pop("testmode")
210-
data["testmode"] = testmode
211-
return params, data
186+
querystring = generate_querystring(params)
187+
if querystring:
188+
url += "?" + querystring
189+
params = None
190+
191+
return url, payload, params
212192

213193
def _perform_http_call_apikey(
214194
self,
@@ -226,7 +206,7 @@ def _perform_http_call_apikey(
226206
self._client.verify = True
227207
self._setup_retry()
228208

229-
url, payload, params = self._format_request_data(path, data, params, http_method)
209+
url, payload, params = self._format_request_data(path, data, params)
230210
try:
231211
headers = {
232212
"Accept": "application/json",
@@ -260,7 +240,7 @@ def _perform_http_call_oauth(
260240
params: Optional[Dict[str, Any]] = None,
261241
idempotency_key: str = "",
262242
) -> requests.Response:
263-
url, payload, params = self._format_request_data(path, data, params, http_method)
243+
url, payload, params = self._format_request_data(path, data, params)
264244
try:
265245
headers = {
266246
"Accept": "application/json",

tests/test_api_client.py

Lines changed: 0 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -530,22 +530,3 @@ 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)