Methods refactored to use _sessionpost(), tox.ini file added
[diaspy.git] / diaspy / models.py
index f3aaf1313b3363b8f93d8a5e870059de967f2bfa..aa0d632daead03a7a2901c11ce63d9d4dc7fb750 100644 (file)
@@ -1,4 +1,4 @@
-import requests
+#!/usr/bin/env python3
 
 
 class Post:
@@ -7,7 +7,6 @@ class Post:
     .. note::
         Remember that you need to have access to the post.
         Remember that you also need to be logged in.
-
     """
     def __init__(self, post_id, client):
         """
@@ -15,11 +14,6 @@ class Post:
         :type post_id: str
         :param client: client object used to authenticate
         :type client: client.Client
-
-        .. note::
-            The login function of the client should be called,
-            before calling any of the post functions.
-
         """
         self._client = client
         self.post_id = post_id
@@ -27,43 +21,47 @@ class Post:
     def get_data(self):
         """This function retrieves data of the post.
         """
-        r = self._client.session.get('{0}/posts/{1}.json'.format(self._client.pod, self.post_id))
-        if r.status_code == 200: 
+        r = self._client._sessionget('posts/{1}.json'.format(self.post_id))
+        if r.status_code == 200:
             return r.json()
-        else: 
+        else:
             raise Exception('wrong status code: {0}'.format(r.status_code))
 
     def like(self):
-        """This function likes a post. 
+        """This function likes a post.
         It abstracts the 'Like' functionality.
 
         :returns: dict -- json formatted like object.
-
         """
         data = {'authenticity_token': self._client.get_token()}
 
-        r = self._client.session.post('{0}/posts/{1}/likes'.format(self._client.pod, self.post_id),
+        r = self._client._sessionpost('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))
+            raise Exception('{0}: Post could not be liked.'
+                            .format(r.status_code))
 
         return r.json()
 
     def delete_like(self):
         """This function removes a like from a post
-
         """
         data = {'authenticity_token': self._client.get_token()}
 
         post_data = self.get_data()
 
-        r = self._client.session.delete('{0}/posts/{1}/likes/{2}'.format(self._client.pod, self.post_id, post_data['interactions']['likes'][0]['id']),
+        r = self._client.session.delete('{0}/posts/{1}/likes/{2}'
+                                        .format(self._client.pod,
+                                                self.post_id,
+                                                post_data['interactions']
+                                                         ['likes'][0]['id']),
                                         data=data)
 
         if r.status_code != 204:
-            raise Exception('{0}: Like could not be removed.'.format(r.status_code))
+            raise Exception('{0}: Like could not be removed.'
+                            .format(r.status_code))
 
     def reshare(self):
         """This function reshares a post
@@ -74,12 +72,13 @@ class Post:
         data = {'root_guid': post_data['guid'],
                 'authenticity_token': self._client.get_token()}
 
-        r = self._client.session.post('{0}/reshares'.format(self._client.pod),
+        r = self._client._sessionpost('reshares',
                                       data=data,
                                       headers={'accept': 'application/json'})
 
         if r.status_code != 201:
-            raise Exception('{0}: Post could not be reshared.'.format(r.status_code))
+            raise Exception('{0}: Post could not be reshared.'
+                            .format(r.status_code))
 
         return r.json()
 
@@ -93,12 +92,13 @@ class Post:
         data = {'text': text,
                 'authenticity_token': self._client.get_token()}
 
-        r = self._client.session.post('{0}/posts/{1}/comments'.format(self._client.pod, self.post_id),
+        r = self._client._sessionpost('posts/{0}/comments'.format(self.post_id),
                                       data=data,
                                       headers={'accept': 'application/json'})
 
         if r.status_code != 201:
-            raise Exception('{0}: Comment could not be posted.'.format(r.status_code))
+            raise Exception('{0}: Comment could not be posted.'
+                            .format(r.status_code))
 
         return r.json()
 
@@ -111,19 +111,23 @@ class Post:
         """
         data = {'authenticity_token': self._client.get_token()}
 
-        r = self._client.session.delete('{0}/posts/{1}/comments/{2}'.format(self._client.pod, self.post_id, comment_id),
+        r = self._client.session.delete('{0}/posts/{1}/comments/{2}'
+                                        .format(self._client.pod,
+                                                self.post_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))
+            raise Exception('{0}: Comment could not be deleted.'
+                            .format(r.status_code))
 
     def delete(self):
         """ This function deletes this post
-
         """
         data = {'authenticity_token': self._client.get_token()}
-
         r = self._client.session.delete('{0}/posts/{1}'.format(self._client.pod, self.post_id),
                                         data=data,
                                         headers={'accept': 'application/json'})
+        if r.status_code != 204:
+            raise Exception('{0}: Post could not be deleted'.format(r.status_code))