Introducing find_one method for Node resources

This commit is contained in:
2015-10-15 16:11:19 +02:00
parent 5a856c8972
commit 90f8922667

View File

@@ -31,6 +31,25 @@ class Node(List, Find, Create, Post, Update, Delete, Replace):
url = utils.join_url_params(url, params)
return cls(api.get(url))
@classmethod
def find_one(cls, params, api=None):
"""Get one resource starting from parameters different than the resource
id. TODO if more than one match for the query is found, raise exception.
"""
api = api or Api.Default()
# Force delivery of only 1 result
params['max_results'] = 1
url = utils.join_url_params(cls.path, params)
response = api.get(url)
# Keep the response a dictionary, and cast it later into an object.
if response['_items']:
return cls(response['_items'][0])
else:
return None
def update(self, attributes=None, api=None):
api = api or self.api
attributes = attributes or self.to_dict()