本文整理汇总了Python中tastypie.api.Api.top_level方法的典型用法代码示例。如果您正苦于以下问题:Python Api.top_level方法的具体用法?Python Api.top_level怎么用?Python Api.top_level使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类tastypie.api.Api
的用法示例。
在下文中一共展示了Api.top_level方法的8个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: test_custom_api_serializer
# 需要导入模块: from tastypie.api import Api [as 别名]
# 或者: from tastypie.api.Api import top_level [as 别名]
def test_custom_api_serializer(self):
"""Confirm that an Api can use a custom serializer"""
# Origin: https://github.com/django-tastypie/django-tastypie/pull/817
class JSONSerializer(Serializer):
formats = ('json', )
api = Api(serializer_class=JSONSerializer)
api.register(NoteResource())
request = HttpRequest()
request.META = {'HTTP_ACCEPT': 'text/javascript'}
resp = api.top_level(request)
self.assertEqual(resp.status_code, 200)
self.assertEqual(resp['content-type'], 'application/json',
msg="Expected application/json response but received %s" % resp['content-type'])
request = HttpRequest()
request.META = {'HTTP_ACCEPT': 'application/xml'}
resp = api.top_level(request)
self.assertEqual(resp.status_code, 200)
self.assertEqual(resp['content-type'], 'application/json',
msg="Expected application/json response but received %s" % resp['content-type'])
示例2: test_top_level_jsonp
# 需要导入模块: from tastypie.api import Api [as 别名]
# 或者: from tastypie.api.Api import top_level [as 别名]
def test_top_level_jsonp(self):
api = Api()
api.register(NoteResource())
api.register(UserResource())
request = HttpRequest()
request.META = {"HTTP_ACCEPT": "text/javascript"}
request.GET = {"callback": "foo"}
resp = api.top_level(request)
self.assertEqual(resp.status_code, 200)
self.assertEqual(resp["content-type"].split(";")[0], "text/javascript")
self.assertEqual(
resp.content.decode("utf-8"),
'foo({"notes": {"list_endpoint": "/api/v1/notes/", "schema": "/api/v1/notes/schema/"}, "users": {"list_endpoint": "/api/v1/users/", "schema": "/api/v1/users/schema/"}})',
)
request = HttpRequest()
request.META = {"HTTP_ACCEPT": "text/javascript"}
request.GET = {"callback": ""}
try:
resp = api.top_level(request)
self.fail("Broken callback didn't fail!")
except BadRequest:
# Regression: We expect this, which is fine, but this used to
# be an import error.
pass
示例3: test_top_level_include_schema_content
# 需要导入模块: from tastypie.api import Api [as 别名]
# 或者: from tastypie.api.Api import top_level [as 别名]
def test_top_level_include_schema_content(self):
api = Api()
note_resource = NoteResource()
user_resource = UserResource()
api.register(note_resource)
api.register(user_resource)
request = HttpRequest()
request.GET = {'fullschema': 'true'}
resp = api.top_level(request)
self.assertEqual(resp.status_code, 200)
content = json.loads(resp.content.decode('utf-8'))
content['notes']['schema'] = adjust_schema(content['notes']['schema'])
content['users']['schema'] = adjust_schema(content['users']['schema'])
dummy_request = HttpRequest()
dummy_request.method = 'GET'
notes_schema = adjust_schema(json.loads(note_resource.get_schema(dummy_request).content.decode('utf-8')))
user_schema = adjust_schema(json.loads(user_resource.get_schema(dummy_request).content.decode('utf-8')))
self.assertEqual(content['notes']['list_endpoint'], '/api/v1/notes/')
self.assertEqual(content['notes']['schema'], notes_schema)
self.assertEqual(content['users']['list_endpoint'], '/api/v1/users/')
self.assertEqual(content['users']['schema'], user_schema)
示例4: test_top_level
# 需要导入模块: from tastypie.api import Api [as 别名]
# 或者: from tastypie.api.Api import top_level [as 别名]
def test_top_level(self):
api = Api()
api.register(NoteResource())
api.register(UserResource())
request = HttpRequest()
resp = api.top_level(request)
self.assertEqual(resp.status_code, 200)
self.assertEqual(resp.content, '{"notes": "/api/v1/notes/", "users": "/api/v1/users/"}')
示例5: test_top_level
# 需要导入模块: from tastypie.api import Api [as 别名]
# 或者: from tastypie.api.Api import top_level [as 别名]
def test_top_level(self):
api = Api()
api.register(NoteResource())
api.register(UserResource())
request = HttpRequest()
resp = api.top_level(request)
self.assertEqual(resp.status_code, 200)
self.assertEqual(resp.content.decode('utf-8'), '{"notes": {"list_endpoint": "/api/v1/notes/", "schema": "/api/v1/notes/schema/"}, "users": {"list_endpoint": "/api/v1/users/", "schema": "/api/v1/users/schema/"}}')
示例6: test_top_level_jsonp
# 需要导入模块: from tastypie.api import Api [as 别名]
# 或者: from tastypie.api.Api import top_level [as 别名]
def test_top_level_jsonp(self):
api = Api()
api.register(NoteResource())
api.register(UserResource())
request = HttpRequest()
request.META = {'HTTP_ACCEPT': 'text/javascript'}
request.GET = {'callback': 'foo'}
resp = api.top_level(request)
self.assertEqual(resp.status_code, 200)
self.assertEqual(resp['content-type'].split(';')[0], 'text/javascript')
self.assertEqual(resp.content.decode('utf-8'), 'foo({"notes": {"list_endpoint": "/api/v1/notes/", "schema": "/api/v1/notes/schema/"}, "users": {"list_endpoint": "/api/v1/users/", "schema": "/api/v1/users/schema/"}})')
request = HttpRequest()
request.META = {'HTTP_ACCEPT': 'text/javascript'}
request.GET = {'callback': ''}
# Regression: We expect this, which is fine, but this used to
# be an import error.
with self.assertRaises(BadRequest):
api.top_level(request)
示例7: test_jsonp_not_on_by_default
# 需要导入模块: from tastypie.api import Api [as 别名]
# 或者: from tastypie.api.Api import top_level [as 别名]
def test_jsonp_not_on_by_default(self):
api = Api()
api.register(NoteResource())
api.register(UserResource())
request = HttpRequest()
request.META = {'HTTP_ACCEPT': 'text/javascript'}
request.GET = {'callback': 'foo'}
resp = api.top_level(request)
self.assertEqual(resp.status_code, 200)
self.assertEqual(resp['content-type'].split(';')[0], 'application/json')
self.assertFalse("foo" in resp.content.decode('utf-8'))
示例8: test_top_level_jsonp
# 需要导入模块: from tastypie.api import Api [as 别名]
# 或者: from tastypie.api.Api import top_level [as 别名]
def test_top_level_jsonp(self):
api = Api()
api.register(NoteResource())
api.register(UserResource())
request = HttpRequest()
request.META = {"HTTP_ACCEPT": "text/javascript"}
request.GET = {"callback": "foo"}
resp = api.top_level(request)
self.assertEqual(resp.status_code, 200)
self.assertEqual(resp["content-type"].split(";")[0], "text/javascript")
self.assertEqual(
resp.content,
'foo({"notes": {"list_endpoint": "/api/v1/notes/", "schema": "/api/v1/notes/schema/"}, "users": {"list_endpoint": "/api/v1/users/", "schema": "/api/v1/users/schema/"}})',
)