-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.py
141 lines (116 loc) · 4.5 KB
/
app.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
from authlib.integrations.flask_oauth2 import ResourceProtector
from authlib.integrations.flask_oauth2 import current_token
from authlib.integrations.sqla_oauth2 import create_bearer_token_validator
from dotenv import load_dotenv
from flask import Flask, request, jsonify
from models import db, OAuth2Token, HealthData
load_dotenv()
require_oauth = ResourceProtector()
# protect resource
bearer_cls = create_bearer_token_validator(db.session, OAuth2Token)
require_oauth.register_token_validator(bearer_cls())
app = Flask(__name__)
# load default configuration
app.config.from_object('settings')
# Initialize the database
db.init_app(app)
@app.route('/health-data')
def get_health_data_list():
try:
with require_oauth.acquire() as token:
user = token.user
health_data_list = HealthData.query.with_entities(
HealthData.id,
HealthData.data,
HealthData.created_at
).filter_by(author_id=user.id).all()
return {
'data': [{
'id': health_data.id,
'data': health_data.data,
'created_at': health_data.created_at
} for health_data in health_data_list]
}
except:
return {
"error": "invalid_token",
"error_description": "The access token provided is expired, revoked, malformed, or invalid for other reasons."
}, 401
@app.route('/workout-program', methods=['POST'])
def add_workout_program():
try:
with require_oauth.acquire() as token:
user = token.user
data = request.get_json()
# Create a new health data record for the workout program
workout_program = HealthData(
type='workout_program',
data=data,
author_id=user.id
)
db.session.add(workout_program)
db.session.commit()
return {
'id': workout_program.id,
'data': workout_program.data,
'created_at': workout_program.created_at
}, 201
except:
return {
"error": "invalid_token",
"error_description": "The access token provided is expired, revoked, malformed, or invalid for other reasons."
}, 401
@app.route('/workout-program', methods=['GET'])
def get_workout_programs():
try:
with require_oauth.acquire() as token:
user = token.user
workout_programs = HealthData.query.with_entities(
HealthData.id,
HealthData.data,
HealthData.created_at
).filter_by(author_id=user.id, type='workout_program').all()
return {
'data': [{
'id': program.id,
'data': program.data,
'created_at': program.created_at
} for program in workout_programs]
}
except:
return {
"error": "invalid_token",
"error_description": "The access token provided is expired, revoked, malformed, or invalid for other reasons."
}, 401
@app.route('/workout-program/<program_id>', methods=['PUT'])
def update_workout_program(program_id):
try:
with require_oauth.acquire() as token:
user = token.user
data = request.get_json()
# Find the workout program by ID and ensure it belongs to the current user
workout_program = HealthData.query.filter_by(
id=program_id,
author_id=user.id,
type='workout_program'
).first()
if not workout_program:
return {
"error": "not_found",
"error_description": "Workout program not found or you don't have permission to modify it."
}, 404
# Update the workout program data
workout_program.data = data
db.session.commit()
return {
'id': workout_program.id,
'data': workout_program.data,
'created_at': workout_program.created_at
}
except:
return {
"error": "invalid_token",
"error_description": "The access token provided is expired, revoked, malformed, or invalid for other reasons."
}, 401
if __name__ == '__main__':
app.run()