Commit | Line | Data |
---|---|---|
a993a4b6 | 1 | import requests |
ae221396 MK |
2 | |
3 | ||
a993a4b6 | 4 | class Post: |
ae221396 MK |
5 | """This class represents a post. |
6 | ||
7 | .. note:: | |
8 | Remember that you need to have access to the post. | |
9 | ||
ae221396 | 10 | """ |
a993a4b6 MK |
11 | |
12 | def __init__(self, post_id, client): | |
a7661afd MK |
13 | """ |
14 | :param post_id: id or guid of the post | |
15 | :type post_id: str | |
16 | :param client: client object used to authenticate | |
17 | :type client: client.Client | |
18 | ||
19 | .. note:: | |
20 | The login function of the client should be called, | |
21 | before calling any of the post functions. | |
22 | ||
23 | """ | |
a993a4b6 | 24 | self._client = client |
8095ac8b MK |
25 | self.post_id = post_id |
26 | ||
27 | ||
7eb90eb7 | 28 | def get_data(self): |
ae221396 MK |
29 | r = self._client.session.get(self._client.pod + |
30 | '/posts/' + | |
8095ac8b | 31 | self.post_id + |
ae221396 | 32 | '.json') |
a993a4b6 | 33 | if r.status_code == 200: |
b356c9f9 | 34 | return r.json() |
a993a4b6 MK |
35 | else: |
36 | raise Exception('wrong status code: ' + str(r.status_code)) | |
37 | ||
38 | def like(self): | |
39 | """This function likes a post | |
40 | ||
41 | :returns: dict -- json formatted like object. | |
42 | ||
43 | """ | |
44 | ||
45 | data = {'authenticity_token': self._client.get_token()} | |
46 | ||
47 | r = self._client.session.post(self._client.pod + | |
a7661afd | 48 | '/posts/' + |
8095ac8b | 49 | self.post_id + |
a7661afd | 50 | '/likes', |
a993a4b6 MK |
51 | data=data, |
52 | headers={'accept': 'application/json'}) | |
a7661afd MK |
53 | |
54 | if r.status_code != 201: | |
55 | raise Exception(str(r.status_code) + ': Post could not be liked.') | |
56 | ||
a993a4b6 MK |
57 | return r.json() |
58 | ||
5e809c8b | 59 | def delete_like(self): |
a993a4b6 MK |
60 | """This function removes a like from a post |
61 | ||
62 | """ | |
63 | ||
64 | data = {'authenticity_token': self._client.get_token()} | |
65 | ||
7eb90eb7 | 66 | post_data = self.get_data() |
8095ac8b | 67 | |
ae221396 | 68 | r = self._client.session.delete(self._client.pod + '/posts/' + |
8095ac8b | 69 | self.post_id + |
a993a4b6 | 70 | '/likes/' + |
8095ac8b | 71 | str(post_data['interactions'] |
ae221396 | 72 | ['likes'][0]['id']), |
a993a4b6 MK |
73 | data=data) |
74 | ||
a7661afd MK |
75 | if r.status_code != 204: |
76 | raise Exception(str(r.status_code) + | |
77 | ': Like could not be removed.') | |
78 | ||
a993a4b6 MK |
79 | def reshare(self): |
80 | """This function reshares a post | |
81 | ||
82 | """ | |
83 | ||
7eb90eb7 | 84 | post_data = self.get_data() |
8095ac8b MK |
85 | |
86 | data = {'root_guid': post_data['guid'], | |
a993a4b6 MK |
87 | 'authenticity_token': self._client.get_token()} |
88 | ||
89 | r = self._client.session.post(self._client.pod + | |
a7661afd MK |
90 | '/reshares', |
91 | data=data, | |
92 | headers={'accept': 'application/json'}) | |
93 | ||
94 | if r.status_code != 201: | |
95 | raise Exception(str(r.status_code) + | |
96 | ': Post could not be reshared.') | |
a993a4b6 MK |
97 | |
98 | return r.json() | |
99 | ||
100 | def comment(self, text): | |
101 | """This function comments on a post | |
102 | ||
103 | :param post_id: id of the post to comment on. | |
104 | :type post_id: str | |
105 | :param text: text to comment. | |
106 | :type text: str | |
107 | ||
108 | """ | |
109 | ||
110 | data = {'text': text, | |
111 | 'authenticity_token': self._client.get_token()} | |
112 | ||
ae221396 MK |
113 | r = self._client.session.post(self._client.pod + |
114 | '/posts/' + | |
8095ac8b | 115 | self.post_id + |
ae221396 | 116 | '/comments', |
a7661afd MK |
117 | data=data, |
118 | headers={'accept': 'application/json'}) | |
119 | ||
120 | if r.status_code != 201: | |
121 | raise Exception(str(r.status_code) + | |
122 | ': Comment could not be posted.') | |
a993a4b6 MK |
123 | |
124 | return r.json() | |
ae221396 | 125 | |
5e809c8b | 126 | def delete_comment(self, comment_id): |
a993a4b6 MK |
127 | """This function removes a comment from a post |
128 | ||
129 | :param post_id: id of the post to remove the like from. | |
130 | :type post_id: str | |
131 | :param like_id: id of the like to remove. | |
132 | :type like_id: str | |
133 | ||
134 | """ | |
135 | ||
136 | data = {'authenticity_token': self._client.get_token()} | |
137 | ||
ae221396 | 138 | r = self._client.session.delete(self._client.pod + '/posts/' + |
8095ac8b | 139 | self.post_id + |
ae221396 MK |
140 | '/comments/' + |
141 | comment_id, | |
a7661afd MK |
142 | data=data, |
143 | headers={'accept': 'application/json'}) | |
144 | ||
145 | if r.status_code != 204: | |
146 | raise Exception(str(r.status_code) + | |
147 | ': Comment could not be deleted.') | |
5e809c8b MK |
148 | |
149 | def delete(self): | |
150 | """ This function deletes this post | |
151 | ||
152 | """ | |
153 | ||
154 | data = {'authenticity_token': self._client.get_token()} | |
155 | ||
156 | r = self._client.session.delete(self._client.pod + '/posts/' + | |
157 | self.post_id, | |
158 | data=data, | |
159 | headers={'accept': 'application/json'}) |