From: Marek Marecki Date: Sat, 13 Apr 2013 10:44:00 +0000 (+0200) Subject: Further porting on way to use `Connection()` X-Git-Url: https://vcs.fsf.org/?a=commitdiff_plain;h=b95ffe83b7180e49504974ace86b56fefb59e2a6;p=diaspy.git Further porting on way to use `Connection()` --- diff --git a/diaspy/client.py b/diaspy/client.py index 44c01fe..337b6be 100644 --- a/diaspy/client.py +++ b/diaspy/client.py @@ -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': '✓', '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: diff --git a/diaspy/conversations.py b/diaspy/conversations.py index 631f77e..72e13db 100644 --- a/diaspy/conversations.py +++ b/diaspy/conversations.py @@ -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': '✓', - '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'}) diff --git a/diaspy/models.py b/diaspy/models.py index 5c61185..7eaea06 100644 --- a/diaspy/models.py +++ b/diaspy/models.py @@ -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: