本文整理汇总了Python中flask_restx.Api方法的典型用法代码示例。如果您正苦于以下问题:Python flask_restx.Api方法的具体用法?Python flask_restx.Api怎么用?Python flask_restx.Api使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类flask_restx
的用法示例。
在下文中一共展示了flask_restx.Api方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: create_app
# 需要导入模块: import flask_restx [as 别名]
# 或者: from flask_restx import Api [as 别名]
def create_app(env=None):
from flask_restx import Api, Namespace, Resource
app = Flask(__name__)
api = Api(app)
@app.route("/simple/make_a_widget", methods=["POST"])
@accepts(dict(name="some_arg", type=str), schema=WidgetSchema)
@responds(schema=WidgetSchema)
def post():
from flask import jsonify
return request.parsed_obj
@api.route("/restx/make_a_widget")
class WidgetResource(Resource):
@accepts(dict(name="some_arg", type=str), schema=WidgetSchema, api=api)
@responds(schema=WidgetSchema, api=api)
def post(self):
from flask import jsonify
return request.parsed_obj
return app
示例2: create_app
# 需要导入模块: import flask_restx [as 别名]
# 或者: from flask_restx import Api [as 别名]
def create_app(env=None):
from flask_restx import Api, Namespace, Resource
app = Flask(__name__)
api = Api(app)
@api.route("/restx/make_a_widget")
class DoodadResource(Resource):
@accepts(schema=DoodadSchema, api=api)
@responds(schema=DoodadSchema, api=api)
def post(self):
from flask import jsonify
return request.parsed_obj
return app
示例3: create_app
# 需要导入模块: import flask_restx [as 别名]
# 或者: from flask_restx import Api [as 别名]
def create_app():
app = Flask(__name__)
api = Api(app)
@api.route("/widget")
class WidgetResource(Resource):
@accepts("WidgetCreate", schema=WidgetSchema(only=["foo", "baz"]), api=api)
@responds(schema=WidgetSchema, api=api)
def post(self):
# save data to a new record, and return ID
return request.parsed_obj
@accepts(dict(name="id", type=int), schema=WidgetSchema, api=api)
@responds(schema=WidgetSchema(exclude=["created_at"]), api=api)
def patch(self):
return request.parsed_obj
return app
示例4: create_app
# 需要导入模块: import flask_restx [as 别名]
# 或者: from flask_restx import Api [as 别名]
def create_app(env=None):
from flask_restx import Api, Namespace, Resource
app = Flask(__name__)
api = Api(app)
@api.route("/restx/make_a_widget")
class WidgetResource(Resource):
@accepts(schema=WidgetSchema, api=api)
@responds(schema=WidgetSchema, api=api)
def post(self):
from flask import jsonify
return request.parsed_obj
return app
示例5: test_arguments_are_added_to_request_with_Resource_and_schema
# 需要导入模块: import flask_restx [as 别名]
# 或者: from flask_restx import Api [as 别名]
def test_arguments_are_added_to_request_with_Resource_and_schema(app, client): # noqa
class TestSchema(Schema):
_id = fields.Integer()
name = fields.String()
api = Api(app)
@api.route("/test")
class TestResource(Resource):
@accepts(
"Foo",
dict(name="foo", type=int, help="An important foo"),
schema=TestSchema,
api=api,
)
def post(self):
assert request.parsed_obj
assert request.parsed_obj["_id"] == 42
assert request.parsed_obj["name"] == "test name"
return "success"
with client as cl:
resp = cl.post("/test?foo=3", json={"_id": 42, "name": "test name"})
assert resp.status_code == 200
示例6: test_respond_schema_instance
# 需要导入模块: import flask_restx [as 别名]
# 或者: from flask_restx import Api [as 别名]
def test_respond_schema_instance(app, client): # noqa
class TestSchema(Schema):
_id = fields.Integer()
name = fields.String()
api = Api(app)
@api.route("/test")
class TestResource(Resource):
@responds(schema=TestSchema(), api=api)
def get(self):
obj = {"_id": 42, "name": "Jon Snow"}
return obj
with client as cl:
resp = cl.get("/test")
obj = resp.json
assert obj["_id"] == 42
assert obj["name"] == "Jon Snow"
示例7: test_respond_schema_instance_respects_exclude
# 需要导入模块: import flask_restx [as 别名]
# 或者: from flask_restx import Api [as 别名]
def test_respond_schema_instance_respects_exclude(app, client): # noqa
class TestSchema(Schema):
_id = fields.Integer()
name = fields.String()
api = Api(app)
@api.route("/test")
class TestResource(Resource):
@responds(schema=TestSchema(exclude=("_id",)), api=api)
def get(self):
obj = {"_id": 42, "name": "Jon Snow"}
return obj
with client as cl:
resp = cl.get("/test")
obj = resp.json
assert "_id" not in obj
assert obj["name"] == "Jon Snow"
示例8: test_respond_schema_respects_many
# 需要导入模块: import flask_restx [as 别名]
# 或者: from flask_restx import Api [as 别名]
def test_respond_schema_respects_many(app, client): # noqa
class TestSchema(Schema):
_id = fields.Integer()
name = fields.String()
api = Api(app)
@api.route("/test")
class TestResource(Resource):
@responds(schema=TestSchema, many=True, api=api)
def get(self):
obj = [{"_id": 42, "name": "Jon Snow"}]
return obj
with client as cl:
resp = cl.get("/test")
obj = resp.json
assert obj == [{"_id": 42, "name": "Jon Snow"}]
示例9: test_respond_schema_instance_respects_many
# 需要导入模块: import flask_restx [as 别名]
# 或者: from flask_restx import Api [as 别名]
def test_respond_schema_instance_respects_many(app, client): # noqa
class TestSchema(Schema):
_id = fields.Integer()
name = fields.String()
api = Api(app)
@api.route("/test")
class TestResource(Resource):
@responds(schema=TestSchema(many=True), api=api)
def get(self):
obj = [{"_id": 42, "name": "Jon Snow"}]
return obj
with client as cl:
resp = cl.get("/test")
obj = resp.json
assert obj == [{"_id": 42, "name": "Jon Snow"}]
示例10: test_responds_passes_raw_responses_through_untouched
# 需要导入模块: import flask_restx [as 别名]
# 或者: from flask_restx import Api [as 别名]
def test_responds_passes_raw_responses_through_untouched(app, client): # noqa
class TestSchema(Schema):
_id = fields.Integer()
name = fields.String()
api = Api(app)
@api.route("/test")
class TestResource(Resource):
@responds(schema=TestSchema, api=api)
def get(self):
from flask import make_response, Response
obj = {"_id": 42, "name": "Jon Snow"}
return Response("A prebuild response that won't be serialised", 201)
with client as cl:
resp = cl.get("/test")
assert resp.status_code == 201
示例11: test_responds_respects_status_code
# 需要导入模块: import flask_restx [as 别名]
# 或者: from flask_restx import Api [as 别名]
def test_responds_respects_status_code(app, client): # noqa
class TestSchema(Schema):
_id = fields.Integer()
name = fields.String()
api = Api(app)
@api.route("/test")
class TestResource(Resource):
@responds(schema=TestSchema, api=api, status_code=999)
def get(self):
from flask import make_response, Response
obj = {"_id": 42, "name": "Jon Snow"}
return obj
with client as cl:
resp = cl.get("/test")
assert resp.status_code == 999
示例12: test_responds_respects_envelope
# 需要导入模块: import flask_restx [as 别名]
# 或者: from flask_restx import Api [as 别名]
def test_responds_respects_envelope(app, client): # noqa
class TestSchema(Schema):
_id = fields.Integer()
name = fields.String()
api = Api(app)
@api.route("/test")
class TestResource(Resource):
@responds(schema=TestSchema, api=api, envelope='test-data')
def get(self):
from flask import make_response, Response
obj = {"_id": 42, "name": "Jon Snow"}
return obj
with client as cl:
resp = cl.get("/test")
assert resp.status_code == 200
assert resp.json == {'test-data': {'_id': 42, 'name': 'Jon Snow'}}
示例13: test_partial_param_for_schema_loading
# 需要导入模块: import flask_restx [as 别名]
# 或者: from flask_restx import Api [as 别名]
def test_partial_param_for_schema_loading(app, client): # noqa
class TestSchema(Schema):
foo = fields.Str(required=True)
bar = fields.Str(required=True)
api = Api(app)
@api.route("/test")
class TestResource(Resource):
@accepts(schema=TestSchema(), api=api, partial=True)
def post(self):
return "success"
with client as cl:
resp = cl.post("/test", json={"foo": 'foo', "bar": "bar"})
assert resp.status_code == 200
resp = cl.post("/test", json={"foo": 'foo'})
assert resp.status_code == 200
示例14: __init__
# 需要导入模块: import flask_restx [as 别名]
# 或者: from flask_restx import Api [as 别名]
def __init__(self, title=API_TITLE, desc=API_DESC, version=API_VERSION):
self.app = Flask(title, static_url_path='')
# load config
if os.path.exists("config.py"):
self.app.config.from_object("config")
self.api = Api(
self.app,
title=title,
description=desc,
version=version)
self.api.namespaces.clear()
self.api.add_namespace(MAX_API)
# enable cors if flag is set
if os.getenv('CORS_ENABLE') == 'true' and \
(os.environ.get('WERKZEUG_RUN_MAIN') == 'true' or self.app.debug is not True):
CORS(self.app, origins='*')
print('NOTE: MAX Model Server is currently allowing cross-origin requests - (CORS ENABLED)')
示例15: create_app
# 需要导入模块: import flask_restx [as 别名]
# 或者: from flask_restx import Api [as 别名]
def create_app(env=None):
from app.config import config_by_name
from app.routes import register_routes
app = Flask(__name__)
app.config.from_object(config_by_name[env or "test"])
api = Api(app, title="Flaskerific API", version="0.1.0")
register_routes(api, app)
db.init_app(app)
@app.route("/health")
def health():
return jsonify("healthy")
return app