当前位置: 首页>>代码示例>>Python>>正文


Python Api.model方法代码示例

本文整理汇总了Python中flask.ext.restplus.Api.model方法的典型用法代码示例。如果您正苦于以下问题:Python Api.model方法的具体用法?Python Api.model怎么用?Python Api.model使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在flask.ext.restplus.Api的用法示例。


在下文中一共展示了Api.model方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。

示例1: test_marshal_honour_custom_field_mask

# 需要导入模块: from flask.ext.restplus import Api [as 别名]
# 或者: from flask.ext.restplus.Api import model [as 别名]
    def test_marshal_honour_custom_field_mask(self):
        api = Api(self.app)

        model = api.model('Test', {
            'name': fields.String,
            'age': fields.Integer,
            'boolean': fields.Boolean,
        })

        @api.route('/test/')
        class TestResource(Resource):
            def get(self):
                return api.marshal({
                    'name': 'John Doe',
                    'age': 42,
                    'boolean': True
                }, model)

        with self.settings(RESTPLUS_MASK_HEADER='X-Mask'):
            data = self.get_json('/test/', headers={
                'X-Mask': '{name,age}'
            })

        self.assertEqual(data, {
            'name': 'John Doe',
            'age': 42,
        })
开发者ID:frol,项目名称:flask-restplus,代码行数:29,代码来源:test_fields_mask.py

示例2: test_marshal_does_not_hit_unrequired_attributes

# 需要导入模块: from flask.ext.restplus import Api [as 别名]
# 或者: from flask.ext.restplus.Api import model [as 别名]
    def test_marshal_does_not_hit_unrequired_attributes(self):
        api = Api(self.app)

        model = api.model('Test', {
            'name': fields.String,
            'age': fields.Integer,
            'boolean': fields.Boolean,
        })

        class Person(object):
            def __init__(self, name, age):
                self.name = name
                self.age = age

            @property
            def boolean(self):
                raise Exception()

        @api.route('/test/')
        class TestResource(Resource):
            @api.marshal_with(model)
            def get(self):
                return Person('John Doe', 42)

        data = self.get_json('/test/', headers={
            'X-Fields': '{name,age}'
        })
        self.assertEqual(data, {
            'name': 'John Doe',
            'age': 42,
        })
开发者ID:frol,项目名称:flask-restplus,代码行数:33,代码来源:test_fields_mask.py

示例3: test_marshal_with_expose_custom_mask_header

# 需要导入模块: from flask.ext.restplus import Api [as 别名]
# 或者: from flask.ext.restplus.Api import model [as 别名]
    def test_marshal_with_expose_custom_mask_header(self):
        api = Api(self.app)

        model = api.model('Test', {
            'name': fields.String,
            'age': fields.Integer,
            'boolean': fields.Boolean,
        })

        @api.route('/test/')
        class TestResource(Resource):
            @api.marshal_with(model)
            def get(self):
                return {
                    'name': 'John Doe',
                    'age': 42,
                    'boolean': True
                }

        with self.settings(RESTPLUS_MASK_HEADER='X-Mask'):
            specs = self.get_specs()

        op = specs['paths']['/test/']['get']
        self.assertIn('parameters', op)
        self.assertEqual(len(op['parameters']), 1)

        param = op['parameters'][0]
        self.assertEqual(param['name'], 'X-Mask')
开发者ID:frol,项目名称:flask-restplus,代码行数:30,代码来源:test_fields_mask.py

示例4: test_marshal_with_expose_mask_header

# 需要导入模块: from flask.ext.restplus import Api [as 别名]
# 或者: from flask.ext.restplus.Api import model [as 别名]
    def test_marshal_with_expose_mask_header(self):
        api = Api(self.app)

        model = api.model('Test', {
            'name': fields.String,
            'age': fields.Integer,
            'boolean': fields.Boolean,
        })

        @api.route('/test/')
        class TestResource(Resource):
            @api.marshal_with(model)
            def get(self):
                return {
                    'name': 'John Doe',
                    'age': 42,
                    'boolean': True
                }

        specs = self.get_specs()
        op = specs['paths']['/test/']['get']

        self.assertIn('parameters', op)
        self.assertEqual(len(op['parameters']), 1)

        param = op['parameters'][0]

        self.assertEqual(param['name'], 'X-Fields')
        self.assertEqual(param['type'], 'string')
        self.assertEqual(param['format'], 'mask')
        self.assertEqual(param['in'], 'header')
        self.assertNotIn('required', param)
开发者ID:frol,项目名称:flask-restplus,代码行数:34,代码来源:test_fields_mask.py

示例5: test_marshal_with_disabling_mask_header

# 需要导入模块: from flask.ext.restplus import Api [as 别名]
# 或者: from flask.ext.restplus.Api import model [as 别名]
    def test_marshal_with_disabling_mask_header(self):
        api = Api(self.app)

        model = api.model('Test', {
            'name': fields.String,
            'age': fields.Integer,
            'boolean': fields.Boolean,
        })

        @api.route('/test/')
        class TestResource(Resource):
            @api.marshal_with(model)
            def get(self):
                return {
                    'name': 'John Doe',
                    'age': 42,
                    'boolean': True
                }

        with self.settings(RESTPLUS_MASK_SWAGGER=False):
            specs = self.get_specs()

        op = specs['paths']['/test/']['get']

        self.assertNotIn('parameters', op)
开发者ID:frol,项目名称:flask-restplus,代码行数:27,代码来源:test_fields_mask.py

示例6: test_marshal_with_honour_field_mask_list

# 需要导入模块: from flask.ext.restplus import Api [as 别名]
# 或者: from flask.ext.restplus.Api import model [as 别名]
    def test_marshal_with_honour_field_mask_list(self):
        api = Api(self.app)

        model = api.model('Test', {
            'name': fields.String,
            'age': fields.Integer,
            'boolean': fields.Boolean,
        })

        @api.route('/test/')
        class TestResource(Resource):
            @api.marshal_with(model)
            def get(self):
                return [{
                    'name': 'John Doe',
                    'age': 42,
                    'boolean': True
                }, {
                    'name': 'Jane Doe',
                    'age': 33,
                    'boolean': False
                }]

        data = self.get_json('/test/', headers={
            'X-Fields': '{name,age}'
        })
        self.assertEqual(data, [{
            'name': 'John Doe',
            'age': 42,
        }, {
            'name': 'Jane Doe',
            'age': 33,
        }])
开发者ID:frol,项目名称:flask-restplus,代码行数:35,代码来源:test_fields_mask.py

示例7: test_nested_field_as_list_is_reusable

# 需要导入模块: from flask.ext.restplus import Api [as 别名]
# 或者: from flask.ext.restplus.Api import model [as 别名]
    def test_nested_field_as_list_is_reusable(self):
        api = Api(self.app)
        nested_fields = api.model('NestedModel', {'name': fields.String})

        prop = utils.field_to_property(api.as_list(fields.Nested(nested_fields)))
        self.assertEqual(prop, {'type': 'array', 'items': {'$ref': 'NestedModel'}})

        prop = utils.field_to_property(fields.Nested(nested_fields))
        self.assertEqual(prop, {'$ref': 'NestedModel', 'required': True})
开发者ID:vlcinsky,项目名称:flask-restplus,代码行数:11,代码来源:test_swagger_utils.py

示例8: test_models

# 需要导入模块: from flask.ext.restplus import Api [as 别名]
# 或者: from flask.ext.restplus.Api import model [as 别名]
 def test_models(self):
     app = Flask(__name__)
     api = Api(app)
     todo_fields = api.model('Todo', {
         'task': fields.String(required=True, description='The task details')
     })
     parser = reqparse.RequestParser()
     parser.add_argument('todo', type=todo_fields)
     self.assertEqual(utils.parser_to_params(parser), {
         'todo': {
             'type': 'Todo',
             'paramType': 'body',
         },
     })
开发者ID:FashtimeDotCom,项目名称:flask-restplus,代码行数:16,代码来源:test_swagger_utils.py

示例9: test_raise_400_on_invalid_mask

# 需要导入模块: from flask.ext.restplus import Api [as 别名]
# 或者: from flask.ext.restplus.Api import model [as 别名]
    def test_raise_400_on_invalid_mask(self):
        api = Api(self.app)

        model = api.model('Test', {
            'name': fields.String,
            'age': fields.Integer,
        })

        @api.route('/test/')
        class TestResource(Resource):
            @api.marshal_with(model)
            def get(self):
                pass

        with self.app.test_client() as client:
            response = client.get('/test/', headers={'X-Fields': 'name{,missing}'})
            self.assertEqual(response.status_code, 400)
            self.assertEquals(response.content_type, 'application/json')
开发者ID:rmoorman,项目名称:flask-restplus,代码行数:20,代码来源:test_fields_mask.py

示例10: test_is_only_exposed_on_marshal_with

# 需要导入模块: from flask.ext.restplus import Api [as 别名]
# 或者: from flask.ext.restplus.Api import model [as 别名]
    def test_is_only_exposed_on_marshal_with(self):
        api = Api(self.app)

        model = api.model('Test', {
            'name': fields.String,
            'age': fields.Integer,
            'boolean': fields.Boolean,
        })

        @api.route('/test/')
        class TestResource(Resource):
            def get(self):
                return api.marshal({
                    'name': 'John Doe',
                    'age': 42,
                    'boolean': True
                }, model)

        specs = self.get_specs()
        op = specs['paths']['/test/']['get']

        self.assertNotIn('parameters', op)
开发者ID:frol,项目名称:flask-restplus,代码行数:24,代码来源:test_fields_mask.py

示例11: test_marshal_with_skip_missing_fields

# 需要导入模块: from flask.ext.restplus import Api [as 别名]
# 或者: from flask.ext.restplus.Api import model [as 别名]
    def test_marshal_with_skip_missing_fields(self):
        api = Api(self.app)

        model = api.model('Test', {
            'name': fields.String,
            'age': fields.Integer,
        })

        @api.route('/test/')
        class TestResource(Resource):
            @api.marshal_with(model)
            def get(self):
                return {
                    'name': 'John Doe',
                    'age': 42,
                }

        data = self.get_json('/test/', headers={
            'X-Fields': '{name,missing}'
        })
        self.assertEqual(data, {
            'name': 'John Doe',
        })
开发者ID:rmoorman,项目名称:flask-restplus,代码行数:25,代码来源:test_fields_mask.py

示例12: CreditApi

# 需要导入模块: from flask.ext.restplus import Api [as 别名]
# 或者: from flask.ext.restplus.Api import model [as 别名]
   'NumberOfTime60-89DaysPastDueNotWorse', 
   type=float, 
   required=True, 
   help='Number of mortgage and real estate loans including home equity lines of credit',
   location='form')
parser.add_argument(
   'NumberOfDependents', 
   type=float, 
   required=True, 
   help='Number of mortgage and real estate loans including home equity lines of credit',
   location='form')



resource_fields = api.model('Resource', {
    'result': fields.String,
})

from flask.ext.restplus import Resource
@ns.route('/')
class CreditApi(Resource):

   @api.doc(parser=parser)
   @api.marshal_with(resource_fields)
   def post(self):
     args = parser.parse_args()
     result = self.get_result(args)

     return result, 201

   def get_result(self, args):
开发者ID:fclesio,项目名称:learning-space,代码行数:33,代码来源:credit_api.py

示例13: test_with_readonly

# 需要导入模块: from flask.ext.restplus import Api [as 别名]
# 或者: from flask.ext.restplus.Api import model [as 别名]
 def test_with_readonly(self):
     api = Api(self.app)
     nested_fields = api.model('NestedModel', {'name': fields.String})
     field = fields.Nested(nested_fields, readonly=True)
     self.assertEqual(field.__schema__, {'$ref': '#/definitions/NestedModel', 'readOnly': True})
开发者ID:rmoorman,项目名称:flask-restplus,代码行数:7,代码来源:test_swagger_fields.py

示例14: ModelTestCase

# 需要导入模块: from flask.ext.restplus import Api [as 别名]
# 或者: from flask.ext.restplus.Api import model [as 别名]
class ModelTestCase(TestCase):
    def setUp(self):
        super(ModelTestCase, self).setUp()
        blueprint = Blueprint('api', __name__)
        self.api = Api(blueprint)
        self.app.register_blueprint(blueprint)

    def test_model_as_flat_dict(self):
        model = self.api.model('Person', {
            'name': fields.String,
            'age': fields.Integer,
            'birthdate': fields.DateTime,
        })

        self.assertEqual(model.__schema__, {
            'properties': {
                'name': {
                    'type': 'string'
                },
                'age': {
                    'type': 'integer'
                },
                'birthdate': {
                    'type': 'string',
                    'format': 'date-time'
                }
            }
        })

    def test_model_as_nested_dict(self):
        address = self.api.model('Address', {
            'road': fields.String,
        })

        person = self.api.model('Person', {
            'name': fields.String,
            'age': fields.Integer,
            'birthdate': fields.DateTime,
            'address': fields.Nested(address)
        })

        self.assertEqual(person.__schema__, {
            # 'required': ['address'],
            'properties': {
                'name': {
                    'type': 'string'
                },
                'age': {
                    'type': 'integer'
                },
                'birthdate': {
                    'type': 'string',
                    'format': 'date-time'
                },
                'address': {
                    '$ref': '#/definitions/Address',
                }
            }
        })

        self.assertEqual(address.__schema__, {
            'properties': {
                'road': {
                    'type': 'string'
                },
            }
        })

    def test_model_as_dict_with_list(self):
        model = self.api.model('Person', {
            'name': fields.String,
            'age': fields.Integer,
            'tags': fields.List(fields.String),
        })

        self.assertEqual(model.__schema__, {
            'properties': {
                'name': {
                    'type': 'string'
                },
                'age': {
                    'type': 'integer'
                },
                'tags': {
                    'type': 'array',
                    'items': {
                        'type': 'string'
                    }
                }
            }
        })

    def test_model_as_nested_dict_with_list(self):
        address = self.api.model('Address', {
            'road': fields.String,
        })

        person = self.api.model('Person', {
            'name': fields.String,
            'age': fields.Integer,
#.........这里部分代码省略.........
开发者ID:alexa-infra,项目名称:flask-restplus,代码行数:103,代码来源:test_model.py

示例15: test_nested_field_with_description

# 需要导入模块: from flask.ext.restplus import Api [as 别名]
# 或者: from flask.ext.restplus.Api import model [as 别名]
 def test_nested_field_with_description(self):
     api = Api(self.app)
     nested_fields = api.model('NestedModel', {'name': fields.String})
     prop = utils.field_to_property(fields.Nested(nested_fields, description='A description'))
     self.assertEqual(prop, {'$ref': 'NestedModel', 'required': True, 'description': 'A description'})
开发者ID:FashtimeDotCom,项目名称:flask-restplus,代码行数:7,代码来源:test_swagger_utils.py


注:本文中的flask.ext.restplus.Api.model方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。