当对我的一个 API 的调用未返回任何结果时,我正在努力寻找一种返回响应的方法。我正在使用flask_restplus,并尝试使用@api.errorhandler 注释但没有成功,然后我在课堂上尝试了一个普通的try/except 块。
在这个特定问题中,我试图调用端点,其中 GUID 是数据库中不存在的用户。
http://localhost:5000/api/users/b3d8e86b-f2ad-4b6a-b768-e7adc1d94ced
用户端点定义如下:
import logging
from flask import Response
from flask_restplus import Resource, fields
from neomodel.core import DoesNotExist
from app.api.restplus import api
from app.models import User
log = logging.getLogger(__name__)
ns = api.namespace('users', description='Users')
user = api.model('user', {
'id': fields.Integer(readOnly=True, description='The internal id of of a user'),
'user_id': fields.String(required=True, description='The user unique identifier')}
)
@ns.route('/')
@api.response(404, 'Users not found.')
class UsersList(Resource):
@api.marshal_list_with(user)
def get(self):
"""
Returns a list of all users
:return: list_of_users
"""
try:
users = User.nodes
list_of_users = list(users)
return list_of_users
# return json.dumps(dict(users = [user.serialize for user in list_of_users]))
except DoesNotExist:
return Response(('{"No Content": "No user nodes found"}'), status = 200, mimetype = 'application/json')
@ns.route('/<string:user_id>')
class UserItem(Resource):
@api.marshal_with(user)
def get(self, user_id):
"""
Returns a user with the given user_id
:param id: user_id
:return: user
"""
try:
user = User.nodes.get(user_id=user_id)
return user
except User.DoesNotExist:
return Response({'message': 'Could not locate a user with the user_id provided.'}, 404)
我的初始化是在 app/ init .py 中完成的:
import logging.config
from flask import Flask, Blueprint
from neomodel import clear_neo4j_database
from neomodel import db
from app.config import Configuration
from app.web_app.routes import webapp_mod
from app.data_loader.routes import dataloader_mod
from app.utilities import prepare_rerun
from app.api.endpoints.users import ns as users_namespace
from app.api.endpoints.sessions import ns as sessions_namespace
from app.api.endpoints.browsers import ns as browsers_namespace
from app.api.endpoints.os import ns as os_namespace
from app.api.endpoints.applications import ns as applications_namespace
from app.api.endpoints.tenants import ns as tenants_namepspace
from app.api.endpoints.devices import ns as devices_namespace
from app.api.endpoints.environments import ns as environments_namespace
from app.api.restplus import api
from os import path
app = Flask(__name__)
app.config.from_object(Configuration)
app.register_blueprint(dataloader_mod, url_prefix='/data_loader')
log_file_path = path.join(path.dirname(path.abspath(__file__)), 'logging.conf')
logging.config.fileConfig(log_file_path)
log = logging.getLogger(__name__)
blueprint = Blueprint('api', __name__, url_prefix='/api')
api.init_app(blueprint)
api.add_namespace(users_namespace)
api.add_namespace(sessions_namespace)
api.add_namespace(browsers_namespace)
api.add_namespace(applications_namespace)
api.add_namespace(tenants_namepspace)
api.add_namespace(devices_namespace)
api.add_namespace(os_namespace)
api.add_namespace(environments_namespace)
我此时定义 api 的 resplus.py 模块只有 api 对象的定义,但我曾尝试在网上遵循一些示例,并在其中定义通过 users 对象中的注释处理异常的方法。
from flask_restplus import Api
from neomodel.core import DoesNotExist
api = Api(version='1.0', title='Users Activity Log',
description='An API to retreive information about users'' activities in Infor Ming.le')
但是,当我拨打电话时,我得到的不是带有消息和 404 代码的 JSON 响应,而是:
{
"id": null,
"user_id": null
}
提前感谢您的帮助。