From a7661afd8ab6db31b82110e45c75ca5c5d1d3078 Mon Sep 17 00:00:00 2001 From: Moritz Kiefer Date: Sat, 19 Jan 2013 22:43:24 +0100 Subject: [PATCH] Improve exception handling --- diaspy.py | 159 ----------------------------------------------- diaspy/client.py | 50 +++++++++------ diaspy/models.py | 55 +++++++++++----- 3 files changed, 72 insertions(+), 192 deletions(-) delete mode 100644 diaspy.py diff --git a/diaspy.py b/diaspy.py deleted file mode 100644 index da8494f..0000000 --- a/diaspy.py +++ /dev/null @@ -1,159 +0,0 @@ -""" -.. module:: diaspy - :platform: Unix, Windows - :synopsis: Simple python api for diaspora - -.. moduleauthor:: Moritz Kiefer - - -""" -import requests -import re -import json - - -class Client: - """This is the client class to connect to diaspora. - - .. note:: - - Before calling any other function - you have to call :func:`diaspy.Client.login`. - - """ - - def __init__(self, pod): - self._token_regex = re.compile(r'content="(.*?)"\s+name="csrf-token') - self._pod = pod - self._session = requests.Session() - - def _get_token(self): - r = self._session.get(self._pod + "/stream") - token = self._token_regex.search(r.text).group(1) - return token - - - def login(self, username, password): - """This function is used to connect to the pod and log in. - - :param username: The username used to log in. - :type username: str - :param password: The password used to log in. - :type password: str - - """ - self._username = username - self._password = password - r = self._session.get(self._pod + "/users/sign_in") - token = self._token_regex.search(r.text).group(1) - - data = {'user[username]': self._username, - 'user[password]': self._password, - 'authenticity_token': token, - 'commit': ''} - - r = self._session.post(self._pod + "/users/sign_in", data=data) - - def post(self, text, aspect_id='public'): - """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 - - """ - data = {'aspect_ids': aspect_id, - 'status_message[text]': text, - 'authenticity_token': self._get_token()} - r = self._session.post(self._pod + "/status_messages", data=data) - - def get_user_info(self): - """This function returns the current user's attributes. - - :returns: dict -- json formatted user info. - - """ - r = self._session.get(self._pod + "/stream") - regex = re.compile(r'window.current_user_attributes = ({.*})') - userdata = json.loads(regex.search(r.text).group(1)) - return userdata - - def like(self, post_id): - """This function likes a post - - :param post_id: id of the post to like. - :type post_id: str - :returns: dict -- json formatted like object. - - """ - - data = {'authenticity_token': self._get_token()} - - r = self._session.post(self._pod + "/posts/" + - post_id + "/likes", data=data, headers={'accept': 'application/json'}) - return r.json() - - def rmlike(self, post_id, like_id): - """This function removes a like from a post - - :param post_id: id of the post to remove the like from. - :type post_id: str - :param like_id: id of the like to remove. - :type like_id: str - - """ - - data = {'authenticity_token': self._get_token()} - - r = self._session.delete(self._pod + "/posts/" + - post_id + "/likes/" + - like_id, - data=data) - - def reshare(self, post_guid): - """This function reshares a post - - :param post_id: id of the post to resahre. - :type post_id: str - - """ - - data = {'root_guid': post_guid, - 'authenticity_token': self._get_token()} - - r = self._session.post(self._pod + "/reshares", data=data) - - def comment(self, post_id, text): - """This function comments on a post - - :param post_id: id of the post to comment on. - :type post_id: str - :param text: text to comment. - :type text: str - - """ - - data = {'text': text, - 'authenticity_token': self._get_token()} - - r = self._session.post(self._pod + "/posts/" + post_id + "/comments", data=data) - - return r.json() - - def rmcomment(self, post_id, comment_id): - """This function removes a comment from a post - - :param post_id: id of the post to remove the like from. - :type post_id: str - :param like_id: id of the like to remove. - :type like_id: str - - """ - - data = {'authenticity_token': self._get_token()} - - r = self._session.delete(self._pod + "/posts/" + - post_id + "/comments/" + - comment_id, - data=data) diff --git a/diaspy/client.py b/diaspy/client.py index b1ad81f..13782a8 100644 --- a/diaspy/client.py +++ b/diaspy/client.py @@ -6,17 +6,22 @@ import json class Client: """This is the client class to connect to diaspora. - .. note:: - - Before calling any other function - you have to call :func:`diaspy.Client.login`. - """ - def __init__(self, pod): + def __init__(self, pod, username, password): + """ + :param pod: The complete url of the diaspora pod to use. + :type pod: str + :param username: The username used to log in. + :type username: str + :param password: The password used to log in. + :type password: str + + """ self._token_regex = re.compile(r'content="(.*?)"\s+name="csrf-token') self.pod = pod self.session = requests.Session() + self._login(username, password) def get_token(self): """This function gets a token needed for authentication in most cases @@ -25,22 +30,18 @@ class Client: """ - r = self.session.get(self.pod + "/stream") + r = self.session.get(self.pod + '/stream') token = self._token_regex.search(r.text).group(1) return token - def login(self, username, password): + def _login(self, username, password): """This function is used to connect to the pod and log in. - - :param username: The username used to log in. - :type username: str - :param password: The password used to log in. - :type password: str - + .. note:: + This function shouldn't be called manually. """ self._username = username self._password = password - r = self.session.get(self.pod + "/users/sign_in") + r = self.session.get(self.pod + '/users/sign_in') token = self._token_regex.search(r.text).group(1) data = {'user[username]': self._username, @@ -48,7 +49,13 @@ class Client: 'authenticity_token': token, 'commit': ''} - r = self.session.post(self.pod + "/users/sign_in", data=data) + r = self.session.post(self.pod + + '/users/sign_in', + data=data, + headers={'accept': 'application/json'}) + + if r.status_code != 201: + raise Exception(str(r.status_code) + ': Login failed.') def post(self, text, aspect_id='public'): """This function sends a post to an aspect @@ -62,7 +69,14 @@ class Client: data = {'aspect_ids': aspect_id, 'status_message[text]': text, 'authenticity_token': self.get_token()} - r = self.session.post(self.pod + "/status_messages", data=data) + r = self.session.post(self.pod + + "/status_messages", + data=data, + headers={'accept': 'application/json'}) + if r.status_code != 201: + raise Exception(str(r.status_code) + ': Post could not be posted.') + + return r.json() def get_user_info(self): """This function returns the current user's attributes. @@ -70,7 +84,7 @@ class Client: :returns: dict -- json formatted user info. """ - r = self.session.get(self.pod + "/stream") + r = self.session.get(self.pod + '/stream') regex = re.compile(r'window.current_user_attributes = ({.*})') userdata = json.loads(regex.search(r.text).group(1)) return userdata diff --git a/diaspy/models.py b/diaspy/models.py index 8da32a6..4700d21 100644 --- a/diaspy/models.py +++ b/diaspy/models.py @@ -7,18 +7,20 @@ class Post: .. note:: Remember that you need to have access to the post. - :params post_id: id or guid of the post - :type post_id: str - :params client: client object used to authenticate - :type client: Client - - .. note:: - The login function of the client should be called, - before calling any of the post functions. - """ def __init__(self, post_id, client): + """ + :param post_id: id or guid of the post + :type post_id: str + :param client: client object used to authenticate + :type client: client.Client + + .. note:: + The login function of the client should be called, + before calling any of the post functions. + + """ self._client = client r = self._client.session.get(self._client.pod + @@ -40,11 +42,15 @@ class Post: data = {'authenticity_token': self._client.get_token()} r = self._client.session.post(self._client.pod + - "/posts/" + + '/posts/' + str(self.data['id']) + - "/likes", + '/likes', data=data, headers={'accept': 'application/json'}) + + if r.status_code != 201: + raise Exception(str(r.status_code) + ': Post could not be liked.') + return r.json() def rmlike(self): @@ -61,6 +67,10 @@ class Post: ['likes'][0]['id']), data=data) + if r.status_code != 204: + raise Exception(str(r.status_code) + + ': Like could not be removed.') + def reshare(self): """This function reshares a post @@ -70,8 +80,13 @@ class Post: 'authenticity_token': self._client.get_token()} r = self._client.session.post(self._client.pod + - "/reshares", - data=data) + '/reshares', + data=data, + headers={'accept': 'application/json'}) + + if r.status_code != 201: + raise Exception(str(r.status_code) + + ': Post could not be reshared.') return r.json() @@ -92,7 +107,12 @@ class Post: '/posts/' + str(self.data['id']) + '/comments', - data=data) + data=data, + headers={'accept': 'application/json'}) + + if r.status_code != 201: + raise Exception(str(r.status_code) + + ': Comment could not be posted.') return r.json() @@ -112,4 +132,9 @@ class Post: str(self.data['id']) + '/comments/' + comment_id, - data=data) + data=data, + headers={'accept': 'application/json'}) + + if r.status_code != 204: + raise Exception(str(r.status_code) + + ': Comment could not be deleted.') -- 2.25.1