-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathexample_api.py
More file actions
124 lines (108 loc) · 3.54 KB
/
example_api.py
File metadata and controls
124 lines (108 loc) · 3.54 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
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
'''an example of using lcs_client for an api'''
from functools import wraps
from flask import Flask, jsonify, request
import lcs_client
app = Flask(__name__)
class DB:
'''
a fake database for example purposes.
stores our information and caches lcs info for easy retreval.
you don't really have to store lcs data locally, you could
technically call get_profile() every time you need data
'''
_data = {'lcs': {}, 'our': {}}
def get(self, collection, email):
'''access cached lcs or our info'''
return self._data[collection].get('email', None)
def set(self, collection, email, data):
'''update lcs cache or set our info'''
self._data[collection]['email'] = data
def __str__(self):
return str(self._data)
db = DB()
@lcs_client.on_login
def update_profile(profile):
'''
update their profile. if they don't have a profile then fill stuff in
and make note that we need more info
'''
first_time = db.get('lcs', profile['email']) == None
db.set('lcs', profile['email'], profile)
if first_time:
db.set('our', profile['email'], {
'needs_additional_info': True,
'todos': [],
'favorite_food': None
})
def require_user(f):
'''a decorator that gets a user object and passes it to the handler'''
@wraps(f)
def wrapper(*args, **kwargs):
if not request.is_json:
return jsonify({
'success': False,
'error': 'json body required'
}), 400
body = request.json
user = lcs_client.User(token=body['token'])
return f(*args, user=user, **kwargs)
return wrapper
@app.route('/profile', methods=['POST'])
@require_user
def profile(user=None):
lcs = db.get('lcs', user.email)
our = db.get('our', user.email)
return jsonify({
'success': True,
'profile': {
'email': lcs['email'],
'needs_additional_info': our['needs_additional_info'],
'todos': our['todos'],
'favorite_food': our['favorite_food'],
}
})
@app.route('/additional_info', methods=['POST'])
@require_user
def set_additional_info(user=None):
'''
the idea is that when the ui first loads the profile it will check if
we need to collect additional info. if we do
'''
our = db.get('our', user.email)
our['favorite_food'] = request.json['favorite_food']
our['needs_additional_info'] = False
db.set('our', user.email, our)
return jsonify({'success': True})
@app.route('/todo', methods=['POST'])
@require_user
def new_todo(user=None):
todo = request.json
our = db.get('our', user.email)
our['todos'].append({
'text': todo['text']
})
db.set('our', user.email, our)
return jsonify({'success': True, '_id': len(our['todos']) - 1})
@app.route('/todo', methods=['DELETE'])
@require_user
def resolve(user=None):
_id = request.json['_id']
our = db.get('our', user.email)
if len(our['todos']) <= _id:
return jsonify({'success': False, 'error': '_id does not exist'})
del our['todos'][_id]
db.set('our', user.email, our)
return jsonify({'success': True})
@app.errorhandler(lcs_client.CredentialError)
def unauth(e):
return jsonify({
'success': False,
'error': 'invalid credentials: ' + str(e),
}), 403
@app.errorhandler(Exception)
def handler(e):
print(type(e))
# idealy you'd also get the correct response code
return jsonify({'success': False, 'error': str(e)}), 400
if __name__ == '__main__':
app.run()