Huge refactoring done in many places, bit of redesign of API (can break
authorMarek Marecki <triviuss@gmail.com>
Sun, 7 Jul 2013 14:11:30 +0000 (16:11 +0200)
committerMarek Marecki <triviuss@gmail.com>
Sun, 7 Jul 2013 14:11:30 +0000 (16:11 +0200)
stuff)

diaspy/client.py
diaspy/conversations.py
diaspy/errors.py
diaspy/models.py
diaspy/streams.py

index ce07990a70cf33be9fbcfaf94e89de21b47d9a44..a4540f74cc198677696209095d6bda60c8d959e2 100644 (file)
@@ -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):
index b1560add8411c0d8163f0b78ba8d057d3018e2c7..03511065759a10021bfbddb01de0fe3f504e5264 100644 (file)
@@ -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:
index b411ad3a27231b0b11cbdc4d77fe478b37910eb4..0620373964ddc5ebf28fb5877e3c505fb8efb995 100644 (file)
@@ -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
index 8bbf86b9c7c19857aa3259bd3aba007736771d07..3cbe814a958a16301908cd0ab9f56b09bd96835c 100644 (file)
@@ -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)
index 2a3c233750067df39f845dad204cbcfb398275fc..adc42eb0114986a72285b3bc7466f78fb3b9d2b1 100644 (file)
@@ -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)