"""
self._token_regex = re.compile(r'content="(.*?)"\s+name="csrf-token')
self.pod = pod
- self.logged_in = False
self.session = requests.Session()
+ self._post_data = None
self._setlogindata(username, password)
+ self._login()
def get_token(self):
"""This function gets a token needed for authentication in most cases
headers={'accept': 'application/json'})
if r.status_code != 201: raise Exception('{0}: Login failed.'.format(r.status_code))
- else: self.logged_in = True
-
- def post(self, text, aspect_id='public', photos=None):
- """This function sends a post to an aspect
-
+
+ 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.
:type aspect_id: str
+ """
+ data = {}
+ data['aspect_id'] = aspect_id
+ data['status_message'] = {'text': text}
+ if photos: data['photos'] = photos
+ self._post_data = data
- :returns: diaspy.models.Post -- the Post which has been created
+ def _post(self):
+ """Sends post to an aspect.
+ :returns: diaspy.models.Post -- the Post which has been created
"""
- data = {'aspect_ids': aspect_id,
- 'status_message': {'text': text}}
-
- if photos:
- data['photos'] = photos
r = self.session.post('{0}/status_messages'.format(self.pod),
- data=json.dumps(data),
+ data=json.dumps(self._post_data),
headers={'content-type': 'application/json',
'accept': 'application/json',
'x-csrf-token': self.get_token()})
return diaspy.models.Post(str(r.json()['id']), self)
+ def post(self, text, aspect_id='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
+
+ :returns: diaspy.models.Post -- the Post which has been created
+
+ """
+ self._setpostdata(text, aspect_id, photos)
+ return self._post()
+
def get_user_info(self):
"""This function returns the current user's attributes.
return userdata
def post_picture(self, filename):
+ """This method posts a picture to D*.
+
+ :param filename: Path to picture file.
+ :type filename: str
+ """
aspects = self.get_user_info()['aspects']
params = {}
params['photo[pending]'] = 'true'