Skip to content

Commit

Permalink
Merge pull request #85 from jyothish6190/json-serialization-msue-67
Browse files Browse the repository at this point in the history
json serialization - bug fixed
  • Loading branch information
bho-sift authored Jun 23, 2022
2 parents 03e404e + c33e403 commit f33daea
Show file tree
Hide file tree
Showing 3 changed files with 11 additions and 3 deletions.
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

0 comments on commit f33daea

Please sign in to comment.