From 78cc478af9e64662ea0eb0971bf43217d49858c3 Mon Sep 17 00:00:00 2001 From: Marek Marecki Date: Sun, 7 Jul 2013 16:11:30 +0200 Subject: [PATCH] Huge refactoring done in many places, bit of redesign of API (can break stuff) --- diaspy/client.py | 2 +- diaspy/conversations.py | 2 +- diaspy/errors.py | 6 ++++++ diaspy/models.py | 11 ++++++++--- diaspy/streams.py | 20 ++++++++++---------- 5 files changed, 26 insertions(+), 15 deletions(-) diff --git a/diaspy/client.py b/diaspy/client.py index ce07990..a4540f7 100644 --- a/diaspy/client.py +++ b/diaspy/client.py @@ -106,7 +106,7 @@ class Client: raise Exception('wrong status code: {0}'.format(r.status_code)) mailbox = r.json() - return [diaspy.conversations.Conversation(str(conversation['conversation']['id']), self.connection) + return [diaspy.conversations.Conversation(self.connection, conversation['conversation']['id']) for conversation in mailbox] def add_aspect(self, aspect_name, visible=0): diff --git a/diaspy/conversations.py b/diaspy/conversations.py index b1560ad..0351106 100644 --- a/diaspy/conversations.py +++ b/diaspy/conversations.py @@ -25,7 +25,7 @@ class Conversation(): def _fetch(self): """Fetches JSON data representing conversation. """ - request = self._connection.get('conversations/{}.json'.format(self.conv_id)) + request = self._connection.get('conversations/{}.json'.format(self.id)) if request.status_code == 200: self.data = request.json()['conversation'] else: diff --git a/diaspy/errors.py b/diaspy/errors.py index b411ad3..0620373 100644 --- a/diaspy/errors.py +++ b/diaspy/errors.py @@ -40,6 +40,12 @@ class PostError(DiaspyError): pass +class StreamError(DiaspyError): + """Exception raised when something related to streams goes wrong. + """ + pass + + def react(r, message='', accepted=[200, 201, 202, 203, 204, 205, 206], exception=DiaspyError): """This method tries to decides how to react to a response code passed to it. If it's an diff --git a/diaspy/models.py b/diaspy/models.py index 8bbf86b..3cbe814 100644 --- a/diaspy/models.py +++ b/diaspy/models.py @@ -231,12 +231,17 @@ class Post(): def _fetch(self): """This function retrieves data of the post. """ - request = self._connection.get('posts/{0}.json'.format(self.post_id)) + request = self._connection.get('posts/{0}.json'.format(self.id)) if request.status_code != 200: raise errors.PostError('wrong status code: {0}'.format(request.status_code)) else: self.data = request.json() + def update(self): + """Updates post data. + """ + self._fetch() + def like(self): """This function likes a post. It abstracts the 'Like' functionality. @@ -245,7 +250,7 @@ class Post(): """ data = {'authenticity_token': repr(self._connection)} - request = self._connection.post('posts/{0}/likes'.format(self.post_id), + request = self._connection.post('posts/{0}/likes'.format(self.id), data=data, headers={'accept': 'application/json'}) @@ -260,7 +265,7 @@ class Post(): data = {'authenticity_token': self._connection.get_token()} request = self._connection.delete('posts/{0}/likes/{1}' - .format(self.post_id, + .format(self.id, self.data['interactions'] ['likes'][0]['id']), data=data) diff --git a/diaspy/streams.py b/diaspy/streams.py index 2a3c233..adc42eb 100644 --- a/diaspy/streams.py +++ b/diaspy/streams.py @@ -56,8 +56,8 @@ class Generic(): if max_time: params['max_time'] = max_time request = self._connection.get(self._location, params=params) if request.status_code != 200: - raise Exception('wrong status code: {0}'.format(request.status_code)) - return [Post(str(post['id']), self._connection) for post in request.json()] + raise error.StreamError('wrong status code: {0}'.format(request.status_code)) + return [Post(self._connection, post['id']) for post in request.json()] def _expand(self, new_stream): """Appends older posts to stream. @@ -73,11 +73,11 @@ class Generic(): def _update(self, new_stream): """Updates stream with new posts. """ - ids = [post.post_id for post in self._stream] + ids = [post.id for post in self._stream] stream = self._stream for i in range(len(new_stream)): - if new_stream[-i].post_id not in ids: + if new_stream[-i].id not in ids: stream = [new_stream[-i]] + stream ids.append(new_stream[-i].post_id) self._stream = stream @@ -94,7 +94,7 @@ class Generic(): for post in self._stream: deleted = False try: - post.get_data() + post.update() stream.append(post) except Exception: deleted = True @@ -133,8 +133,8 @@ class Outer(Generic): """ request = self._connection.get(self._location) if request.status_code != 200: - raise Exception('wrong status code: {0}'.format(request.status_code)) - return [Post(str(post['id']), self._connection) for post in request.json()] + raise error.StreamError('wrong status code: {0}'.format(request.status_code)) + return [Post(self._connection, post['id']) for post in request.json()] class Stream(Generic): @@ -169,11 +169,11 @@ class Stream(Generic): data=json.dumps(data), headers={'content-type': 'application/json', 'accept': 'application/json', - 'x-csrf-token': self._connection.get_token()}) + 'x-csrf-token': repr(self._connection)}) if request.status_code != 201: raise Exception('{0}: Post could not be posted.'.format(request.status_code)) - post = Post(str(request.json()['id']), self._connection) + post = Post(self._connection, request.json()['id']) return post def _photoupload(self, filename): @@ -197,7 +197,7 @@ class Stream(Generic): params['photo[aspect_ids][{0}]'.format(i)] = aspect['id'] headers = {'content-type': 'application/octet-stream', - 'x-csrf-token': self._connection.get_token(), + 'x-csrf-token': repr(self._connection), 'x-file-name': filename} request = self._connection.post('photos', data=image, params=params, headers=headers) -- 2.25.1