Add option to get a valid json dump.

Alternative approach to #1
json_dump
Raphaël Vinot 2015-04-17 10:48:55 +02:00
parent 8978457a67
commit 4b5b74aa76
2 changed files with 19 additions and 4 deletions

View File

@ -3,8 +3,19 @@
import requests
import urlparse
from datetime import datetime
from datetime import datetime, date
import os
import json
class DatetimeEncoder(json.JSONEncoder):
def default(self, obj):
if isinstance(obj, datetime):
return obj.strftime('%Y-%m-%d %H:%M:%S')
elif isinstance(obj, date):
return obj.strftime('%Y-%m-%d')
# Let the base class default method raise the TypeError
return json.JSONEncoder.default(self, obj)
class PyLevel2(object):
@ -17,7 +28,7 @@ class PyLevel2(object):
self.session = requests.Session()
self.session.headers.update({'content-type': 'application/json'})
def events(self, count=None, year=None, month=None):
def events(self, count=None, year=None, month=None, json_dump=False):
"""
Get the events.
"""
@ -55,12 +66,14 @@ class PyLevel2(object):
else:
# invalid query
pass
if json_dump:
return json.dumps(to_return, cls=DatetimeEncoder)
return to_return
else:
# Something bad happened
pass
def spaceapi(self):
def spaceapi(self, json_dump=False):
"""
Gives information about Level2.
"""
@ -73,6 +86,8 @@ class PyLevel2(object):
else:
# invalid query
pass
if json_dump:
return json.dumps(data, cls=DatetimeEncoder)
return data
else:
# Something bad happened

View File

@ -4,7 +4,7 @@ from setuptools import setup
setup(
name='pylevel2',
version='1.0',
version='1.1',
author='Raphaël Vinot',
author_email='raphael.vinot@gmail.com',
maintainer='Raphaël Vinot',