Small style and code fixes
[diaspy.git] / diaspy / conversations.py
1 #!/usr/bin/env python3
2
3
4 class Conversation():
5 """This class represents a conversation.
6
7 .. note::
8 Remember that you need to have access to the conversation.
9 """
10 def __init__(self, conv_id, connection):
11 """
12 :param conv_id: id of the post and not the guid!
13 :type conv_id: str
14 :param connection: connection object used to authenticate
15 :type connection: connection.Connection
16
17 .. note::
18 The login function of the connection should be called,
19 before calling any of the post functions.
20
21 """
22 self._connection = connection
23 self.conv_id = conv_id
24
25 def get_data(self):
26 """ returns the plain json data representing conversation.
27 """
28 r = self._connection.get('conversations/{1}.json'.format(self.conv_id))
29 if r.status_code == 200:
30 return r.json()['conversation']
31 else:
32 raise Exception('wrong status code: {0}'.format(r.status_code))
33
34 def answer(self, text):
35 """ answer that conversation
36
37 :param text: text to answer.
38 :type text: str
39
40 """
41
42 data = {'message[text]': text,
43 'utf8': '✓',
44 'authenticity_token': self._connection.get_token()}
45
46 r = self._connection.post('conversations/{}/messages'.format(self.conv_id),
47 data=data,
48 headers={'accept': 'application/json'})
49 if r.status_code != 200:
50 raise Exception('{0}: Answer could not be posted.'
51 .format(r.status_code))
52 return r.json()
53
54 def delete(self):
55 """ delete this conversation
56 has to be implemented
57 """
58 data = {'authenticity_token': self._connection.get_token()}
59
60 r = self._connection.delete('conversations/{0}/visibility/'
61 .format(self.conv_id),
62 data=data,
63 headers={'accept': 'application/json'})
64
65 if r.status_code != 404:
66 raise Exception('{0}: Conversation could not be deleted.'
67 .format(r.status_code))
68
69 def get_subject(self):
70 """ return the subject of this conversation
71 """
72 return self.get_data()['subject']