Small changes to make it work with the Server

This commit is contained in:
Eibriel
2015-03-27 15:34:35 +01:00
parent 8deaed5fee
commit 59dda5777e
4 changed files with 57 additions and 20 deletions

View File

@@ -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__

View File

@@ -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

18
attractsdk/nodes.py Executable file
View File

@@ -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"

View File

@@ -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()