From 9088535d66192a4dd28405a16bad3a5c0214c570 Mon Sep 17 00:00:00 2001 From: Moritz Kiefer Date: Sat, 30 Mar 2013 00:33:18 +0100 Subject: [PATCH] Fixed posting and many style fixes --- diaspy/client.py | 65 +++++++++++++++++++++-------------------- diaspy/conversations.py | 18 +++++++----- diaspy/models.py | 51 +++++++++++++++++++++----------- setup.py | 2 +- tests.py | 19 +++++++----- 5 files changed, 92 insertions(+), 63 deletions(-) diff --git a/diaspy/client.py b/diaspy/client.py index aae0681..b558c4d 100644 --- a/diaspy/client.py +++ b/diaspy/client.py @@ -35,19 +35,17 @@ class Client: return token def _setlogindata(self, username, password): - """This function is used to set data for login. - - .. note:: + """This function is used to set data for login. + + .. note:: It should be called before _login() function. """ #r = self.session.get(self.pod + '/users/sign_in') #token = self._token_regex.search(r.text).group(1) self._username, self._password = username, password - self._login_data = { - 'user[username]': self._username, + self._login_data = {'user[username]': self._username, 'user[password]': self._password, - 'authenticity_token': self.get_token(), - } + 'authenticity_token': self.get_token()} def _login(self): """This function is used to connect to the pod and log in. @@ -55,21 +53,23 @@ class Client: r = self.session.post('{0}/users/sign_in'.format(self.pod), 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_id, photos): + 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. - + :param text: Text to post. :type text: str - :param aspect_id: Aspect id to send post to. - :type aspect_id: str + :param aspect_ids: Aspect ids to send post to. + :type aspect_ids: str """ data = {} - data['aspect_id'] = aspect_id + data['aspect_ids'] = aspect_ids data['status_message'] = {'text': text} - if photos: data['photos'] = photos + if photos: + data['photos'] = photos self._post_data = data def _post(self): @@ -82,21 +82,23 @@ class Client: headers={'content-type': 'application/json', 'accept': 'application/json', 'x-csrf-token': self.get_token()}) - if r.status_code != 201: raise Exception('{0}: Post could not be posted.'.format(r.status_code)) + if r.status_code != 201: + raise Exception('{0}: Post could not be posted.'.format( + r.status_code)) return diaspy.models.Post(str(r.json()['id']), self) - def post(self, text, aspect_id='public', photos=None): + def post(self, text, aspect_ids='public', photos=None): """This function sends a post to an aspect :param text: Text to post. :type text: str - :param aspect_id: Aspect id to send post to. - :type aspect_id: str + :param aspect_ids: Aspect ids to send post to. + :type aspect_ids: str :returns: diaspy.models.Post -- the Post which has been created """ - self._setpostdata(text, aspect_id, photos) + self._setpostdata(text, aspect_ids, photos) post = self._post() self._post_data = {} return post @@ -144,14 +146,13 @@ class Client: """ - data = {'authenticity_token': self.get_token()} r = self.session.get('{0}/stream.json'.format(self.pod)) if r.status_code != 200: raise Exception('wrong status code: {0}'.format(r.status_code)) stream = r.json() - posts = [ diaspy.models.Post(str(post['id']), self) for post in stream ] + posts = [diaspy.models.Post(str(post['id']), self) for post in stream] return posts @@ -162,7 +163,6 @@ class Client: """ - data = {'authenticity_token': self.get_token()} r = self.session.get('{0}/notifications.json'.format(self.pod)) if r.status_code != 200: @@ -179,14 +179,14 @@ class Client: """ - data = {'authenticity_token': self.get_token()} r = self.session.get('/mentions.json'.format(self.pod)) if r.status_code != 200: raise Exception('wrong status code: {0}'.format(r.status_code)) mentions = r.json() - posts = [ diaspy.models.Post(str(post['id']), self) for post in mentions ] + posts = [diaspy.models.Post(str(post['id']), self) for + post in mentions] return posts @@ -199,14 +199,14 @@ class Client: """ - data = {'authenticity_token': self.get_token()} r = self.session.get('{0}/tags/{1}.json'.format(self.pod, tag)) if r.status_code != 200: raise Exception('wrong status code: {0}'.format(r.status_code)) tagged_posts = r.json() - posts = [ diaspy.models.Post(str(post['id']), self) for post in tagged_posts ] + posts = [diaspy.models.Post(str(post['id']), self) for + post in tagged_posts] return posts @@ -245,7 +245,8 @@ class Client: 'aspect_id': aspect_id, 'person_id': user_id} - r = self.session.delete('{0}/aspect_memberships/42.json'.format(self.pod), + r = self.session.delete('{0}/aspect_memberships/42.json'.format( + self.pod), data=data) if r.status_code != 200: @@ -286,14 +287,15 @@ class Client: """ - data = {'authenticity_token': self.get_token()} 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 ] + conversations = [diaspy.conversations.Conversation( + str(conversation['conversation']['id']), self) for + conversation in mailbox] return conversations @@ -319,6 +321,7 @@ class Client: data=data, headers={'accept': 'application/json'}) if r.status_code != 200: - raise Exception('{0}: Conversation could not be started.'.format(r.status_code)) + raise Exception('{0}: Conversation could not be started.' + .format(r.status_code)) return r.json() diff --git a/diaspy/conversations.py b/diaspy/conversations.py index 22afe55..0bf4b62 100644 --- a/diaspy/conversations.py +++ b/diaspy/conversations.py @@ -1,5 +1,3 @@ -import requests - class Conversation: """This class represents a conversation. @@ -25,7 +23,8 @@ class Conversation: def get_data(self): """ returns the plain json data representing conversation. """ - r = self._client.session.get('{0}/conversations/{1}.json'.format(self._client.pod, self.conv_id)) + r = self._client.session.get('{0}/conversations/{1}.json' + .format(self._client.pod, self.conv_id)) if r.status_code == 200: return r.json()['conversation'] else: @@ -43,11 +42,13 @@ 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.session.post('{0}/conversations/{1}/messages' + .format(self._client.pod, 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)) + raise Exception('{0}: Answer could not be posted.' + .format(r.status_code)) return r.json() @@ -57,12 +58,15 @@ class Conversation: """ data = {'authenticity_token': self._client.get_token()} - r = self._client.session.delete('{0}/conversations/{1}/visibility/'.format(self._client.pod, self.conv_id), + r = self._client.session.delete('{0}/conversations/{1}/visibility/' + .format(self._client.pod, + self.conv_id), data=data, headers={'accept': 'application/json'}) if r.status_code != 404: - raise Exception('{0}: Conversation could not be deleted.'.format(r.status_code)) + raise Exception('{0}: Conversation could not be deleted.' + .format(r.status_code)) def get_subject(self): """ return the subject of this conversation diff --git a/diaspy/models.py b/diaspy/models.py index f04328e..95e2043 100644 --- a/diaspy/models.py +++ b/diaspy/models.py @@ -1,6 +1,3 @@ -import requests - - class Post: """This class represents a post. @@ -22,14 +19,15 @@ 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.session.get('{0}/posts/{1}.json' + .format(self._client.pod, 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. @@ -37,12 +35,14 @@ 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.session.post('{0}/posts/{1}/likes' + .format(self._client.pod, 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() @@ -54,11 +54,16 @@ class Post: 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,7 +79,8 @@ class Post: 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() @@ -88,12 +94,14 @@ 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.session.post('{0}/posts/{1}/comments' + .format(self._client.pod, 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() @@ -106,12 +114,16 @@ 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 @@ -119,6 +131,11 @@ class Post: """ data = {'authenticity_token': self._client.get_token()} - r = self._client.session.delete('{0}/posts/{1}'.format(self._client.pod, self.post_id), + 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/setup.py b/setup.py index d2f5a3c..2fad8a6 100644 --- a/setup.py +++ b/setup.py @@ -3,6 +3,6 @@ setup(name='diaspy', version='0.0.1', author='Moritz Kiefer', author_email='moritz.kiefer@gmail.com', - packages = find_packages(), + packages=find_packages(), install_requires=['requests'] ) diff --git a/tests.py b/tests.py index d64b235..cc48bcc 100644 --- a/tests.py +++ b/tests.py @@ -5,7 +5,8 @@ import getpass # failure to import any of the modules below indicates failed tests # modules used by diaspy -import requests, re +import requests +import re # actual diaspy code import diaspy @@ -21,23 +22,27 @@ class ClientTests(unittest.TestCase): def testInitialization(self): """This test checks initialization of Client() instance. """ - client = diaspy.client.Client(pod=__pod__, username=__username__, password=__passwd__) + client = diaspy.client.Client(pod=__pod__, + username=__username__, + password=__passwd__) self.assertEqual(__pod__, client.pod) self.assertEqual(__username__, client._username) self.assertEqual(__passwd__, client._password) self.assertEqual(None, client._post_data) - self.assertEqual(client._token_regex, re.compile(r'content="(.*?)"\s+name="csrf-token')) + self.assertEqual(client._token_regex, + re.compile(r'content="(.*?)"\s+name="csrf-token')) self.assertEqual(client._login_data['user[username]'], 'testuser') self.assertEqual(client._login_data['user[password]'], 'testpassword') - self.assertEqual(client._login_data['authenticity_token'], client.get_token()) + self.assertEqual(client._login_data['authenticity_token'], + client.get_token()) def testPreparationOfPostData(self): """This test checks correctness of data set for posting. """ - -if __name__ == '__main__': +if __name__ == '__main__': __passwd__ = getpass.getpass(prompt='Password used for testing: ') - if __passwd__ == '': __passwd__ = 'testpassword' + if __passwd__ == '': + __passwd__ = 'testpassword' unittest.main() -- 2.25.1