-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathuser.py
More file actions
67 lines (49 loc) · 2.01 KB
/
Copy pathuser.py
File metadata and controls
67 lines (49 loc) · 2.01 KB
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
import requests
import json
from uri import URI, ME_URI
import code
class user(object):
def auth(self, user, password):
# This is how you will authenticate your Write.as account and retreive an access token for future requests
data = {"alias": user, "pass": password }
r = requests.post(URI, data=json.dumps(data),
headers={"Content-Type":"application/json"})
if r.status_code != 200:
return "Error in login(): %s" % r.json()["error_msg"]
else:
user = r.json()["data"]
return user
def authout(self, token):
r = requests.delete("https://write.as/api/auth/me",
headers={"Authorization": "Token %s" % token})
if r.status_code != 204:
return "Error in logout(): %s" % r.json()["error_msg"]
else:
return "You are logged out!"
def getPosts(self, token):
p = requests.get(ME_URI + "/posts",
headers={"Authorization":"Token %s" % token,
"Content-Type":"application/json"})
if p.status_code != 200:
return "Error in retrievePosts(): %s" % p.json()["error_msg"]
else:
uposts = p.json()["data"]
return uposts
def getCollections(self, token):
c = requests.get(ME_URI + "/collections",
headers={"Authorization":"Token %s" % token,
"Content-Type":"application/json"})
if c.status_code != 200:
return "Error in retrieveCollections(): %s" % c.json()["error_msg"]
else:
ucollections = c.json()["data"]
return ucollections
def getChannels(self, token):
c = requests.get(ME_URI + "/channels",
headers={"Authorization":"Token %s" % token,
"Content-Type":"application/json"})
if c.status_code != 200:
return "Error in retrieveChannels(): %s" % c.json()["error_msg"]
else:
uchannels = c.json()["data"]
return uchannels