Minor documentation fix in diaspy/models.py
[diaspy.git] / diaspy / conversations.py
1 import requests
2
3 class Conversation:
4 """This class represents a conversation.
5
6 .. note::
7 Remember that you need to have access to the conversation.
8
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.session.get(self._client.pod +
30 '/conversations/' +
31 self.conv_id +
32 '.json')
33 if r.status_code == 200:
34 return r.json()['conversation']
35 else:
36 raise Exception('wrong status code: ' + str(r.status_code))
37
38 def answer(self, text):
39 """ answer that conversation
40
41 :param text: text to answer.
42 :type text: str
43
44 """
45
46 data = {'message[text]': text,
47 'utf8': '✓',
48 'authenticity_token': self._client.get_token()}
49
50 r = self._client.session.post(self._client.pod +
51 '/conversations/' +
52 self.conv_id +
53 '/messages',
54 data=data,
55 headers={'accept': 'application/json'})
56 if r.status_code != 200:
57 raise Exception(str(r.status_code) +
58 ': Answer could not be posted.')
59
60 return r.json()
61
62 def delete(self):
63 """ delete this conversation
64 has to be implemented
65 """
66 data = {'authenticity_token': self._client.get_token()}
67
68 r = self._client.session.delete(self._client.pod + '/conversations/' +
69 self.conv_id +
70 '/visibility/',
71 data=data,
72 headers={'accept': 'application/json'})
73
74 if r.status_code != 404:
75 raise Exception(str(r.status_code) +
76 ': Conversation could not be deleted.')
77
78 def get_subject(self):
79 """ return the subject of this conversation
80 """
81 return self.get_data()['subject']