Methods refactored to use _sessionpost(), tox.ini file added
authorMarek Marecki <triviuss@gmail.com>
Sat, 30 Mar 2013 13:30:48 +0000 (14:30 +0100)
committerMarek Marecki <triviuss@gmail.com>
Sat, 30 Mar 2013 13:30:48 +0000 (14:30 +0100)
diaspy/client.py
diaspy/conversations.py
diaspy/models.py
tox.ini [new file with mode: 0644]

index 2af284b6ce71fc33a98a18748155d468550023b5..fcb12dbc70de876d28d3cd4ca5e04924796d972c 100644 (file)
@@ -79,7 +79,7 @@ class Client:
     def _login(self):
         """This function is used to connect to the pod and log in.
         """
-        r = self.session.post('{0}/users/sign_in'.format(self.pod),
+        r = self._sessionpost('users/sign_in',
                               data=self._login_data,
                               headers={'accept': 'application/json'})
         if r.status_code != 201:
@@ -105,7 +105,7 @@ class Client:
 
         :returns: diaspy.models.Post -- the Post which has been created
         """
-        r = self.session.post('{0}/status_messages'.format(self.pod),
+        r = self._sessionpost('status_messages',
                               data=json.dumps(self._post_data),
                               headers={'content-type': 'application/json',
                                        'accept': 'application/json',
@@ -161,9 +161,7 @@ class Client:
                    'x-csrf-token': self.get_token(),
                    'x-file-name': filename}
 
-        r = self.session.post('{0}/photos'.format(self.pod),
-                              params=params, data=data, headers=headers)
-
+        r = self._sessionpost('photos', params=params, data=data, headers=headers)
         return r
 
     def get_stream(self):
@@ -232,7 +230,8 @@ class Client:
             raise Exception('wrong status code: {0}'.format(r.status_code))
 
         mailbox = r.json()
-        return [diaspy.conversations.Conversation(str(conversation['conversation']['id']), self) for conversation in mailbox]
+        return [diaspy.conversations.Conversation(str(conversation['conversation']['id']), self)
+                for conversation in mailbox]
 
     def add_user_to_aspect(self, user_id, aspect_id):
         """ this function adds a user to an aspect.
@@ -243,13 +242,11 @@ class Client:
         :type aspect_id: str
 
         """
-
         data = {'authenticity_token': self.get_token(),
                 'aspect_id': aspect_id,
                 'person_id': user_id}
 
-        r = self.session.post('{0}/aspect_memberships.json'.format(self.pod),
-                              data=data)
+        r = self._sessionpost('aspect_memberships.json', data=data)
 
         if r.status_code != 201:
             raise Exception('wrong status code: {0}'.format(r.status_code))
@@ -263,8 +260,7 @@ class Client:
                 'aspect[name]': aspect_name,
                 'aspect[contacts_visible]': visible}
 
-        r = self.session.post('{0}/aspects'.format(self.pod),
-                              data=data)
+        r = self._sessionpost('aspects', data=data)
 
         if r.status_code != 200:
             raise Exception('wrong status code: {0}'.format(r.status_code))
@@ -303,25 +299,6 @@ class Client:
         if r.status_code != 404:
             raise Exception('wrong status code: {0}'.format(r.status_code))
 
-    def get_mailbox(self):
-        """This functions returns a list of messages found in the conversation.
-
-        :returns: list -- list of Conversation objects.
-
-        """
-
-        r = self.session.get('{0}/conversations.json'.format(self.pod))
-
-        if r.status_code != 200:
-            raise Exception('wrong status code: {0}'.format(r.status_code))
-
-        mailbox = r.json()
-        conversations = [diaspy.conversations.Conversation(
-                         str(conversation['conversation']['id']), self) for
-                         conversation in mailbox]
-
-        return conversations
-
     def new_conversation(self, contacts, subject, text):
         """Start a new conversation.
 
@@ -338,7 +315,7 @@ class Client:
                 'utf8': '&#x2713;',
                 'authenticity_token': self.get_token()}
 
-        r = self.session.post('{0}/conversations/'.format(self.pod),
+        r = self._sessionpost('conversations/',
                               data=data,
                               headers={'accept': 'application/json'})
         if r.status_code != 200:
index a05620a2057e3545e6c55739346c137e1a118629..862bdbbe68f30d9b79ceafb97f67c7b4b8e8a3dc 100644 (file)
@@ -44,14 +44,12 @@ class Conversation:
                 'utf8': '&#x2713;',
                 'authenticity_token': self._client.get_token()}
 
-        r = self._client.session.post('{0}/conversations/{1}/messages'
-                                      .format(self._client.pod, self.conv_id),
+        r = self._client._sessionpost('conversations/{}/messages'.format(self.conv_id),
                                       data=data,
                                       headers={'accept': 'application/json'})
         if r.status_code != 200:
             raise Exception('{0}: Answer could not be posted.'
                             .format(r.status_code))
-
         return r.json()
 
     def delete(self):
index 34a675b9bde1e1d5dcc3f88415611e4324eda606..aa0d632daead03a7a2901c11ce63d9d4dc7fb750 100644 (file)
@@ -35,8 +35,7 @@ class Post:
         """
         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'})
 
@@ -73,7 +72,7 @@ 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'})
 
@@ -93,8 +92,7 @@ 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'})
 
@@ -126,7 +124,6 @@ class Post:
 
     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),
@@ -134,12 +131,3 @@ class Post:
                                         headers={'accept': 'application/json'})
         if r.status_code != 204:
             raise Exception('{0}: Post could not be deleted'.format(r.status_code))
-
-        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))
diff --git a/tox.ini b/tox.ini
new file mode 100644 (file)
index 0000000..00f8f44
--- /dev/null
+++ b/tox.ini
@@ -0,0 +1,3 @@
+[flake8]
+ignore=E701
+max-line-length=120