From 490a69d005c8b5fcbd011453918c4dca17870ee7 Mon Sep 17 00:00:00 2001 From: Marek Marecki Date: Sat, 30 Mar 2013 14:30:48 +0100 Subject: [PATCH] Methods refactored to use _sessionpost(), tox.ini file added --- diaspy/client.py | 39 ++++++++------------------------------- diaspy/conversations.py | 4 +--- diaspy/models.py | 18 +++--------------- tox.ini | 3 +++ 4 files changed, 15 insertions(+), 49 deletions(-) create mode 100644 tox.ini diff --git a/diaspy/client.py b/diaspy/client.py index 2af284b..fcb12db 100644 --- a/diaspy/client.py +++ b/diaspy/client.py @@ -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': '✓', '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: diff --git a/diaspy/conversations.py b/diaspy/conversations.py index a05620a..862bdbb 100644 --- a/diaspy/conversations.py +++ b/diaspy/conversations.py @@ -44,14 +44,12 @@ class Conversation: 'utf8': '✓', '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): diff --git a/diaspy/models.py b/diaspy/models.py index 34a675b..aa0d632 100644 --- a/diaspy/models.py +++ b/diaspy/models.py @@ -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 index 0000000..00f8f44 --- /dev/null +++ b/tox.ini @@ -0,0 +1,3 @@ +[flake8] +ignore=E701 +max-line-length=120 -- 2.25.1