From 59dda5777ee13a709fc311e2c697e992036a83af Mon Sep 17 00:00:00 2001 From: Eibriel Date: Fri, 27 Mar 2015 15:34:35 +0100 Subject: [PATCH] Small changes to make it work with the Server --- attractsdk/__init__.py | 2 ++ attractsdk/api.py | 19 +++++++++++-------- attractsdk/nodes.py | 18 ++++++++++++++++++ attractsdk/resource.py | 38 ++++++++++++++++++++++++++------------ 4 files changed, 57 insertions(+), 20 deletions(-) create mode 100755 attractsdk/nodes.py diff --git a/attractsdk/__init__.py b/attractsdk/__init__.py index c0fdb4b..dec0c4c 100644 --- a/attractsdk/__init__.py +++ b/attractsdk/__init__.py @@ -1,3 +1,5 @@ from attractsdk.api import Api +from attractsdk.nodes import Node +from attractsdk.nodes import NodeType from attractsdk.exceptions import ResourceNotFound, UnauthorizedAccess, MissingConfig from attractsdk.config import __version__, __pypi_packagename__ diff --git a/attractsdk/api.py b/attractsdk/api.py index 66af7f4..f55a557 100644 --- a/attractsdk/api.py +++ b/attractsdk/api.py @@ -56,18 +56,21 @@ class Api(object): return Api._api_singleton - def basic_auth(self): - """Returns base64 encoded token. Used to encode auth credentials + def basic_auth(self, token=None): + """Returns base64 encoded token. Used to encode credentials for retrieving the token. """ - credentials = "{0}:{1}".format(self.username, self.password) + if token: + credentials = "{0}:0".format(token['token']) + else: + credentials = "%s:%s" % (self.username, self.password) return base64.b64encode(credentials.encode('utf-8')).decode('utf-8').replace("\n", "") def get_token(self): """Generate new token by making a POST request """ - path = "/users" - payload = None + path = "/tokens" + payload = {'username': self.username} if self.token: return self.token @@ -167,19 +170,19 @@ class Api(object): """Make POST request """ return self.request(utils.join_url(self.endpoint, action), 'POST', - body=params or {}, headers=headers or {}, refresh_token=refresh_token) + body=params or {}, headers=headers or {}) def put(self, action, params=None, headers=None): """Make PUT request """ return self.request(utils.join_url(self.endpoint, action), 'PUT', - body=params or {}, headers=headers or {}, refresh_token=refresh_token) + body=params or {}, headers=headers or {}) def patch(self, action, params=None, headers=None): """Make PATCH request """ return self.request(utils.join_url(self.endpoint, action), 'PATCH', - body=params or {}, headers=headers or {}, refresh_token=refresh_token) + body=params or {}, headers=headers or {}) def delete(self, action, headers=None): """Make DELETE request diff --git a/attractsdk/nodes.py b/attractsdk/nodes.py new file mode 100755 index 0000000..30bb31b --- /dev/null +++ b/attractsdk/nodes.py @@ -0,0 +1,18 @@ +from resource import List +from resource import Find +from resource import Create +from resource import Post +from resource import Update +from resource import Delete + + +class Node(List, Find, Create, Post, Update, Delete): + """Node class wrapping the REST nodes endpoint + """ + path = "nodes" + + +class NodeType(List, Find, Create, Post, Delete): + """NodeType class wrapping the REST node_types endpoint + """ + path = "node_types" diff --git a/attractsdk/resource.py b/attractsdk/resource.py index f09a567..b10ba22 100644 --- a/attractsdk/resource.py +++ b/attractsdk/resource.py @@ -64,7 +64,7 @@ class Resource(object): """ if isinstance(value, dict): cls = self.convert_resources.get(name, Resource) - return cls(value, api=self.api) + return cls(value) elif isinstance(value, list): new_list = [] for obj in value: @@ -173,8 +173,19 @@ class Update(Resource): def update(self, attributes=None): attributes = attributes or self.to_dict() - url = utils.join_url(self.path, str(self['id'])) - new_attributes = self.api.put(url, attributes, self.http_headers()) + etag = attributes['_etag'] + attributes.pop('_id') + attributes.pop('_etag') + attributes.pop('_created') + attributes.pop('_updated') + attributes.pop('_links') + if 'parent' in attributes: + attributes.pop('parent') + url = utils.join_url(self.path, str(self['_id'])) + headers = utils.merge_dict( + self.http_headers(), + {'If-Match': str(etag)}) + new_attributes = self.api.put(url, attributes, headers) self.error = None self.merge(new_attributes) return self.success() @@ -209,8 +220,10 @@ class Delete(Resource): >>> node = Node.find("507f1f77bcf86cd799439011") >>> node.delete() """ - url = utils.join_url(self.path, str(self['id'])) - new_attributes = self.api.delete(url) + url = utils.join_url(self.path, str(self['_id'])) + etag = self['_etag'] + headers = {'If-Match': str(etag)} + new_attributes = self.api.delete(url, headers) self.error = None self.merge(new_attributes) return self.success() @@ -218,18 +231,19 @@ class Delete(Resource): class Post(Resource): - def post(self, name, attributes=None, cls=Resource, fieldname='id'): + def post(self, attributes=None, cls=Resource, fieldname='id'): """Constructs url with passed in headers and makes post request via post method in api class. """ attributes = attributes or {} - url = utils.join_url(self.path, str(self[fieldname]), name) - if not isinstance(attributes, Resource): - attributes = Resource(attributes, api=self.api) - new_attributes = self.api.post(url, attributes.to_dict(), attributes.http_headers()) - if isinstance(cls, Resource): + url = utils.join_url(self.path) + """if not isinstance(attributes, Resource): + attributes = Resource(attributes, api=self.api)""" + new_attributes = self.api.post(url, attributes, {}) + """if isinstance(cls, Resource): cls.error = None cls.merge(new_attributes) return self.success() else: - return cls(new_attributes, api=self.api) + return cls(new_attributes, api=self.api)""" + return self.success()