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