本文整理汇总了Python中flask_restless.APIManager类的典型用法代码示例。如果您正苦于以下问题:Python APIManager类的具体用法?Python APIManager怎么用?Python APIManager使用的例子?那么恭喜您, 这里精选的类代码示例或许可以为您提供帮助。
在下文中一共展示了APIManager类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: test_init_app
def test_init_app(self):
self.db.create_all()
manager = APIManager(flask_sqlalchemy_db=self.db)
manager.create_api(self.Person)
manager.init_app(self.flaskapp)
response = self.app.get('/api/person')
assert response.status_code == 200
示例2: TestFlaskSQLAlchemy
class TestFlaskSQLAlchemy(FlaskSQLAlchemyTestBase):
"""Tests for deleting resources defined as Flask-SQLAlchemy models instead
of pure SQLAlchemy models.
"""
def setUp(self):
"""Creates the Flask-SQLAlchemy database and models."""
super(TestFlaskSQLAlchemy, self).setUp()
class Person(self.db.Model):
id = self.db.Column(self.db.Integer, primary_key=True)
self.Person = Person
self.db.create_all()
self.manager = APIManager(self.flaskapp, flask_sqlalchemy_db=self.db)
self.manager.create_api(self.Person, methods=['DELETE'])
def test_delete(self):
"""Tests for deleting a resource."""
person = self.Person(id=1)
self.session.add(person)
self.session.commit()
response = self.app.delete('/api/person/1')
assert response.status_code == 204
assert self.Person.query.count() == 0
示例3: test_multiple_managers_init_multiple_apps
def test_multiple_managers_init_multiple_apps(self):
"""Tests for calling :meth:`~APIManager.init_app` on multiple
:class:`~flask.Flask` applications after calling
:meth:`~APIManager.create_api` on multiple instances of
:class:`APIManager`.
"""
manager1 = APIManager(session=self.session)
manager2 = APIManager(session=self.session)
# Create the Flask applications and the test clients.
flaskapp1 = self.flaskapp
flaskapp2 = Flask(__name__)
testclient1 = self.app
testclient2 = flaskapp2.test_client()
force_content_type_jsonapi(testclient2)
# First create the API, then initialize the Flask applications after.
manager1.create_api(self.Person)
manager2.create_api(self.Article)
manager1.init_app(flaskapp1)
manager2.init_app(flaskapp2)
# Tests that only the first Flask application gets requests for
# /api/person and only the second gets requests for /api/article.
response = testclient1.get('/api/person')
assert response.status_code == 200
response = testclient1.get('/api/article')
assert response.status_code == 404
response = testclient2.get('/api/person')
assert response.status_code == 404
response = testclient2.get('/api/article')
assert response.status_code == 200
示例4: test_constructor_app
def test_constructor_app(self):
"""Tests for providing a :class:`~flask.Flask` application in
the constructor.
"""
manager = APIManager(app=self.flaskapp, session=self.session)
manager.create_api(self.Person)
response = self.app.get('/api/person')
assert response.status_code == 200
示例5: test_single_manager_init_single_app
def test_single_manager_init_single_app(self):
"""Tests for calling :meth:`~APIManager.init_app` with a single
:class:`~flask.Flask` application after calling
:meth:`~APIManager.create_api`.
"""
manager = APIManager(session=self.session)
manager.create_api(self.Person)
manager.init_app(self.flaskapp)
response = self.app.get('/api/person')
assert response.status_code == 200
示例6: test_schema_app_in_constructor
def test_schema_app_in_constructor(self):
manager = APIManager(self.flaskapp, session=self.session)
manager.create_api(self.Article)
manager.create_api(self.Person)
response = self.app.get('/api')
self.assertEqual(response.status_code, 200)
document = loads(response.data)
info = document['meta']['modelinfo']
self.assertEqual(sorted(info), ['article', 'person'])
self.assertTrue(info['article']['url'].endswith('/api/article'))
self.assertTrue(info['person']['url'].endswith('/api/person'))
示例7: test_empty_url_prefix
def test_empty_url_prefix(self):
"""Tests for specifying an empty string as URL prefix at the manager
level but not when creating an API.
"""
manager = APIManager(self.flaskapp, session=self.session,
url_prefix='')
manager.create_api(self.Person)
response = self.app.get('/person')
assert response.status_code == 200
response = self.app.get('/api/person')
assert response.status_code == 404
示例8: init_app
def init_app(app):
manager = APIManager(app, flask_sqlalchemy_db=db, url_prefix='/models')
manager.create_api(User,
preprocessors=admin_only_preprocessors,
url_prefix='',
methods=all_methods,
max_page_size=100,
exclude=('password',))
manager.create_api(Role,
preprocessors=admin_only_preprocessors,
url_prefix='',
methods=all_methods,
max_page_size=100)
app.logger.addFilter(ProcessingExceptionFilter())
示例9: test_create_api_before_db_create_all
def test_create_api_before_db_create_all(self):
"""Tests that we can create APIs before
:meth:`flask_sqlalchemy.SQLAlchemy.create_all` is called.
"""
manager = APIManager(self.flaskapp, flask_sqlalchemy_db=self.db)
manager.create_api(self.Person)
self.db.create_all()
person = self.Person(id=1)
self.db.session.add(person)
self.db.session.commit()
response = self.app.get('/api/person/1')
assert response.status_code == 200
document = loads(response.data)
person = document['data']
assert '1' == person['id']
示例10: test_override_url_prefix
def test_override_url_prefix(self):
"""Tests that a call to :meth:`APIManager.create_api` can
override the URL prefix provided in the constructor to the
manager class, if the new URL starts with a slash.
"""
manager = APIManager(self.flaskapp, session=self.session,
url_prefix='/foo')
manager.create_api(self.Person, url_prefix='/bar')
manager.create_api(self.Article, url_prefix='')
response = self.app.get('/bar/person')
assert response.status_code == 200
response = self.app.get('/article')
assert response.status_code == 200
response = self.app.get('/foo/person')
assert response.status_code == 404
response = self.app.get('/foo/article')
assert response.status_code == 404
示例11: TestFlaskSQLAlchemy
class TestFlaskSQLAlchemy(FlaskSQLAlchemyTestBase):
"""Tests for updating resources defined as Flask-SQLAlchemy models instead
of pure SQLAlchemy models.
"""
def setUp(self):
"""Creates the Flask-SQLAlchemy database and models."""
super(TestFlaskSQLAlchemy, self).setUp()
# HACK During testing, we don't want the session to expire, so that we
# can access attributes of model instances *after* a request has been
# made (that is, after Flask-Restless does its work and commits the
# session).
session_options = dict(expire_on_commit=False)
# Overwrite the `db` and `session` attributes from the superclass.
self.db = SQLAlchemy(self.flaskapp, session_options=session_options)
self.session = self.db.session
class Person(self.db.Model):
id = self.db.Column(self.db.Integer, primary_key=True)
name = self.db.Column(self.db.Unicode)
self.Person = Person
self.db.create_all()
self.manager = APIManager(self.flaskapp, flask_sqlalchemy_db=self.db)
self.manager.create_api(self.Person, methods=['PATCH'])
def test_create(self):
"""Tests for creating a resource."""
person = self.Person(id=1, name=u'foo')
self.session.add(person)
self.session.commit()
data = {
'data': {
'id': '1',
'type': 'person',
'attributes': {
'name': u'bar'
}
}
}
response = self.app.patch('/api/person/1', data=dumps(data))
assert response.status_code == 204
assert person.name == 'bar'
示例12: test_single_manager_init_multiple_apps
def test_single_manager_init_multiple_apps(self):
"""Tests for calling :meth:`~APIManager.init_app` on multiple
:class:`~flask.Flask` applications after calling
:meth:`~APIManager.create_api`.
"""
manager = APIManager(session=self.session)
flaskapp1 = self.flaskapp
flaskapp2 = Flask(__name__)
testclient1 = self.app
testclient2 = flaskapp2.test_client()
force_content_type_jsonapi(testclient2)
manager.create_api(self.Person)
manager.init_app(flaskapp1)
manager.init_app(flaskapp2)
response = testclient1.get('/api/person')
assert response.status_code == 200
response = testclient2.get('/api/person')
assert response.status_code == 200
示例13: test_multiple_managers_init_single_app
def test_multiple_managers_init_single_app(self):
"""Tests for calling :meth:`~APIManager.init_app` on a single
:class:`~flask.Flask` application after calling
:meth:`~APIManager.create_api` on multiple instances of
:class:`APIManager`.
"""
manager1 = APIManager(session=self.session)
manager2 = APIManager(session=self.session)
# First create the API, then initialize the Flask applications after.
manager1.create_api(self.Person)
manager2.create_api(self.Article)
manager1.init_app(self.flaskapp)
manager2.init_app(self.flaskapp)
# Tests that both endpoints are accessible on the Flask application.
response = self.app.get('/api/person')
assert response.status_code == 200
response = self.app.get('/api/article')
assert response.status_code == 200
示例14: test_universal_preprocessor
def test_universal_preprocessor(self):
"""Tests universal preprocessor and postprocessor applied to all
methods created with the API manager.
"""
class Counter:
"""An object that increments a counter on each invocation."""
def __init__(self):
self._counter = 0
def __call__(self, *args, **kw):
self._counter += 1
def __eq__(self, other):
if isinstance(other, Counter):
return self._counter == other._counter
if isinstance(other, int):
return self._counter == other
return False
increment1 = Counter()
increment2 = Counter()
preprocessors = dict(GET_COLLECTION=[increment1])
postprocessors = dict(GET_COLLECTION=[increment2])
manager = APIManager(self.flaskapp, session=self.session,
preprocessors=preprocessors,
postprocessors=postprocessors)
manager.create_api(self.Person)
manager.create_api(self.Article)
# After each request, regardless of API endpoint, both counters should
# be incremented.
self.app.get('/api/person')
self.app.get('/api/article')
self.app.get('/api/person')
assert increment1 == increment2 == 3
示例15: init_app
def init_app(self, app, **kwargs):
self.app = app
self.manager = APIManager(self.app, **kwargs)
swagger = Blueprint('swagger', __name__, static_folder='static',
static_url_path=self.app.static_url_path + '/swagger', )
@swagger.route('/swagger')
def swagger_ui():
return redirect('/static/swagger/swagger-ui/index.html')
@swagger.route('/swagger.json')
def swagger_json():
# I can only get this from a request context
self.swagger['host'] = urlparse.urlparse(request.url_root).netloc
return jsonify(self.swagger)
app.register_blueprint(swagger)