Refactored `Post()` to use `repr()` on `Connection()`,
authorMarek Marecki <triviuss@gmail.com>
Sun, 7 Jul 2013 13:35:30 +0000 (15:35 +0200)
committerMarek Marecki <triviuss@gmail.com>
Sun, 7 Jul 2013 13:35:30 +0000 (15:35 +0200)
refactored internals and outer API

diaspy/errors.py
diaspy/models.py

index 43888cfe90e08e57deee3d3961543cca4633ecd4..b411ad3a27231b0b11cbdc4d77fe478b37910eb4 100644 (file)
@@ -34,6 +34,12 @@ class AspectError(DiaspyError):
     pass
 
 
+class PostError(DiaspyError):
+    """Exception raised when something related to posts 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 b876c212f5276b1da050a8740267d0aa947b6e98..8bbf86b9c7c19857aa3259bd3aba007736771d07 100644 (file)
@@ -194,7 +194,7 @@ class Notification():
         :param unread: which state set for notification
         :type unread: bool
         """
-        headers = {'x-csrf-token': self._connection.get_token()}
+        headers = {'x-csrf-token': repr(self._connection)}
         params = {'set_unread': json.dumps(unread)}
         self._connection.put('notifications/{0}'.format(self['id']), params=params, headers=headers)
         self.data['unread'] = unread
@@ -206,15 +206,16 @@ class Post():
     .. note::
         Remember that you need to have access to the post.
     """
-    def __init__(self, post_id, connection):
+    def __init__(self, connection, id, fetch=True):
         """
-        :param post_id: id or guid of the post
-        :type post_id: str
+        :param id: id or guid of the post
+        :type id: str
         :param connection: connection object used to authenticate
         :type connection: connection.Connection
         """
         self._connection = connection
-        self.post_id = post_id
+        self.id = id
+        if fetch: self._fetch()
 
     def __repr__(self):
         """Returns string containing more information then str().
@@ -227,13 +228,14 @@ class Post():
         """
         return self.get_data()['text']
 
-    def get_data(self):
+    def _fetch(self):
         """This function retrieves data of the post.
         """
-        r = self._connection.get('posts/{0}.json'.format(self.post_id))
-        if r.status_code != 200:
-            raise Exception('wrong status code: {0}'.format(r.status_code))
-        return r.json()
+        request = self._connection.get('posts/{0}.json'.format(self.post_id))
+        if request.status_code != 200:
+            raise errors.PostError('wrong status code: {0}'.format(request.status_code))
+        else:
+            self.data = request.json()
 
     def like(self):
         """This function likes a post.
@@ -241,53 +243,43 @@ class Post():
 
         :returns: dict -- json formatted like object.
         """
-        data = {'authenticity_token': self._connection.get_token()}
+        data = {'authenticity_token': repr(self._connection)}
 
-        r = self._connection.post('posts/{0}/likes'.format(self.post_id),
+        request = self._connection.post('posts/{0}/likes'.format(self.post_id),
                                   data=data,
                                   headers={'accept': 'application/json'})
 
-        if r.status_code != 201:
-            raise Exception('{0}: Post could not be liked.'
-                            .format(r.status_code))
-
-        return r.json()
+        if request.status_code != 201:
+            raise errors.PostError('{0}: Post could not be liked.'
+                            .format(request.status_code))
+        return request.json()
 
     def delete_like(self):
         """This function removes a like from a post
         """
         data = {'authenticity_token': self._connection.get_token()}
 
-        post_data = self.get_data()
-
-        r = self._connection.delete('posts/{0}/likes/{1}'
+        request = self._connection.delete('posts/{0}/likes/{1}'
                                     .format(self.post_id,
-                                            post_data['interactions']
+                                            self.data['interactions']
                                                      ['likes'][0]['id']),
                                     data=data)
-
-        if r.status_code != 204:
-            raise Exception('{0}: Like could not be removed.'
-                            .format(r.status_code))
+        if request.status_code != 204:
+            raise errors.PostError('{0}: Like could not be removed.'
+                            .format(request.status_code))
 
     def reshare(self):
         """This function reshares a post
-
         """
-        post_data = self.get_data()
+        data = {'root_guid': self.data['guid'],
+                'authenticity_token': repr(self._connection)}
 
-        data = {'root_guid': post_data['guid'],
-                'authenticity_token': self._connection.get_token()}
-
-        r = self._connection.post('reshares',
+        request = self._connection.post('reshares',
                                   data=data,
                                   headers={'accept': 'application/json'})
-
-        if r.status_code != 201:
-            raise Exception('{0}: Post could not be reshared.'
-                            .format(r.status_code))
-
-        return r.json()
+        if request.status_code != 201:
+            raise Exception('{0}: Post could not be reshared'.format(request.status_code))
+        return request.json()
 
     def comment(self, text):
         """This function comments on a post
@@ -296,17 +288,15 @@ class Post():
         :type text: str
         """
         data = {'text': text,
-                'authenticity_token': self._connection.get_token()}
-
-        r = self._connection.post('posts/{0}/comments'.format(self.post_id),
+                'authenticity_token': repr(self._connection)}
+        request = self._connection.post('posts/{0}/comments'.format(self.id),
                                   data=data,
                                   headers={'accept': 'application/json'})
 
-        if r.status_code != 201:
+        if request.status_code != 201:
             raise Exception('{0}: Comment could not be posted.'
-                            .format(r.status_code))
-
-        return r.json()
+                            .format(request.status_code))
+        return request.json()
 
     def delete_comment(self, comment_id):
         """This function removes a comment from a post
@@ -314,24 +304,23 @@ class Post():
         :param comment_id: id of the comment to remove.
         :type comment_id: str
         """
-        data = {'authenticity_token': self._connection.get_token()}
-
-        r = self._connection.delete('posts/{0}/comments/{1}'
-                                    .format(self.post_id,
+        data = {'authenticity_token': repr(self._connection)}
+        request = self._connection.delete('posts/{0}/comments/{1}'
+                                    .format(self.id,
                                             comment_id),
                                     data=data,
                                     headers={'accept': 'application/json'})
 
-        if r.status_code != 204:
-            raise Exception('{0}: Comment could not be deleted.'
-                            .format(r.status_code))
+        if request.status_code != 204:
+            raise errors.PostError('{0}: Comment could not be deleted'
+                            .format(request.status_code))
 
     def delete(self):
         """ This function deletes this post
         """
-        data = {'authenticity_token': self._connection.get_token()}
-        r = self._connection.delete('posts/{0}'.format(self.post_id),
+        data = {'authenticity_token': repr(self._connection)}
+        request = self._connection.delete('posts/{0}'.format(self.id),
                                     data=data,
                                     headers={'accept': 'application/json'})
-        if r.status_code != 204:
-            raise Exception('{0}: Post could not be deleted'.format(r.status_code))
+        if request.status_code != 204:
+            raise errors.PostError('{0}: Post could not be deleted'.format(request.status_code))