Skip to content

Commit eea4118

Browse files
committed
Add create_post and edit_post
1 parent 1d6e925 commit eea4118

File tree

1 file changed

+72
-0
lines changed

1 file changed

+72
-0
lines changed

loading_api_wrapper/api.py

Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
1+
from concurrent.futures import thread
12
import math
3+
from pydoc import resolve
24

35
import requests
46

@@ -203,3 +205,73 @@ def get_editorials(self, page=None, post_type=None, sort=None):
203205
return {"code": 404, "post": data}
204206

205207
return {"code": 200, "post": data}
208+
209+
def create_post(self, thread_id, message):
210+
if not thread_id:
211+
return {"code": 400, "message": '"thread_id" is not allowed to be empty'}
212+
213+
url = f"{API_URL}/{API_VERSION}/posts/{thread_id}"
214+
headers = {
215+
"User-Agent": USER_AGENT,
216+
"content-type": "application/x-www-form-urlencoded",
217+
}
218+
data = {"body": message}
219+
response = requests.post(
220+
url,
221+
headers=headers,
222+
data=data,
223+
cookies=self._cookies,
224+
)
225+
226+
# Has no auth token.
227+
if response.status_code == 401:
228+
return response.json()
229+
230+
# Post id doesn't exist.
231+
if response.status_code == 404:
232+
return response.json()
233+
234+
if response.status_code == 201:
235+
return {
236+
"code": response.status_code,
237+
"message": "Post created",
238+
"data": response.json(),
239+
}
240+
241+
# Handle any other unknown status code.
242+
return response.json()
243+
244+
def edit_post(self, post_id, message):
245+
if not message:
246+
return {"code": 400, "message": '"message" is not allowed to be empty'}
247+
248+
url = f"{API_URL}/{API_VERSION}/posts/{post_id}"
249+
headers = {
250+
"User-Agent": USER_AGENT,
251+
"content-type": "application/x-www-form-urlencoded",
252+
}
253+
data = {"body": message}
254+
response = requests.patch(
255+
url,
256+
headers=headers,
257+
data=data,
258+
cookies=self._cookies,
259+
)
260+
261+
# Has no auth token.
262+
if response.status_code == 401:
263+
return response.json()
264+
265+
# Post id doesn't exist.
266+
if response.status_code == 404:
267+
return response.json()
268+
269+
if response.status_code == 200:
270+
return {
271+
"code": response.status_code,
272+
"message": "Post updated",
273+
"data": response.json(),
274+
}
275+
276+
# Handle any other unknown status code.
277+
return response.json()

0 commit comments

Comments
 (0)