feat: add api of body record
continuous-integration/drone/push Build is passing
Details
continuous-integration/drone/push Build is passing
Details
This commit is contained in:
parent
5a83cea081
commit
c30c644f7c
|
@ -2,12 +2,14 @@ from apiflask import APIBlueprint
|
|||
|
||||
from app.api.v1.api import api
|
||||
from app.api.v1.auth import auth
|
||||
from app.api.v1.record import record
|
||||
|
||||
|
||||
def create_v1():
|
||||
bp_v1 = APIBlueprint('v1', __name__)
|
||||
bp_v1.register_blueprint(api, url_prefix='/api')
|
||||
bp_v1.register_blueprint(auth, url_prefix='/auth')
|
||||
bp_v1.register_blueprint(record, url_prefix='/record')
|
||||
return bp_v1
|
||||
|
||||
|
||||
|
|
|
@ -1,6 +1,7 @@
|
|||
from apiflask import APIBlueprint
|
||||
|
||||
from app import rpc
|
||||
from app.api.v1.exception.auth import WechatLoginError, AuthError
|
||||
from app.api.v1.schema.auth import WechatLoginIn, WechatLoginOut, RefreshTokenIn, RefreshTokenOut
|
||||
|
||||
auth = APIBlueprint('auth', __name__)
|
||||
|
@ -13,8 +14,7 @@ def wechat_login(json_data):
|
|||
try:
|
||||
result = rpc.admin.wechat_login(json_data['code'])
|
||||
except Exception as e:
|
||||
# TODO 返回 apiflask 的 json 格式的 error
|
||||
raise e
|
||||
raise WechatLoginError()
|
||||
return result
|
||||
|
||||
|
||||
|
@ -25,6 +25,5 @@ def refresh_token(query_data):
|
|||
try:
|
||||
result = rpc.admin.refresh_token(query_data['refresh_token'])
|
||||
except Exception as e:
|
||||
# TODO 返回 apiflask 的 json 格式的 error
|
||||
raise e
|
||||
raise AuthError()
|
||||
return result
|
|
@ -0,0 +1,12 @@
|
|||
from apiflask import HTTPError
|
||||
|
||||
|
||||
class AuthError(HTTPError):
|
||||
status_code = 403
|
||||
message = '请登陆'
|
||||
|
||||
|
||||
class WechatLoginError(HTTPError):
|
||||
status_code = 500
|
||||
message = '微信登录失败'
|
||||
|
|
@ -0,0 +1,11 @@
|
|||
from apiflask import HTTPError
|
||||
|
||||
|
||||
class AddBodyRecordError(HTTPError):
|
||||
status_code = 500
|
||||
message = 'add body record failed.'
|
||||
|
||||
|
||||
class BodyRecordNotFound(HTTPError):
|
||||
status_code = 404
|
||||
message = '找不到该记录'
|
|
@ -0,0 +1,46 @@
|
|||
from apiflask import APIBlueprint
|
||||
from flask import session
|
||||
|
||||
from app import rpc
|
||||
from app.api.v1.exception.record import AddBodyRecordError, BodyRecordNotFound
|
||||
from app.api.v1.schema.record import BodyRecordIn, BodyRecordOut, BodyRecordsIn, BodyRecordsOut
|
||||
from app.util.common import login_required
|
||||
|
||||
record = APIBlueprint('record', __name__)
|
||||
|
||||
|
||||
@record.post('/')
|
||||
@record.input(BodyRecordIn)
|
||||
@login_required
|
||||
def add_record(json_data):
|
||||
try:
|
||||
rpc.body_record.add(user_obj_id=session['user_id'],
|
||||
height=json_data['height'],
|
||||
weight=json_data['weight'],
|
||||
bmi=json_data['bmi'])
|
||||
except Exception as e:
|
||||
raise AddBodyRecordError()
|
||||
return {'msg': 'add body record success'}
|
||||
|
||||
|
||||
@record.get('/<record_id>')
|
||||
@record.output(BodyRecordOut)
|
||||
@login_required
|
||||
def get_record(record_id):
|
||||
try:
|
||||
result = rpc.body_record.get_one(record_id)
|
||||
except Exception as e:
|
||||
raise BodyRecordNotFound()
|
||||
return result
|
||||
|
||||
|
||||
@record.get('/list')
|
||||
@record.input(BodyRecordsIn, location='query')
|
||||
@record.output(BodyRecordsOut)
|
||||
@login_required
|
||||
def get_record(query_data):
|
||||
try:
|
||||
result = rpc.body_record.get_all_by_user(query_data['user_id'])
|
||||
except Exception as e:
|
||||
raise BodyRecordNotFound()
|
||||
return result
|
|
@ -0,0 +1,25 @@
|
|||
from apiflask import Schema
|
||||
from apiflask import fields
|
||||
|
||||
|
||||
class BodyRecordIn(Schema):
|
||||
height = fields.Float()
|
||||
weight = fields.Float()
|
||||
bmi = fields.Float()
|
||||
|
||||
|
||||
class BodyRecordOut(Schema):
|
||||
record_id = fields.String(attribute='_id')
|
||||
height = fields.Float()
|
||||
weight = fields.Float()
|
||||
bmi = fields.Float()
|
||||
create_time = fields.String()
|
||||
update_time = fields.String(allow_none=True)
|
||||
|
||||
|
||||
class BodyRecordsIn(Schema):
|
||||
user_id = fields.String()
|
||||
|
||||
|
||||
class BodyRecordsOut(Schema):
|
||||
records = fields.Nested(BodyRecordOut)
|
|
@ -5,6 +5,8 @@ from functools import wraps
|
|||
|
||||
from flask import request, session
|
||||
|
||||
from app.api.v1.exception.auth import AuthError
|
||||
|
||||
|
||||
def split_group(dict_list, key):
|
||||
dict_list.sort(key=itemgetter(key))
|
||||
|
@ -28,12 +30,10 @@ def login_required(f):
|
|||
def wrapper(*args, **kwargs):
|
||||
token = request.headers.get("Authorization", default=None)
|
||||
if not token:
|
||||
# TODO raise apiflask 的 json 格式的 error
|
||||
return '请登陆'
|
||||
raise AuthError()
|
||||
user_id = rpc.admin.identify(token)
|
||||
if not user_id:
|
||||
# TODO raise apiflask 的 json 格式的 error
|
||||
return '请登陆'
|
||||
raise AuthError()
|
||||
session['user_id'] = user_id
|
||||
return f(*args, **kwargs)
|
||||
return wrapper
|
||||
|
|
Loading…
Reference in New Issue