Skip to content
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

Feature/date objects #27

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
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
11 changes: 11 additions & 0 deletions sunlight/service.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
:class:`sunlight.services.openstates.OpenStates`) inherit from this.
"""
import sys
import datetime

import sunlight.config
import sunlight.errors
Expand Down Expand Up @@ -111,3 +112,13 @@ class EntityDict(dict):
def __init__(self, data={}, meta=None):
dict.__init__(self, data)
self._meta = meta


def datetime_parser(dct):
for k, v in dct.items():
for dte_fmt in ("%Y-%m-%d", "%Y-%m-%dT%H:%M:%SZ"):
try:
dct[k] = datetime.datetime.strptime(v, dte_fmt)
except Exception:
pass
return dct
3 changes: 2 additions & 1 deletion sunlight/services/capitolwords.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@


from sunlight.errors import InvalidRequestException, BadRequestException
from sunlight.service import datetime_parser

import sunlight.service
import json
Expand Down Expand Up @@ -104,7 +105,7 @@ def _get_url(self, pathparts, apikey, **kwargs):
return ret

def _decode_response(self, response):
ret = json.loads(response)
ret = json.loads(response, object_hook=datetime_parser)
if "error" in ret:
ex = InvalidRequestException(ret['error'])
ex.response = ret
Expand Down
4 changes: 2 additions & 2 deletions sunlight/services/congress.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
"""

import sunlight.service
from sunlight.service import EntityDict, EntityList
from sunlight.service import EntityDict, EntityList, datetime_parser
from sunlight.pagination import pageable
from sunlight.errors import SunlightException
import json
Expand Down Expand Up @@ -342,7 +342,7 @@ def _get_url(self, pathparts, apikey, **kwargs):

def _decode_response(self, response):
try:
data = json.loads(response)
data = json.loads(response, object_hook=datetime_parser)
except Exception:
raise SunlightException('Error parsing response! Something must be wrong with our Congress... API')
results = data.pop('results', None)
Expand Down
4 changes: 2 additions & 2 deletions sunlight/services/opencivic.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@

import sunlight.service
from sunlight.errors import BadRequestException
from sunlight.service import EntityDict
from sunlight.service import EntityDict, datetime_parser
import json

module_name = "opencivic"
Expand Down Expand Up @@ -77,4 +77,4 @@ def _get_url(self, objs, apikey, **kwargs):
)

def _decode_response(self, response):
return json.loads(response)
return json.loads(response, object_hook=datetime_parser)
3 changes: 2 additions & 1 deletion sunlight/services/openstates.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
# of the LICENSE file.

import sunlight.service
from sunlight.service import datetime_parser
from sunlight.errors import BadRequestException
import json

Expand Down Expand Up @@ -196,4 +197,4 @@ def _get_url(self, objs, apikey, **kwargs):
)

def _decode_response(self, response):
return json.loads(response)
return json.loads(response, object_hook=datetime_parser)