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 | ||
10 | :params post_id: id or guid of the post | |
11 | :type post_id: str | |
12 | :params client: client object used to authenticate | |
13 | :type 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 | """ | |
a993a4b6 MK |
20 | |
21 | def __init__(self, post_id, client): | |
ae221396 | 22 | |
a993a4b6 | 23 | self._client = client |
ae221396 MK |
24 | r = self._client.session.get(self._client.pod + |
25 | '/posts/' + | |
26 | post_id + | |
27 | '.json') | |
a993a4b6 MK |
28 | if r.status_code == 200: |
29 | self.data = r.json() | |
30 | else: | |
31 | raise Exception('wrong status code: ' + str(r.status_code)) | |
32 | ||
33 | def like(self): | |
34 | """This function likes a post | |
35 | ||
36 | :returns: dict -- json formatted like object. | |
37 | ||
38 | """ | |
39 | ||
40 | data = {'authenticity_token': self._client.get_token()} | |
41 | ||
42 | r = self._client.session.post(self._client.pod + | |
43 | "/posts/" + | |
44 | str(self.data['id']) + | |
45 | "/likes", | |
46 | data=data, | |
47 | headers={'accept': 'application/json'}) | |
48 | return r.json() | |
49 | ||
50 | def rmlike(self): | |
51 | """This function removes a like from a post | |
52 | ||
53 | """ | |
54 | ||
55 | data = {'authenticity_token': self._client.get_token()} | |
56 | ||
ae221396 | 57 | r = self._client.session.delete(self._client.pod + '/posts/' + |
a993a4b6 MK |
58 | str(self.data['id']) + |
59 | '/likes/' + | |
ae221396 MK |
60 | str(self.data['interactions'] |
61 | ['likes'][0]['id']), | |
a993a4b6 MK |
62 | data=data) |
63 | ||
64 | def reshare(self): | |
65 | """This function reshares a post | |
66 | ||
67 | """ | |
68 | ||
69 | data = {'root_guid': self.data['guid'], | |
70 | 'authenticity_token': self._client.get_token()} | |
71 | ||
72 | r = self._client.session.post(self._client.pod + | |
73 | "/reshares", | |
74 | data=data) | |
75 | ||
76 | return r.json() | |
77 | ||
78 | def comment(self, text): | |
79 | """This function comments on a post | |
80 | ||
81 | :param post_id: id of the post to comment on. | |
82 | :type post_id: str | |
83 | :param text: text to comment. | |
84 | :type text: str | |
85 | ||
86 | """ | |
87 | ||
88 | data = {'text': text, | |
89 | 'authenticity_token': self._client.get_token()} | |
90 | ||
ae221396 MK |
91 | r = self._client.session.post(self._client.pod + |
92 | '/posts/' + | |
93 | str(self.data['id']) + | |
94 | '/comments', | |
95 | data=data) | |
a993a4b6 MK |
96 | |
97 | return r.json() | |
ae221396 | 98 | |
a993a4b6 MK |
99 | def rmcomment(self, comment_id): |
100 | """This function removes a comment from a post | |
101 | ||
102 | :param post_id: id of the post to remove the like from. | |
103 | :type post_id: str | |
104 | :param like_id: id of the like to remove. | |
105 | :type like_id: str | |
106 | ||
107 | """ | |
108 | ||
109 | data = {'authenticity_token': self._client.get_token()} | |
110 | ||
ae221396 MK |
111 | r = self._client.session.delete(self._client.pod + '/posts/' + |
112 | str(self.data['id']) + | |
113 | '/comments/' + | |
114 | comment_id, | |
115 | data=data) |