4 from diaspy
import errors
8 """This class represents a conversation.
11 Remember that you need to have access to the conversation.
13 def __init__(self
, connection
, id, fetch
=True):
15 :param conv_id: id of the post and not the guid!
17 :param connection: connection object used to authenticate
18 :type connection: connection.Connection
20 self
._connection
= connection
23 if fetch
: self
._fetch
()
26 """Fetches JSON data representing conversation.
28 request
= self
._connection
.get('conversations/{}.json'.format(self
.id))
29 if request
.status_code
== 200:
30 self
.data
= request
.json()['conversation']
32 raise errors
.ConversationError('cannot download conversation data: {0}'.format(request
.status_code
))
34 def answer(self
, text
):
35 """Answer that conversation
37 :param text: text to answer.
40 data
= {'message[text]': text
,
42 'authenticity_token': repr(self
._connection
)}
44 request
= self
._connection
.post('conversations/{}/messages'.format(self
.id),
46 headers
={'accept': 'application/json'})
47 if request
.status_code
!= 200:
48 raise errors
.ConversationError('{0}: Answer could not be posted.'
49 .format(request
.status_code
))
53 """Delete this conversation.
54 Has to be implemented.
56 data
= {'authenticity_token': repr(self
._connection
)}
58 request
= self
._connection
.delete('conversations/{0}/visibility/'
61 headers
={'accept': 'application/json'})
63 if request
.status_code
!= 404:
64 raise errors
.ConversationError('{0}: Conversation could not be deleted.'
65 .format(request
.status_code
))
67 def get_subject(self
):
68 """Returns the subject of this conversation
70 return self
.data
['subject']