-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathposts.py
82 lines (60 loc) · 2.4 KB
/
posts.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
from uri import POST_URI
import requests
import json
import code
class post(object):
def __init__(self):
pass
def get(self, id):
p = requests.get(POST_URI + "/%s" % id,
headers={"Content-Type":"application/json"})
if p.status_code != 200:
return "Error in retrievePost(): %s" % p.json()["error_msg"]
else:
post = p.json()["data"]
return post
def update(self, token, id, **kwargs):
data = json.dumps(kwargs)
p = requests.post(POST_URI + "/%s" % id, data=data,
headers={"Authorization": "Token %s" % token,
"Content-Type":"application/json"})
if p.status_code != 200:
return "Error in updatePost(): %s" % p.json()["error_msg"]
else:
post = p.json()
return post
def post(self, token, body, title, **kwargs):
d = {"body": body,
"title": title}
k = kwargs
data = {**d, **k}
p = requests.post(POST_URI, data=json.dumps(data),
headers={"Authorization": "Token %s" % token,
"Content-Type":"application/json"})
if p.status_code != 201:
return "Error in createPost(): %s" % p.json()["error_msg"]
else:
post = p.json()["data"]
return post
def delete(self, token, id):
p = requests.delete(POST_URI + "/%s" % id,
headers={"Authorization": "Token %s" % token,
"Content-Type": "application/json"})
if p.status_code != 204:
return "Error in deletePost(): Invalid token or post doesn't exist under your account."
# The error msg that API gives is not helpful...so created a new one
# 'This is an unhelpful error message for a miscellaneous internal error.'
# return "Error in deletePost(): %s" % p.json()["error_msg"]
else:
return "Post deleted!"
def claim(self, token, id, ptoken):
data = [{"id": id,
"token": ptoken}]
p = requests.post(POST_URI + "/claim", data=json.dumps(data),
headers={"Authorization": "Token %s" % token,
"Content-Type": "application/json"})
if p.status_code != 200:
return "Error in claimPost(): %s" % p.json()["error_msg"]
else:
post = p.json()["data"]
return post