From 7ebc8b0d863a67431aa168f018af5cae43276b39 Mon Sep 17 00:00:00 2001 From: Marek Marecki Date: Sun, 7 Jul 2013 15:18:43 +0200 Subject: [PATCH] Refactored `Conversation()` to use `repr()` function on `Connection()`, refactored internals and API of `Conversation()` --- diaspy/conversations.py | 66 ++++++++++++++++++++--------------------- diaspy/errors.py | 6 ++++ 2 files changed, 38 insertions(+), 34 deletions(-) diff --git a/diaspy/conversations.py b/diaspy/conversations.py index fc2218d..b1560ad 100644 --- a/diaspy/conversations.py +++ b/diaspy/conversations.py @@ -1,72 +1,70 @@ #!/usr/bin/env python3 +from diaspy import errors + + class Conversation(): """This class represents a conversation. .. note:: Remember that you need to have access to the conversation. """ - def __init__(self, conv_id, connection): + def __init__(self, connection, id, fetch=True): """ :param conv_id: id of the post and not the guid! :type conv_id: str :param connection: connection object used to authenticate :type connection: connection.Connection - - .. note:: - The login function of the connection should be called, - before calling any of the post functions. - """ self._connection = connection - self.conv_id = conv_id + self.id = id + self.data = {} + if fetch: self._fetch() - def get_data(self): - """ returns the plain json data representing conversation. + def _fetch(self): + """Fetches JSON data representing conversation. """ - r = self._connection.get('conversations/{1}.json'.format(self.conv_id)) - if r.status_code == 200: - return r.json()['conversation'] + request = self._connection.get('conversations/{}.json'.format(self.conv_id)) + if request.status_code == 200: + self.data = request.json()['conversation'] else: - raise Exception('wrong status code: {0}'.format(r.status_code)) + raise errors.ConversationError('cannot download conversation data: {0}'.format(request.status_code)) def answer(self, text): - """ answer that conversation + """Answer that conversation :param text: text to answer. :type text: str - """ - data = {'message[text]': text, 'utf8': '✓', - 'authenticity_token': self._connection.get_token()} + 'authenticity_token': repr(self._connection)} - r = self._connection.post('conversations/{}/messages'.format(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)) - return r.json() + request = self._connection.post('conversations/{}/messages'.format(self.id), + data=data, + headers={'accept': 'application/json'}) + if request.status_code != 200: + raise errors.ConversationError('{0}: Answer could not be posted.' + .format(request.status_code)) + return request.json() def delete(self): - """ delete this conversation - has to be implemented + """Delete this conversation. + Has to be implemented. """ - data = {'authenticity_token': self._connection.get_token()} + data = {'authenticity_token': repr(self._connection)} - r = self._connection.delete('conversations/{0}/visibility/' - .format(self.conv_id), + request = self._connection.delete('conversations/{0}/visibility/' + .format(self.id), data=data, headers={'accept': 'application/json'}) - if r.status_code != 404: - raise Exception('{0}: Conversation could not be deleted.' - .format(r.status_code)) + if request.status_code != 404: + raise errors.ConversationError('{0}: Conversation could not be deleted.' + .format(request.status_code)) def get_subject(self): - """ return the subject of this conversation + """Returns the subject of this conversation """ - return self.get_data()['subject'] + return self.data['subject'] diff --git a/diaspy/errors.py b/diaspy/errors.py index 90cc28d..74562c3 100644 --- a/diaspy/errors.py +++ b/diaspy/errors.py @@ -22,6 +22,12 @@ class UserError(DiaspyError): pass +class ConversationError(DiaspyError): + """Exception raised when something related to conversations goes wrong. + """ + pass + + def react(r, message='', accepted=[200, 201, 202, 203, 204, 205, 206], exception=DiaspyError): """This method tries to decides how to react to a response code passed to it. If it's an -- 2.25.1