本文整理汇总了Python中connexion.App.add_api方法的典型用法代码示例。如果您正苦于以下问题:Python App.add_api方法的具体用法?Python App.add_api怎么用?Python App.add_api使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类connexion.App
的用法示例。
在下文中一共展示了App.add_api方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: test_validator_map
# 需要导入模块: from connexion import App [as 别名]
# 或者: from connexion.App import add_api [as 别名]
def test_validator_map(json_validation_spec_dir, spec):
def validate_type(validator, types, instance, schema):
types = _utils.ensure_list(types)
errors = Draft4RequestValidator.VALIDATORS['type'](validator, types, instance, schema)
for e in errors:
yield e
if 'string' in types and 'minLength' not in schema:
errors = Draft4RequestValidator.VALIDATORS['minLength'](validator, 1, instance, schema)
for e in errors:
yield e
MinLengthRequestValidator = extend(Draft4RequestValidator, {'type': validate_type})
class MyRequestBodyValidator(RequestBodyValidator):
def __init__(self, *args, **kwargs):
super(MyRequestBodyValidator, self).__init__(*args, validator=MinLengthRequestValidator, **kwargs)
validator_map = {'body': MyRequestBodyValidator}
app = App(__name__, specification_dir=json_validation_spec_dir)
app.add_api(spec, validate_responses=True, validator_map=validator_map)
app_client = app.app.test_client()
res = app_client.post('/v1.0/minlength', data=json.dumps({'foo': 'bar'}), content_type='application/json') # type: flask.Response
assert res.status_code == 200
res = app_client.post('/v1.0/minlength', data=json.dumps({'foo': ''}), content_type='application/json') # type: flask.Response
assert res.status_code == 400
示例2: test_no_swagger_json_api
# 需要导入模块: from connexion import App [as 别名]
# 或者: from connexion.App import add_api [as 别名]
def test_no_swagger_json_api(simple_api_spec_dir):
""" Verify the swagger.json file is not returned when set to False when adding api. """
app = App(__name__, port=5001, specification_dir=simple_api_spec_dir, debug=True)
app.add_api('swagger.yaml', swagger_json=False)
app_client = app.app.test_client()
swagger_json = app_client.get('/v1.0/swagger.json') # type: flask.Response
assert swagger_json.status_code == 404
示例3: test_swagger_json_api
# 需要导入模块: from connexion import App [as 别名]
# 或者: from connexion.App import add_api [as 别名]
def test_swagger_json_api(simple_api_spec_dir):
""" Verify the swagger.json file is returned for default setting passed to api. """
app = App(__name__, port=5001, specification_dir=simple_api_spec_dir, debug=True)
app.add_api('swagger.yaml')
app_client = app.app.test_client()
swagger_json = app_client.get('/v1.0/swagger.json') # type: flask.Response
assert swagger_json.status_code == 200
示例4: test_no_swagger_json_api
# 需要导入模块: from connexion import App [as 别名]
# 或者: from connexion.App import add_api [as 别名]
def test_no_swagger_json_api(simple_api_spec_dir, spec):
""" Verify the spec json file is not returned when set to False when adding api. """
app = App(__name__, port=5001, specification_dir=simple_api_spec_dir, debug=True)
app.add_api(spec, options={"serve_spec": False})
app_client = app.app.test_client()
url = '/v1.0/{spec}'.format(spec=spec.replace("yaml", "json"))
swagger_json = app_client.get(url) # type: flask.Response
assert swagger_json.status_code == 404
示例5: test_swagger_json_app
# 需要导入模块: from connexion import App [as 别名]
# 或者: from connexion.App import add_api [as 别名]
def test_swagger_json_app(simple_api_spec_dir, spec):
""" Verify the spec json file is returned for default setting passed to app. """
app = App(__name__, port=5001, specification_dir=simple_api_spec_dir, debug=True)
app.add_api(spec)
app_client = app.app.test_client()
url = '/v1.0/{spec}'
url = url.format(spec=spec.replace("yaml", "json"))
spec_json = app_client.get(url) # type: flask.Response
assert spec_json.status_code == 200
示例6: test_app_with_relative_path
# 需要导入模块: from connexion import App [as 别名]
# 或者: from connexion.App import add_api [as 别名]
def test_app_with_relative_path(simple_api_spec_dir, spec):
# Create the app with a relative path and run the test_app testcase below.
app = App(__name__, port=5001,
specification_dir='..' / simple_api_spec_dir.relative_to(TEST_FOLDER),
debug=True)
app.add_api(spec)
app_client = app.app.test_client()
get_bye = app_client.get('/v1.0/bye/jsantos') # type: flask.Response
assert get_bye.status_code == 200
assert get_bye.data == b'Goodbye jsantos'
示例7: build_app_from_fixture
# 需要导入模块: from connexion import App [as 别名]
# 或者: from connexion.App import add_api [as 别名]
def build_app_from_fixture(api_spec_folder, **kwargs):
debug = True
if 'debug' in kwargs:
debug = kwargs['debug']
del (kwargs['debug'])
cnx_app = App(__name__,
port=5001,
specification_dir=FIXTURES_FOLDER / api_spec_folder,
debug=debug)
cnx_app.add_api('swagger.yaml', **kwargs)
return cnx_app
示例8: test_using_all_fields_in_path_item
# 需要导入模块: from connexion import App [as 别名]
# 或者: from connexion.App import add_api [as 别名]
def test_using_all_fields_in_path_item(simple_api_spec_dir):
"""Test that connexion will try to add an endpoint only on http methods.
test also that each http methods has its own endpoint.
"""
app = App(__name__, specification_dir=simple_api_spec_dir)
app.add_api('openapi.yaml')
test_methods = set()
for rule in app.app.url_map.iter_rules():
if rule.rule != "/v1.0/add_operation_on_http_methods_only":
continue
test_methods.update({method.lower() for method in rule.methods})
assert set(test_methods) == METHODS
示例9: test_no_swagger_ui
# 需要导入模块: from connexion import App [as 别名]
# 或者: from connexion.App import add_api [as 别名]
def test_no_swagger_ui(simple_api_spec_dir):
app = App(__name__, port=5001, specification_dir=simple_api_spec_dir,
swagger_ui=False, debug=True)
app.add_api('swagger.yaml')
app_client = app.app.test_client()
swagger_ui = app_client.get('/v1.0/ui/') # type: flask.Response
assert swagger_ui.status_code == 404
app2 = App(__name__, port=5001, specification_dir=simple_api_spec_dir, debug=True)
app2.add_api('swagger.yaml', swagger_ui=False)
app2_client = app2.app.test_client()
swagger_ui2 = app2_client.get('/v1.0/ui/') # type: flask.Response
assert swagger_ui2.status_code == 404
示例10: test_no_swagger_ui
# 需要导入模块: from connexion import App [as 别名]
# 或者: from connexion.App import add_api [as 别名]
def test_no_swagger_ui(simple_api_spec_dir, spec):
options = {"swagger_ui": False}
app = App(__name__, port=5001, specification_dir=simple_api_spec_dir,
options=options, debug=True)
app.add_api(spec)
app_client = app.app.test_client()
swagger_ui = app_client.get('/v1.0/ui/') # type: flask.Response
assert swagger_ui.status_code == 404
app2 = App(__name__, port=5001, specification_dir=simple_api_spec_dir, debug=True)
app2.add_api(spec, options={"swagger_ui": False})
app2_client = app2.app.test_client()
swagger_ui2 = app2_client.get('/v1.0/ui/') # type: flask.Response
assert swagger_ui2.status_code == 404
示例11: test_app_with_different_uri_parser
# 需要导入模块: from connexion import App [as 别名]
# 或者: from connexion.App import add_api [as 别名]
def test_app_with_different_uri_parser(simple_api_spec_dir):
from connexion.decorators.uri_parsing import FirstValueURIParser
app = App(__name__, port=5001,
specification_dir='..' / simple_api_spec_dir.relative_to(TEST_FOLDER),
options={"uri_parser_class": FirstValueURIParser},
debug=True)
app.add_api('swagger.yaml')
app_client = app.app.test_client()
resp = app_client.get(
'/v1.0/test_array_csv_query_param?items=a,b,c&items=d,e,f'
) # type: flask.Response
assert resp.status_code == 200
j = json.loads(resp.get_data(as_text=True))
assert j == ['a', 'b', 'c']
示例12: test_handle_add_operation_error_debug
# 需要导入模块: from connexion import App [as 别名]
# 或者: from connexion.App import add_api [as 别名]
def test_handle_add_operation_error_debug(simple_api_spec_dir):
app = App(__name__, specification_dir=simple_api_spec_dir, debug=True)
app.api_cls = type('AppTest', (app.api_cls,), {})
app.api_cls.add_operation = mock.MagicMock(side_effect=Exception('operation error!'))
api = app.add_api('swagger.yaml', resolver=lambda oid: (lambda foo: 'bar'))
assert app.api_cls.add_operation.called
assert api.resolver.resolve_function_from_operation_id('faux')('bah') == 'bar'
示例13: test_app_with_resolver
# 需要导入模块: from connexion import App [as 别名]
# 或者: from connexion.App import add_api [as 别名]
def test_app_with_resolver(simple_api_spec_dir, spec):
from connexion.resolver import Resolver
resolver = Resolver()
app = App(__name__, port=5001,
specification_dir='..' / simple_api_spec_dir.relative_to(TEST_FOLDER),
resolver=resolver)
api = app.add_api(spec)
assert api.resolver is resolver
示例14: test_dict_as_yaml_path
# 需要导入模块: from connexion import App [as 别名]
# 或者: from connexion.App import add_api [as 别名]
def test_dict_as_yaml_path(simple_api_spec_dir):
swagger_yaml_path = simple_api_spec_dir / 'swagger.yaml'
with swagger_yaml_path.open(mode='rb') as swagger_yaml:
contents = swagger_yaml.read()
try:
swagger_template = contents.decode()
except UnicodeDecodeError:
swagger_template = contents.decode('utf-8', 'replace')
swagger_string = jinja2.Template(swagger_template).render({})
specification = yaml.safe_load(swagger_string) # type: dict
app = App(__name__, port=5001, specification_dir=simple_api_spec_dir, debug=True)
app.add_api(specification)
app_client = app.app.test_client()
swagger_json = app_client.get('/v1.0/swagger.json') # type: flask.Response
assert swagger_json.status_code == 200
示例15: test_dict_as_yaml_path
# 需要导入模块: from connexion import App [as 别名]
# 或者: from connexion.App import add_api [as 别名]
def test_dict_as_yaml_path(simple_api_spec_dir, spec):
openapi_yaml_path = simple_api_spec_dir / spec
with openapi_yaml_path.open(mode='rb') as openapi_yaml:
contents = openapi_yaml.read()
try:
openapi_template = contents.decode()
except UnicodeDecodeError:
openapi_template = contents.decode('utf-8', 'replace')
openapi_string = jinja2.Template(openapi_template).render({})
specification = yaml.load(openapi_string, ExtendedSafeLoader) # type: dict
app = App(__name__, port=5001, specification_dir=simple_api_spec_dir, debug=True)
app.add_api(specification)
app_client = app.app.test_client()
url = '/v1.0/{spec}'.format(spec=spec.replace("yaml", "json"))
swagger_json = app_client.get(url) # type: flask.Response
assert swagger_json.status_code == 200