self._login()
def _sessionget(self, string):
- """This method gets data from session.
- Performs additional checks if needed.
+ """This method gets data from session.
+ Performs additional checks if needed.
Example:
- To obtain 'foo' from pod one should call `_sessionget('foo')`.
+ To obtain 'foo' from pod one should call `_sessionget('foo')`.
:param string: URL to get without the pod's URL and slash eg. 'stream'.
:type string: str
def _sessionpost(self, string, data, headers={}, params={}):
"""This method posts data to session.
- Performs additional checks if needed.
+ Performs additional checks if needed.
Example:
- To post to 'foo' one should call `_sessionpost('foo', data={})`.
+ To post to 'foo' one should call `_sessionpost('foo', data={})`.
:param string: URL to post without the pod's URL and slash eg. 'status_messages'.
:type string: str
elif not headers and params: r = self.session.post(string, data=data, params=params)
else: r = self.session.post(string, data=data)
return r
-
+
def get_token(self):
"""This function gets a token needed for authentication in most cases
return token
def _setlogindata(self, username, password):
- """This function is used to set data for login.
-
+ """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,
+ self._login_data = {'user[username]': self._username,
'user[password]': self._password,
'authenticity_token': self.get_token(),
}
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):
"""This function prepares data for posting.
-
:param text: Text to post.
:type text: str
:param aspect_id: Aspect id to send post to.
"""
r = self._sessionget('stream.json')
- if r.status_code != 200:
+ if r.status_code != 200:
raise Exception('wrong status code: {0}'.format(r.status_code))
stream = r.json()
- return [ diaspy.models.Post(str(post['id']), self) for post in stream ]
+ return [diaspy.models.Post(str(post['id']), self) for post in stream]
def get_notifications(self):
"""This functions returns a list of notifications.
"""
r = self._sessionget('notifications.json')
- if r.status_code != 200:
+ if r.status_code != 200:
raise Exception('wrong status code: {0}'.format(r.status_code))
notifications = r.json()
raise Exception('wrong status code: {0}'.format(r.status_code))
mentions = r.json()
- return [ diaspy.models.Post(str(post['id']), self) for post in mentions ]
+ return [diaspy.models.Post(str(post['id']), self) for post in mentions]
def get_tag(self, tag):
"""This functions returns a list of posts containing the tag.
raise Exception('wrong status code: {0}'.format(r.status_code))
tagged_posts = r.json()
- return [ diaspy.models.Post(str(post['id']), self) for post in tagged_posts ]
+ return [diaspy.models.Post(str(post['id']), self) for post in tagged_posts]
def get_mailbox(self):
"""This functions returns a list of messages found in the conversation.
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.
-import requests
+#!/usr/bin/env python3
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._sessionget('posts/{1}.json'.format(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()}
def delete_like(self):
"""This function removes a like from a post
-
"""
data = {'authenticity_token': self._client.get_token()}
"""
data = {'authenticity_token': self._client.get_token()}
-
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))