Skip to content

json serialization - bug fixed #85

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Jun 23, 2022
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
8 changes: 7 additions & 1 deletion sift/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
See: https://siftscience.com/docs/references/events-api
"""

import decimal
import json
import requests
import requests.auth
Expand All @@ -26,6 +27,11 @@ def _quote_path(s):
# optional arg to override this
return urllib.parse.quote(s, '')

class DecimalEncoder(json.JSONEncoder):
def default(self, o):
if isinstance(o, decimal.Decimal):
return (str(o),)
return super(DecimalEncoder, self).default(o)

class Client(object):

Expand Down Expand Up @@ -165,7 +171,7 @@ def track(
try:
response = self.session.post(
path,
data=json.dumps(properties),
data=json.dumps(properties, cls=DecimalEncoder),
headers=headers,
timeout=timeout,
params=params)
Expand Down
3 changes: 2 additions & 1 deletion tests/test_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import sys
import unittest
import warnings
from decimal import Decimal

import mock
import requests.exceptions
Expand All @@ -19,7 +20,7 @@ def valid_transaction_properties():
return {
'$buyer_user_id': '123456',
'$seller_user_id': '654321',
'$amount': 1253200,
'$amount': Decimal('1253200.0'),
'$currency_code': 'USD',
'$time': int(datetime.datetime.now().strftime('%s')),
'$transaction_id': 'my_transaction_id',
Expand Down
3 changes: 2 additions & 1 deletion tests/test_client_v203.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import datetime
from decimal import Decimal
import warnings
import json
import mock
Expand All @@ -16,7 +17,7 @@ def valid_transaction_properties():
return {
'$buyer_user_id': '123456',
'$seller_user_id': '654321',
'$amount': 1253200,
'$amount': Decimal('1253200.0'),
'$currency_code': 'USD',
'$time': int(datetime.datetime.now().strftime('%s')),
'$transaction_id': 'my_transaction_id',
Expand Down