我正在尝试使用flask-restplus 在python 中构建一个restful API。我想让 swagger 文档位于与普通“/”不同的位置。
我正在关注此处的文档并已按照说明进行操作。我正在使用 python2.7.3 并具有以下代码~/dev/test/app.py:
from flask import Flask
from flask.ext.restplus import Api, apidoc
app = Flask(__name__)
api = Api(app, ui=False)
@api.route('/doc/', endpoint='doc')
def swagger_ui():
return apidoc.ui_for(api)
app.register_blueprint(apidoc.apidoc)
Run Code Online (Sandbox Code Playgroud)
当我尝试运行它时,python app.py我得到:
Traceback (most recent call last):
File "app.py", line 7 in <module>
@api.route('/doc/', endpoint='doc')
File "/home/logan/.virtualenvs/test/lib/python2.7/site-packages/flask_restplus/api.py", line 191, in wrapper
self.add_resources(cls, *urls, **kwargs)
File "/home/logan/.virtualenvs/test/lib/python2.7/site-packages/flask_restplus/api.py", line 175, in add_resource
super(Api, self).add_resource(resource, *urls, **kwargs)
File "/home/logan/.virtualenvs/test/lib/python2.7/site-packages/flask_restful/__init__.py", line 396, in add_resource
self._register_view(self.app, resource, …Run Code Online (Sandbox Code Playgroud) 我在flask-restplus的帮助下编写了一个简单的API:
from flask import Flask
from flask_restplus import Resource, Api
app = Flask(__name__) # Create a Flask WSGI application
api = Api(app) # Create a Flask-RESTPlus API
@api.route('/hello') # Create a URL route to this resource
class HelloWorld(Resource): # Create a RESTful resource
def get(self): # Create GET endpoint
return {'hello': 'world'}
if __name__ == '__main__':
app.run(debug=True)
Run Code Online (Sandbox Code Playgroud)
当我在浏览器中导航到时,loacalhost:5000/我得到了基本的 Swagger 文档,但我找不到在哪里可以获得 API 的机器可读的纯 yaml 表示形式,它不应该自动生成吗?
使用flask-restplus我注意到如果我处理一些错误,例如:
api.abort(code=404, message='Item not found')
Run Code Online (Sandbox Code Playgroud)
响应呈现:
{
"message": "Item not found. You have requested this URI [/item/1]
but did you mean /item/<item_id> ?"
}
Run Code Online (Sandbox Code Playgroud)
我真的不想要额外的信息,但找不到删除它的方法。
有吗?
嗨,下面是我的运行代码,可以通过以下 URL 访问:http : //127.0.0.1 : 5000/api/documentation
from flask import Flask, Blueprint
from flask_restplus import Api, Resource, fields
app = Flask(__name__)
blueprint = Blueprint('api', __name__, url_prefix='/api')
api = Api(blueprint, doc='/documentation') #,doc=False
app.register_blueprint(blueprint)
app.config['SWAGGER_UI_JSONEDITOR'] = True
login_details = api.model('LoginModel',{'user_name' : fields.String('The Username.'),'pass_word' : fields.String('The password.'),})
# pass_word = api.model('Pwd', {'pass_word' : fields.String('The password.')})
credentials = []
python = {'user_name' : '1234','pwd':'23213413'}
credentials.append(python)
@api.route('/login')
class Language(Resource):
@api.marshal_with(login_details, envelope='the_data',mask='pass_word')
def get(self):
return credentials
@api.expect(login_details)
@api.marshal_with(login_details, envelope='the_data',mask='pass_word')
def post(self):
login_details = api.payload
print(login_details) …Run Code Online (Sandbox Code Playgroud) 我刚刚开始使用flask-restplus进行开发,而且我不是母语人士,
但我会尽量清楚地描述我的问题。
我知道flask中有一个fields模块可以帮助我们定义和过滤响应数据类型,
如字符串、整数、列表等。
使用 fields 模块时有什么方法可以允许 NULL / None 吗?
以下是我使用字段模块捕获值的代码,
add_group = api.model(
"add_group",
{"team_groups": fields.List(fields.Nested(api.model("team_groups", {
"name": fields.String(example="chicago bulls", description="name of add group"),
"display_name": fields.String(example="bulls", description="display name of add group")})))})
Run Code Online (Sandbox Code Playgroud)
如果数据类型display_name不是String,则会出现以下错误,
{
"errors": {
"team_groups.0.display_name": "123 is not of type 'string'"
},
"message": "Input payload validation failed"
}
Run Code Online (Sandbox Code Playgroud)
我想要的是在输入display_name时,我可以输入bulls或None
我的问题,但最终转换为非空值来解决问题。
如果我的问题的任何部分不太清楚,
请告诉我,谢谢。
以下是我的开发环境:
flask-restplus 0.13.0
Python 3.7.4
postman 7.18.1
以下是我更新的代码:
add_group = api.model(
"add_group", …Run Code Online (Sandbox Code Playgroud) 我正在尝试使用Flask-Restplus制作一个api,并大张旗鼓地记录下来。
到目前为止,这是我所拥有的,并且工作正常,但不知道如何添加根路由。
from flask import Flask, Blueprint
from flask_restplus import Api, Resource, apidoc
app = Flask('__name__')
blueprint = Blueprint('v1', __name__, url_prefix='/rest/v1')
api = Api(blueprint, ui=False, version='1.0')
@blueprint.route('/apidoc/', endpoint='apidoc')
def swagger_ui():
return apidoc.ui_for(api)
@blueprint.route('/', endpoint='rootres')
def root():
return ''
app.register_blueprint(blueprint)
ns = api.namespace('test', description='desc')
@ns.route('/', endpoint='rootresource')
class RootResource(Resource)
def get(self):
...
Run Code Online (Sandbox Code Playgroud)
虽然/ rest / v1 / test工作正常,但/ rest / v1给我的页面找不到。
如果我这样修改:
@blueprint.route('/aaa', endpoint='rootres')
def root():
return ''
Run Code Online (Sandbox Code Playgroud)
然后/ rest / v1 / aaa就可以了。
问题:如何使@ blueprint.route('/')工作?
是否可以使用相同的 json_mapping 为对象提供递归映射?
示例:
person_json_mapping = api.model('Person', {
'name': fields.String(),
'date_of_birth': fields.Date(),
'parents': fields.List(fields.Nested(##person_json_mapping##))
}
Run Code Online (Sandbox Code Playgroud)
如何在 self 内部使用person_json_mapping?
您能否描述一下如何在Flask Restplus自动生成的文档中指明基本URL?
我正在运行以下代码,但没有显示在swagger UI中:
from flask import Flask
from flask_restplus import Api
app = Flask(__name__)
api = Api(app,
title='Data Quality Framework API',
version='v0.1',
doc='/data_quality_framework/api/documentation',
contact='me@xxx.com',
base_url='/test')
Run Code Online (Sandbox Code Playgroud)
我正在寻找将restplus Api.model表示为a的方式dictionary,我在诸如以下的调用数据上收到的api
{"name": "the_name",
"data": {"data_specific_key", "value", "key2": "value2" .... }
}
// The data is dynamic, no way to know what fields will have.
Run Code Online (Sandbox Code Playgroud)
不幸的是,在restplus Api中,我只有一种表示ListFields的方法。
我正在查看https://flask-restplus.readthedocs.io/en/stable/marshalling.html#nested-field,但不确定会有所帮助,也许我需要将https://flask-restplus.readthedocs子类化。 io / zh-CN / stable / api.html#flask_restplus.fields.Raw,请创建我自己的字段,谢谢。
我有一个大文件,我们称它为machine_learning_model.hdf5。每次点击后请求端点时,我都会将其加载到我的应用程序中。伪代码如下所示:
def post(self):
model = load_model('./machine_learning_model.hdf5')
return( model.predict())
Run Code Online (Sandbox Code Playgroud)
但是,每次碰到端点时我都在加载文件这一事实引起了问题。通常,在初始化时将大文件加载到Flask应用程序中的正确方法是什么,以便各个端点可以使用该文件中的代码。