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.
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):
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
"""
- 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
"""
- data = {'authenticity_token': self.get_token()}
r = self.session.get('{0}/notifications.json'.format(self.pod))
if r.status_code != 200:
"""
- 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
"""
- 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
'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:
"""
- 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
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()
-import requests
-
class Conversation:
"""This class represents a 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:
'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()
"""
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
-import requests
-
-
class Post:
"""This class represents a 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.
"""
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()
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
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()
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()
"""
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
"""
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))
version='0.0.1',
author='Moritz Kiefer',
author_email='moritz.kiefer@gmail.com',
- packages = find_packages(),
+ packages=find_packages(),
install_requires=['requests']
)
# 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
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()