Skip to content

Commit 1ddd23e

Browse files
committed
Adding custom JSONEncoder to support Resource.
1 parent faf6ddf commit 1ddd23e

File tree

1 file changed

+12
-1
lines changed

1 file changed

+12
-1
lines changed

intercom/__init__.py

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,7 @@
1+
# -*- coding: utf-8 -*-
2+
13
from datetime import datetime
4+
from json import JSONEncoder
25
from .errors import ArgumentError
36
from .lib.setter_property import SetterProperty
47

@@ -55,7 +58,7 @@ def send_request_to_path(cls, method, path, params=None):
5558
}
5659
if method in ('POST', 'PUT', 'DELETE'):
5760
headers['content-type'] = 'application/json'
58-
req_params['data'] = json.dumps(params)
61+
req_params['data'] = json.dumps(params, cls=ResourceEncoder)
5962
elif method == 'GET':
6063
req_params['params'] = params
6164
req_params['headers'] = headers
@@ -180,3 +183,11 @@ def endpoints(cls, value):
180183
@SetterProperty
181184
def endpoint(cls, value):
182185
cls.endpoints = [value]
186+
187+
188+
class ResourceEncoder(JSONEncoder):
189+
def default(self, o):
190+
if hasattr(o, 'from_api'):
191+
# handle API resources
192+
return o.attributes
193+
return super(ResourceEncoder, self).default(o)

0 commit comments

Comments
 (0)