Further porting on way to use `Connection()`
authorMarek Marecki <triviuss@gmail.com>
Sat, 13 Apr 2013 10:44:00 +0000 (12:44 +0200)
committerMarek Marecki <triviuss@gmail.com>
Sat, 13 Apr 2013 10:44:00 +0000 (12:44 +0200)
diaspy/client.py
diaspy/conversations.py
diaspy/models.py

index 44c01fee7af764ef619fd6f24f0a9670db4f6ad8..337b6be99ee535edf204ea397df0e48c924f2a53 100644 (file)
@@ -66,34 +66,6 @@ class Client:
         request = self.connection.delete(string, data, headers)
         return request
 
-    def get_token(self):
-        """This function gets a token needed for authentication in most cases
-
-        :returns: string -- token used to authenticate
-        """
-        r = self.connect.get('stream')
-        token = self._token_regex.search(r.text).group(1)
-        return token
-
-    def _setlogindata(self, username, password):
-        """This function is used to set data for login.
-        .. note::
-            It should be called before _login() function.
-        """
-        self._username, self._password = username, password
-        self._login_data = {'user[username]': self._username,
-                            'user[password]': self._password,
-                            'authenticity_token': self.get_token()}
-
-    def _login(self):
-        """This function is used to connect to the pod and log in.
-        """
-        r = self._sessionpost('users/sign_in',
-                              data=self._login_data,
-                              headers={'accept': 'application/json'})
-        if r.status_code != 201:
-            raise Exception('{0}: Login failed.'.format(r.status_code))
-
     def _setpostdata(self, text, aspect_ids, photos):
         """This function prepares data for posting.
 
@@ -114,7 +86,7 @@ class Client:
 
         :returns: diaspy.models.Post -- the Post which has been created
         """
-        r = self._sessionpost('status_messages',
+        r = self.connection.post('status_messages',
                               data=json.dumps(self._post_data),
                               headers={'content-type': 'application/json',
                                        'accept': 'application/json',
@@ -170,7 +142,7 @@ class Client:
                    'x-csrf-token': self.get_token(),
                    'x-file-name': filename}
 
-        r = self._sessionpost('photos', params=params, data=data, headers=headers)
+        r = self.connection.post('photos', params=params, data=data, headers=headers)
         return r
 
     def get_stream(self):
@@ -191,7 +163,7 @@ class Client:
 
         :returns: list -- list of json formatted notifications
         """
-        r = self._sessionget('notifications.json')
+        r = self.connection.get('notifications.json')
 
         if r.status_code != 200:
             raise Exception('wrong status code: {0}'.format(r.status_code))
@@ -205,7 +177,7 @@ class Client:
 
         :returns: list -- list of Post objects
         """
-        r = self._sessionget('mentions.json')
+        r = self.connection.get('mentions.json')
 
         if r.status_code != 200:
             raise Exception('wrong status code: {0}'.format(r.status_code))
@@ -220,7 +192,7 @@ class Client:
 
         :returns: list -- list of Post objects
         """
-        r = self._sessionget('tags/{0}.json'.format(tag))
+        r = self.connection.get('tags/{0}.json'.format(tag))
 
         if r.status_code != 200:
             raise Exception('wrong status code: {0}'.format(r.status_code))
@@ -233,7 +205,7 @@ class Client:
 
         :returns: list -- list of Conversation objects.
         """
-        r = self._sessionget('conversations.json')
+        r = self.connection.get('conversations.json')
 
         if r.status_code != 200:
             raise Exception('wrong status code: {0}'.format(r.status_code))
@@ -255,7 +227,7 @@ class Client:
                 'aspect_id': aspect_id,
                 'person_id': user_id}
 
-        r = self._sessionpost('aspect_memberships.json', data=data)
+        r = self.connection.post('aspect_memberships.json', data=data)
 
         if r.status_code != 201:
             raise Exception('wrong status code: {0}'.format(r.status_code))
@@ -269,7 +241,7 @@ class Client:
                 'aspect[name]': aspect_name,
                 'aspect[contacts_visible]': visible}
 
-        r = self._sessionpost('aspects', data=data)
+        r = self.connection.post('aspects', data=data)
 
         if r.status_code != 200:
             raise Exception('wrong status code: {0}'.format(r.status_code))
@@ -287,7 +259,7 @@ class Client:
                 'aspect_id': aspect_id,
                 'person_id': user_id}
 
-        r = self._sessiondelete('aspect_memberships/42.json',
+        r = self.connection.delete('aspect_memberships/42.json',
                                 data=data)
 
         if r.status_code != 200:
@@ -300,7 +272,7 @@ class Client:
         """
         data = {'authenticity_token': self.get_token()}
 
-        r = self._sessiondelete('aspects/{}'.format(aspect_id),
+        r = self.connection.delete('aspects/{}'.format(aspect_id),
                                 data=data)
 
         if r.status_code != 404:
@@ -322,7 +294,7 @@ class Client:
                 'utf8': '&#x2713;',
                 'authenticity_token': self.get_token()}
 
-        r = self._sessionpost('conversations/',
+        r = self.connection.post('conversations/',
                               data=data,
                               headers={'accept': 'application/json'})
         if r.status_code != 200:
index 631f77e5e9e5ddf3ef8695b895b512b9cbd3eb05..72e13db496cc431db064abbcf778f27c427c2dd6 100644 (file)
@@ -8,25 +8,25 @@ class Conversation:
         Remember that you need to have access to the conversation.
 
     """
-    def __init__(self, conv_id, client):
+    def __init__(self, conv_id, connection):
         """
         :param conv_id: id of the post and not the guid!
         :type conv_id: str
-        :param client: client object used to authenticate
-        :type client: client.Client
+        :param connection: connection object used to authenticate
+        :type connection: connection.Connection
 
         .. note::
-            The login function of the client should be called,
+            The login function of the connection should be called,
             before calling any of the post functions.
 
         """
-        self._client = client
+        self._connection = connection
         self.conv_id = conv_id
 
     def get_data(self):
         """ returns the plain json data representing conversation.
         """
-        r = self._client._sessionget('conversations/{1}.json'.format(self.conv_id))
+        r = self._connection.get('conversations/{1}.json'.format(self.conv_id))
         if r.status_code == 200:
             return r.json()['conversation']
         else:
@@ -42,9 +42,9 @@ class Conversation:
 
         data = {'message[text]': text,
                 'utf8': '&#x2713;',
-                'authenticity_token': self._client.get_token()}
+                'authenticity_token': self._connection.get_token()}
 
-        r = self._client._sessionpost('conversations/{}/messages'.format(self.conv_id),
+        r = self._connection.post('conversations/{}/messages'.format(self.conv_id),
                                       data=data,
                                       headers={'accept': 'application/json'})
         if r.status_code != 200:
@@ -56,9 +56,9 @@ class Conversation:
         """ delete this conversation
             has to be implemented
         """
-        data = {'authenticity_token': self._client.get_token()}
+        data = {'authenticity_token': self._connection.get_token()}
 
-        r = self._client._sessiondelete('conversations/{0}/visibility/'
+        r = self._connection.delete('conversations/{0}/visibility/'
                                         .format(self.conv_id),
                                         data=data,
                                         headers={'accept': 'application/json'})
index 5c61185e3ccb288d0a56d454ce6790f42fd3223b..7eaea06c0f842c323388ade778754831bc1b480f 100644 (file)
@@ -8,20 +8,20 @@ class Post:
         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):
+    def __init__(self, post_id, connection):
         """
         :param post_id: id or guid of the post
         :type post_id: str
-        :param client: client object used to authenticate
-        :type client: client.Client
+        :param connection: connection object used to authenticate
+        :type connection: connection.Connection
         """
-        self._client = client
+        self._connection = connection
         self.post_id = post_id
 
     def get_data(self):
         """This function retrieves data of the post.
         """
-        r = self._client._sessionget('posts/{1}.json'.format(self.post_id))
+        r = self._connection.get('posts/{1}.json'.format(self.post_id))
         if r.status_code == 200:
             return r.json()
         else:
@@ -33,9 +33,9 @@ class Post:
 
         :returns: dict -- json formatted like object.
         """
-        data = {'authenticity_token': self._client.get_token()}
+        data = {'authenticity_token': self._connection.getToken()}
 
-        r = self._client._sessionpost('posts/{0}/likes'.format(self.post_id),
+        r = self._connection.post('posts/{0}/likes'.format(self.post_id),
                                       data=data,
                                       headers={'accept': 'application/json'})
 
@@ -48,11 +48,11 @@ class Post:
     def delete_like(self):
         """This function removes a like from a post
         """
-        data = {'authenticity_token': self._client.get_token()}
+        data = {'authenticity_token': self._connection.getToken()}
 
         post_data = self.get_data()
 
-        r = self._client._sessiondelete('posts/{0}/likes/{1}'
+        r = self._connection.delete('posts/{0}/likes/{1}'
                                         .format(self.post_id,
                                                 post_data['interactions']
                                                          ['likes'][0]['id']),
@@ -69,9 +69,9 @@ class Post:
         post_data = self.get_data()
 
         data = {'root_guid': post_data['guid'],
-                'authenticity_token': self._client.get_token()}
+                'authenticity_token': self._connection.getToken()}
 
-        r = self._client._sessionpost('reshares',
+        r = self._connection.post('reshares',
                                       data=data,
                                       headers={'accept': 'application/json'})
 
@@ -89,9 +89,9 @@ class Post:
 
         """
         data = {'text': text,
-                'authenticity_token': self._client.get_token()}
+                'authenticity_token': self._connection.getToken()}
 
-        r = self._client._sessionpost('posts/{0}/comments'.format(self.post_id),
+        r = self._connection.post('posts/{0}/comments'.format(self.post_id),
                                       data=data,
                                       headers={'accept': 'application/json'})
 
@@ -108,9 +108,9 @@ class Post:
         :type comment_id: str
 
         """
-        data = {'authenticity_token': self._client.get_token()}
+        data = {'authenticity_token': self._connection.getToken()}
 
-        r = self._client._sessiondelete('posts/{0}/comments/{1}'
+        r = self._connection.delete('posts/{0}/comments/{1}'
                                         .format(self.post_id,
                                                 comment_id),
                                         data=data,
@@ -123,8 +123,8 @@ class Post:
     def delete(self):
         """ This function deletes this post
         """
-        data = {'authenticity_token': self._client.get_token()}
-        r = self._client._sessiondelete('posts/{0}'.format(self.post_id),
+        data = {'authenticity_token': self._connection.getToken()}
+        r = self._connection.delete('posts/{0}'.format(self.post_id),
                                         data=data,
                                         headers={'accept': 'application/json'})
         if r.status_code != 204: