This repository has been archived by the owner on May 10, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
api.py
144 lines (92 loc) · 3.35 KB
/
api.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
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
from flask import Flask, request
from HighLow import HighLow
from HighLowList import HighLowList
import requests
import Helpers
import json
#Create a Flask app instance
app = Flask(__name__)
#MySQL credentials
mysql_config = Helpers.read_json_from_file("config/mysql_config.json")
host = mysql_config["host"]
username = mysql_config["username"]
password = mysql_config["password"]
database = mysql_config["database"]
@app.route("/set/<string:highlowid>", methods=["POST"])
def setproperty(highlowid):
#Verify auth token
token = request.headers["Authentication"].replace("Bearer ", "")
verification = Helpers.verify_token(token)
if 'error' in verification:
return verification
else:
uid = verification["uid"]
high = request.form.get("high") or ""
low = request.form.get("low") or ""
highlow = None
if "highlowid" in request.form:
highlow = HighLow(host, username, password, database, request.form["highlowid"])
highlow.update(high, low)
else:
highlow = HighLow(host, username, password, database)
highlow.create(uid, high, low)
@app.route("/like/<string:highlowid>", methods=["POST"])
def like(highlowid):
#Verify auth token
token = request.headers["Authentication"].replace("Bearer ", "")
verification = Helpers.verify_token(token)
if 'error' in verification:
return verification
else:
uid = verification["uid"]
if 'highlowid' in request.form:
highlow = HighLow(host, username, password, database, request.form["highlowid"])
highlow.like(uid)
else:
return json.dumps({'error':'Must provide HighLow ID'})
@app.route("/comment/<string:highlowid>", methods=["POST"])
def comment(highlowid):
#Verify auth token
token = request.headers["Authentication"].replace("Bearer ", "")
verification = Helpers.verify_token(token)
if 'error' in verification:
return verification
else:
uid = verification["uid"]
message = request.form.get("message") or ""
if 'highlowid' in request.form:
highlow = HighLow(host, username, password, database, request.form["highlowid"])
highlow.comment(uid, message)
else:
return json.dumps({'error':'Must provide HighLow ID'})
#TODO: Add endpoints for getting specific highlows, getting all highlows for user and sorting, etc.
#Those endpoints will make use of the "HighLowList" class
@app.route("/get/today", methods=["GET"])
def get_today():
#Verify auth token
token = request.headers["Authentication"].replace("Bearer ", "")
verification = Helpers.verify_token(token)
if 'error' in verification:
return verification
else:
uid = verification["uid"]
#Now, we use `HighLowList` to get today's highlow
highlowlist = HighLowList(host, username, password, database)
today_highlow = highlowlist.get_today_for_user(uid)
return json.dumps(today_highlow)
@app.route("/get/user", methods=["GET"])
def get_user():
#Verify auth token
token = request.headers["Authentication"].replace("Bearer ", "")
verification = Helpers.verify_token(token)
if 'error' in verification:
return verification
else:
#Defaults to the current user
uid = request.args.get("uid") or verification["uid"]
highlowlist = HighLowList(host, username, password, database)
highlows = highlowlist.get_highlows_for_user(uid, sortby=request.args.get("sortby"), limit=request.args.get("limit"))
return json.dumps(highlows)
#Run the app
if __name__ == '__main__':
app.run(debug=True)