Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 6 additions & 3 deletions luno_python/base_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -70,10 +70,13 @@ def do(self, method, path, req=None, auth=False):
:type req: object
:type auth: bool
"""
try:
params = json.loads(json.dumps(req))
except Exception:
if req is None:
params = None
else:
try:
params = json.loads(json.dumps(req))
except TypeError as e:
raise TypeError("luno: request parameters must be JSON-serializable: %s" % str(e)) from e
headers = {"User-Agent": self.make_user_agent()}
args = dict(timeout=self.timeout, params=params, headers=headers)
if auth:
Expand Down
28 changes: 28 additions & 0 deletions tests/test_client.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
from decimal import Decimal

import pytest
import requests
import requests_mock
Expand Down Expand Up @@ -262,3 +264,29 @@ def test_get_balances_with_malformed_response():
# Test without account_id on malformed response
result = c.get_balances()
assert result == MOCK_MALFORMED_RESPONSE


def test_client_do_with_non_serializable_params():
"""Test that non-JSON-serializable parameters raise a clear error."""
c = Client()
c.set_base_url("mock://test/")

# Test with Decimal (not JSON-serializable)
with pytest.raises(TypeError) as exc_info:
c.do("GET", "/", req={"amount": Decimal("10.5")})
assert "JSON-serializable" in str(exc_info.value)

# Test with custom object (not JSON-serializable)
class CustomObject:
pass

with pytest.raises(TypeError) as exc_info:
c.do("GET", "/", req={"obj": CustomObject()})
assert "JSON-serializable" in str(exc_info.value)

# Test with None request (should not raise)
adapter = requests_mock.Adapter()
c.session.mount("mock", adapter)
adapter.register_uri("GET", "mock://test/", json={"result": "ok"})
result = c.do("GET", "/", req=None)
assert result["result"] == "ok"