a05620a2057e3545e6c55739346c137e1a118629
[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 """
11 def __init__(self, conv_id, client):
12 """
13 :param conv_id: id of the post and not the guid!
14 :type conv_id: str
15 :param client: client object used to authenticate
16 :type client: client.Client
17
18 .. note::
19 The login function of the client should be called,
20 before calling any of the post functions.
21
22 """
23 self._client = client
24 self.conv_id = conv_id
25
26 def get_data(self):
27 """ returns the plain json data representing conversation.
28 """
29 r = self._client._sessionget('conversations/{1}.json'.format(self.conv_id))
30 if r.status_code == 200:
31 return r.json()['conversation']
32 else:
33 raise Exception('wrong status code: {0}'.format(r.status_code))
34
35 def answer(self, text):
36 """ answer that conversation
37
38 :param text: text to answer.
39 :type text: str
40
41 """
42
43 data = {'message[text]': text,
44 'utf8': '✓',
45 'authenticity_token': self._client.get_token()}
46
47 r = self._client.session.post('{0}/conversations/{1}/messages'
48 .format(self._client.pod, self.conv_id),
49 data=data,
50 headers={'accept': 'application/json'})
51 if r.status_code != 200:
52 raise Exception('{0}: Answer could not be posted.'
53 .format(r.status_code))
54
55 return r.json()
56
57 def delete(self):
58 """ delete this conversation
59 has to be implemented
60 """
61 data = {'authenticity_token': self._client.get_token()}
62
63 r = self._client.session.delete('{0}/conversations/{1}/visibility/'
64 .format(self._client.pod,
65 self.conv_id),
66 data=data,
67 headers={'accept': 'application/json'})
68
69 if r.status_code != 404:
70 raise Exception('{0}: Conversation could not be deleted.'
71 .format(r.status_code))
72
73 def get_subject(self):
74 """ return the subject of this conversation
75 """
76 return self.get_data()['subject']