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.
: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',
'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):
: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))
: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))
: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))
: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))
'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))
'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))
'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:
"""
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:
'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:
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:
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:
""" 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'})
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:
: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'})
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']),
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'})
"""
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'})
: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,
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: