API: refactor
This commit is contained in:
parent
8bd0d4c26e
commit
8b186c2e41
@ -66,13 +66,10 @@ def get_activity(auth_user_id, activity_id):
|
|||||||
return jsonify(response_object), code
|
return jsonify(response_object), code
|
||||||
|
|
||||||
|
|
||||||
@activities_blueprint.route(
|
def get_activity_data(auth_user_id, activity_id, data_type):
|
||||||
'/activities/<int:activity_id>/gpx', methods=['GET']
|
"""Get chart data from an activity gpx file"""
|
||||||
)
|
|
||||||
@authenticate
|
|
||||||
def get_activity_gpx(auth_user_id, activity_id):
|
|
||||||
"""Get gpx file for an activity"""
|
|
||||||
activity = Activity.query.filter_by(id=activity_id).first()
|
activity = Activity.query.filter_by(id=activity_id).first()
|
||||||
|
content = ''
|
||||||
if activity:
|
if activity:
|
||||||
if not activity.gpx or activity.gpx == '':
|
if not activity.gpx or activity.gpx == '':
|
||||||
response_object = {
|
response_object = {
|
||||||
@ -82,83 +79,52 @@ def get_activity_gpx(auth_user_id, activity_id):
|
|||||||
return jsonify(response_object), 400
|
return jsonify(response_object), 400
|
||||||
|
|
||||||
try:
|
try:
|
||||||
|
if data_type == 'chart':
|
||||||
|
content = get_chart_data(activity.gpx)
|
||||||
|
else: # data_type == 'gpx'
|
||||||
with open(activity.gpx, encoding='utf-8') as f:
|
with open(activity.gpx, encoding='utf-8') as f:
|
||||||
gpx_content = f.read()
|
content = f.read()
|
||||||
except Exception as e:
|
except Exception as e:
|
||||||
appLog.error(e)
|
appLog.error(e)
|
||||||
response_object = {
|
response_object = {'status': 'error',
|
||||||
'status': 'error',
|
|
||||||
'message': 'internal error',
|
'message': 'internal error',
|
||||||
'data': {
|
'data': ({'chart_data': content}
|
||||||
'gpx': ''
|
if data_type == 'chart'
|
||||||
}
|
else {'gpx': content})}
|
||||||
}
|
|
||||||
return jsonify(response_object), 500
|
return jsonify(response_object), 500
|
||||||
|
|
||||||
status = 'success'
|
status = 'success'
|
||||||
message = ''
|
message = ''
|
||||||
code = 200
|
code = 200
|
||||||
else:
|
else:
|
||||||
gpx_content = ''
|
|
||||||
status = 'not found'
|
status = 'not found'
|
||||||
message = f'Activity not found (id: {activity_id})'
|
message = f'Activity not found (id: {activity_id})'
|
||||||
code = 404
|
code = 404
|
||||||
|
|
||||||
response_object = {
|
response_object = {'status': status,
|
||||||
'status': status,
|
|
||||||
'message': message,
|
'message': message,
|
||||||
'data': {
|
'data': ({'chart_data': content}
|
||||||
'gpx': gpx_content
|
if data_type == 'chart'
|
||||||
}
|
else {'gpx': content})}
|
||||||
}
|
|
||||||
return jsonify(response_object), code
|
return jsonify(response_object), code
|
||||||
|
|
||||||
|
|
||||||
|
@activities_blueprint.route(
|
||||||
|
'/activities/<int:activity_id>/gpx', methods=['GET']
|
||||||
|
)
|
||||||
|
@authenticate
|
||||||
|
def get_activity_gpx(auth_user_id, activity_id):
|
||||||
|
"""Get gpx file for an activity"""
|
||||||
|
return get_activity_data(auth_user_id, activity_id, 'gpx')
|
||||||
|
|
||||||
|
|
||||||
@activities_blueprint.route(
|
@activities_blueprint.route(
|
||||||
'/activities/<int:activity_id>/chart_data', methods=['GET']
|
'/activities/<int:activity_id>/chart_data', methods=['GET']
|
||||||
)
|
)
|
||||||
@authenticate
|
@authenticate
|
||||||
def get_activity_chart_data(auth_user_id, activity_id):
|
def get_activity_chart_data(auth_user_id, activity_id):
|
||||||
"""Get chart data from an activity gpx file"""
|
"""Get chart data from an activity gpx file"""
|
||||||
activity = Activity.query.filter_by(id=activity_id).first()
|
return get_activity_data(auth_user_id, activity_id, 'chart')
|
||||||
if activity:
|
|
||||||
if not activity.gpx or activity.gpx == '':
|
|
||||||
response_object = {
|
|
||||||
'status': 'fail',
|
|
||||||
'message': f'No gpx file for this activity (id: {activity_id})'
|
|
||||||
}
|
|
||||||
return jsonify(response_object), 400
|
|
||||||
|
|
||||||
try:
|
|
||||||
chart_content = get_chart_data(activity.gpx)
|
|
||||||
except Exception as e:
|
|
||||||
appLog.error(e)
|
|
||||||
response_object = {
|
|
||||||
'status': 'error',
|
|
||||||
'message': 'internal error',
|
|
||||||
'data': {
|
|
||||||
'chart_data': ''
|
|
||||||
}
|
|
||||||
}
|
|
||||||
return jsonify(response_object), 500
|
|
||||||
|
|
||||||
status = 'success'
|
|
||||||
message = ''
|
|
||||||
code = 200
|
|
||||||
else:
|
|
||||||
chart_content = ''
|
|
||||||
status = 'not found'
|
|
||||||
message = f'Activity not found (id: {activity_id})'
|
|
||||||
code = 404
|
|
||||||
|
|
||||||
response_object = {
|
|
||||||
'status': status,
|
|
||||||
'message': message,
|
|
||||||
'data': {
|
|
||||||
'chart_data': chart_content
|
|
||||||
}
|
|
||||||
}
|
|
||||||
return jsonify(response_object), code
|
|
||||||
|
|
||||||
|
|
||||||
@activities_blueprint.route('/activities', methods=['POST'])
|
@activities_blueprint.route('/activities', methods=['POST'])
|
||||||
|
Loading…
Reference in New Issue
Block a user