本文整理汇总了Python中flask.ext.restplus.Api类的典型用法代码示例。如果您正苦于以下问题:Python Api类的具体用法?Python Api怎么用?Python Api使用的例子?那么恭喜您, 这里精选的类代码示例或许可以为您提供帮助。
在下文中一共展示了Api类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: test_marshal_with_disabling_mask_header
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)
示例2: test_marshal_with_expose_mask_header
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)
示例3: test_marshal_with_expose_custom_mask_header
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')
示例4: test_marshal_honour_custom_field_mask
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,
})
示例5: test_marshal_does_not_hit_unrequired_attributes
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,
})
示例6: test_marshal_with_honour_field_mask_list
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,
}])
示例7: test_will_prettyprint_json_in_debug_mode
def test_will_prettyprint_json_in_debug_mode(self):
self.app.config['DEBUG'] = True
api = Api(self.app)
class Foo1(Resource):
def get(self):
return {'foo': 'bar', 'baz': 'asdf'}
api.add_resource(Foo1, '/foo', endpoint='bar')
with self.app.test_client() as client:
foo = client.get('/foo')
# Python's dictionaries have random order (as of "new" Pythons,
# anyway), so we can't verify the actual output here. We just
# assert that they're properly prettyprinted.
lines = foo.data.splitlines()
lines = [line.decode() for line in lines]
self.assertEquals("{", lines[0])
self.assertTrue(lines[1].startswith(' '))
self.assertTrue(lines[2].startswith(' '))
self.assertEquals("}", lines[3])
# Assert our trailing newline.
self.assertTrue(foo.data.endswith(b'\n'))
示例8: test_nested_field_as_list_is_reusable
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})
示例9: test_models
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',
},
})
示例10: test_json_float_marshalled
def test_json_float_marshalled(self):
api = Api(self.app)
class FooResource(Resource):
fields = {'foo': fields.Float}
def get(self):
return marshal({"foo": 3.0}, self.fields)
api.add_resource(FooResource, '/api')
app = self.app.test_client()
resp = app.get('/api')
self.assertEquals(resp.status_code, 200)
self.assertEquals(resp.data.decode('utf-8'), '{"foo": 3.0}\n')
示例11: test_raise_400_on_invalid_mask
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')
示例12: test_is_only_exposed_on_marshal_with
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)
示例13: test_marshal_with_skip_missing_fields
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',
})
示例14: Flask
from flask import Flask
from flask.ext.restplus import Api
app = Flask("dmon-agent")
api = Api(app, version='0.0.4', title='DICE Monitoring Agent API',
description="RESTful API for the DICE Monitoring Platform Agent (dmon-agent)",
)
# changes the descriptor on the Swagger WUI and appends to api /dmon and then /v1
agent = api.namespace('agent', description='dmon agent operations')
示例15: Flask
from flask import Flask
from flask.ext.restplus import Api, Resource, fields
app = Flask(__name__)
api = Api(
app,
version="1.0",
title="Todo API",
description="A simple TODO API extracted from the original flask-restful example",
)
ns = api.namespace("todos", description="TODO operations")
TODOS = {"todo1": {"task": "build an API"}, "todo2": {"task": "?????"}, "todo3": {"task": "profit!"}}
todo = api.model("Todo", {"task": fields.String(required=True, description="The task details")})
listed_todo = api.model(
"ListedTodo",
{
"id": fields.String(required=True, description="The todo ID"),
"todo": fields.Nested(todo, description="The Todo"),
},
)
def abort_if_todo_doesnt_exist(todo_id):
if todo_id not in TODOS:
api.abort(404, "Todo {} doesn't exist".format(todo_id))