diff --git a/pillarsdk/nodes.py b/pillarsdk/nodes.py index 5de23d7..9ec15be 100755 --- a/pillarsdk/nodes.py +++ b/pillarsdk/nodes.py @@ -30,7 +30,9 @@ class Node(List, Find, Create, Post, Update, Delete, Replace): url = utils.join_url(cls.path, str(resource_id)) if params: url = utils.join_url_params(url, params) - return cls(api.get(url)) + + item = utils.convert_datetime(api.get(url)) + return cls(item) @classmethod def find_one(cls, params, api=None): @@ -46,7 +48,8 @@ class Node(List, Find, Create, Post, Update, Delete, Replace): response = api.get(url) # Keep the response a dictionary, and cast it later into an object. if response['_items']: - return cls(response['_items'][0]) + item = utils.convert_datetime(response['_items'][0]) + return cls(item) else: raise ResourceNotFound(response) diff --git a/pillarsdk/resource.py b/pillarsdk/resource.py index 297991c..3247ced 100644 --- a/pillarsdk/resource.py +++ b/pillarsdk/resource.py @@ -1,4 +1,5 @@ import uuid +from datetime import datetime from . import utils from .api import Api @@ -112,7 +113,8 @@ class Find(Resource): api = api or Api.Default() url = utils.join_url(cls.path, str(resource_id)) - return cls(api.get(url)) + item = utils.convert_datetime(api.get(url)) + return cls(item) @classmethod def find_first(cls, params, api=None): @@ -135,7 +137,7 @@ class Find(Resource): response = api.get(url) res = cls(response) if res._items: - return res._items[0] + return utils.convert_datetime(res._items[0]) else: return None @@ -153,7 +155,7 @@ class Find(Resource): response = api.get(url) # Keep the response a dictionary, and cast it later into an object. if response['_items']: - return cls(response['_items'][0]) + return cls(utils.convert_datetime(response['_items'][0])) else: raise ResourceNotFound(response) @@ -183,6 +185,8 @@ class List(Resource): try: response = api.get(url) + for item in response['_items']: + item = utils.convert_datetime(item) return cls.list_class(response) except AttributeError: # To handle the case when response is JSON Array diff --git a/pillarsdk/utils.py b/pillarsdk/utils.py index cb3e980..9beefc5 100644 --- a/pillarsdk/utils.py +++ b/pillarsdk/utils.py @@ -1,4 +1,5 @@ import re +from datetime import datetime try: from urllib.parse import urlencode @@ -44,3 +45,15 @@ def merge_dict(data, *override): for current_dict in (data,) + override: result.update(current_dict) return result + + +def convert_datetime(item): + """Starting from an JSON object, find and replace the _create and _updated + keys with actual datetime objects. + """ + keys = ['_updated', '_created'] + + for k in keys: + item[k] = datetime.strptime(item[k], "%a, %d %b %Y %H:%M:%S %Z") + + return item