Commit | Line | Data |
---|---|---|
88ec6cda MM |
1 | #!/usr/bin/env python3 |
2 | ||
70227e79 | 3 | |
385e7ebe | 4 | class Conversation(): |
70227e79 B |
5 | """This class represents a conversation. |
6 | ||
7 | .. note:: | |
8 | Remember that you need to have access to the conversation. | |
70227e79 | 9 | """ |
b95ffe83 | 10 | def __init__(self, conv_id, connection): |
70227e79 | 11 | """ |
df912114 MM |
12 | :param conv_id: id of the post and not the guid! |
13 | :type conv_id: str | |
b95ffe83 MM |
14 | :param connection: connection object used to authenticate |
15 | :type connection: connection.Connection | |
70227e79 B |
16 | |
17 | .. note:: | |
b95ffe83 | 18 | The login function of the connection should be called, |
70227e79 B |
19 | before calling any of the post functions. |
20 | ||
21 | """ | |
b95ffe83 | 22 | self._connection = connection |
70227e79 B |
23 | self.conv_id = conv_id |
24 | ||
25 | def get_data(self): | |
df912114 | 26 | """ returns the plain json data representing conversation. |
70227e79 | 27 | """ |
b95ffe83 | 28 | r = self._connection.get('conversations/{1}.json'.format(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': '✓', | |
b95ffe83 | 44 | 'authenticity_token': self._connection.get_token()} |
70227e79 | 45 | |
b95ffe83 | 46 | r = self._connection.post('conversations/{}/messages'.format(self.conv_id), |
385e7ebe MM |
47 | data=data, |
48 | headers={'accept': 'application/json'}) | |
70227e79 | 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 | return r.json() |
53 | ||
54 | def delete(self): | |
55 | """ delete this conversation | |
56 | has to be implemented | |
57 | """ | |
b95ffe83 | 58 | data = {'authenticity_token': self._connection.get_token()} |
70227e79 | 59 | |
b95ffe83 | 60 | r = self._connection.delete('conversations/{0}/visibility/' |
385e7ebe MM |
61 | .format(self.conv_id), |
62 | data=data, | |
63 | headers={'accept': 'application/json'}) | |
70227e79 B |
64 | |
65 | if r.status_code != 404: | |
9088535d MK |
66 | raise Exception('{0}: Conversation could not be deleted.' |
67 | .format(r.status_code)) | |
70227e79 B |
68 | |
69 | def get_subject(self): | |
70 | """ return the subject of this conversation | |
71 | """ | |
72 | return self.get_data()['subject'] |