Skip to content
This repository has been archived by the owner on Feb 21, 2022. It is now read-only.

Commit

Permalink
Add JSON serializer for datetime.
Browse files Browse the repository at this point in the history
  • Loading branch information
lefcha committed Jun 2, 2015
1 parent 4646873 commit 501fff5
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 3 deletions.
5 changes: 4 additions & 1 deletion tests/test_api.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
import time
import datetime
import os

import todoist


Expand Down Expand Up @@ -276,7 +278,8 @@ def test_item(api_token):
assert 'UpdatedItem1' in [p['content'] for p in api.state['Items']]
assert api.items.get_by_id(item1['id']) == item1

item2 = api.items.add('Item2', inbox['id'])
date_string = datetime.datetime(2038, 01, 19, 03, 14, 07)
item2 = api.items.add('Item2', inbox['id'], date_string=date_string)
api.commit()
api.items.sync()

Expand Down
11 changes: 9 additions & 2 deletions todoist/api.py
Original file line number Diff line number Diff line change
Expand Up @@ -280,6 +280,11 @@ def _post(self, call, url=None, **kwargs):
except ValueError:
return response.text

def _json_serializer(self, obj):
import datetime
if isinstance(obj, datetime.datetime):
return obj.isoformat()

# Sync
def generate_uuid(self):
"""
Expand All @@ -294,7 +299,8 @@ def sync(self, commands=None, **kwargs):
"""
data = {
'token': self.token,
'commands': json.dumps(commands or [], separators=',:'),
'commands': json.dumps(commands or [], separators=',:',
default=self._json_serializer),
'day_orders_timestamp': self.state['DayOrdersTimestamp'],
}
if not commands:
Expand Down Expand Up @@ -394,7 +400,8 @@ def query(self, queries, **kwargs):
"""
Performs date queries and other searches, and returns the results.
"""
params = {'queries': json.dumps(queries, separators=',:'),
params = {'queries': json.dumps(queries, separators=',:',
default=self._json_serializer),
'token': self.token}
params.update(kwargs)
return self._get('query', params=params)
Expand Down

0 comments on commit 501fff5

Please sign in to comment.