Commit | Line | Data |
---|---|---|
70227e79 B |
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 | """ | |
70227e79 B |
10 | def __init__(self, conv_id, client): |
11 | """ | |
df912114 MM |
12 | :param conv_id: id of the post and not the guid! |
13 | :type conv_id: str | |
70227e79 B |
14 | :param client: client object used to authenticate |
15 | :type client: client.Client | |
16 | ||
17 | .. note:: | |
18 | The login function of the client should be called, | |
19 | before calling any of the post functions. | |
20 | ||
21 | """ | |
22 | self._client = client | |
23 | self.conv_id = conv_id | |
24 | ||
25 | def get_data(self): | |
df912114 | 26 | """ returns the plain json data representing conversation. |
70227e79 | 27 | """ |
86690435 | 28 | r = self._client.session.get('{0}/conversations/{1}.json'.format(self._client.pod, self.conv_id)) |
70227e79 B |
29 | if r.status_code == 200: |
30 | return r.json()['conversation'] | |
31 | else: | |
86690435 | 32 | raise Exception('wrong status code: {0}'.format(r.status_code)) |
70227e79 B |
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._client.get_token()} | |
45 | ||
86690435 | 46 | r = self._client.session.post('{0}/conversations/{1}/messages'.format(self._client.pod, self.conv_id), |
70227e79 B |
47 | data=data, |
48 | headers={'accept': 'application/json'}) | |
49 | if r.status_code != 200: | |
86690435 | 50 | raise Exception('{0}: Answer could not be posted.'.format(r.status_code)) |
70227e79 B |
51 | |
52 | return r.json() | |
53 | ||
54 | def delete(self): | |
55 | """ delete this conversation | |
56 | has to be implemented | |
57 | """ | |
58 | data = {'authenticity_token': self._client.get_token()} | |
59 | ||
86690435 | 60 | r = self._client.session.delete('{0}/conversations/{1}/visibility/'.format(self._client.pod, self.conv_id), |
70227e79 B |
61 | data=data, |
62 | headers={'accept': 'application/json'}) | |
63 | ||
64 | if r.status_code != 404: | |
86690435 | 65 | raise Exception('{0}: Conversation could not be deleted.'.format(r.status_code)) |
70227e79 B |
66 | |
67 | def get_subject(self): | |
68 | """ return the subject of this conversation | |
69 | """ | |
70 | return self.get_data()['subject'] |