本文整理汇总了Python中mimeparse.best_match方法的典型用法代码示例。如果您正苦于以下问题:Python mimeparse.best_match方法的具体用法?Python mimeparse.best_match怎么用?Python mimeparse.best_match使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类mimeparse
的用法示例。
在下文中一共展示了mimeparse.best_match方法的5个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: determine_content_type
# 需要导入模块: import mimeparse [as 别名]
# 或者: from mimeparse import best_match [as 别名]
def determine_content_type(self, request):
content_types = [
'application/vnd.siren+json',
'application/vnd.hal+json',
'application/hal+json',
'application/json',
]
accept = request.META.get('HTTP_ACCEPT', '*/*')
try:
content_type = best_match(content_types, accept)
except MimeTypeParseException:
content_type = None
if not content_type:
return content_types[-1]
return content_type
示例2: select_renderer
# 需要导入模块: import mimeparse [as 别名]
# 或者: from mimeparse import best_match [as 别名]
def select_renderer(request: web.Request, renderers: OrderedDict, force=True):
"""
Given a request, a list of renderers, and the ``force`` configuration
option, return a two-tuple of:
(media type, render callable). Uses mimeparse to find the best media
type match from the ACCEPT header.
"""
header = request.headers.get('ACCEPT', '*/*')
best_match = mimeparse.best_match(renderers.keys(), header)
if not best_match or best_match not in renderers:
if force:
return tuple(renderers.items())[0]
else:
raise web.HTTPNotAcceptable
return best_match, renderers[best_match]
# ###### Renderers ######
# Use a class so that json module is easily override-able
示例3: negotiate
# 需要导入模块: import mimeparse [as 别名]
# 或者: from mimeparse import best_match [as 别名]
def negotiate(self, request):
request.headers.getall('Accept', "*/*")
mime_type = mimeparse.best_match(['application/json', 'text/event-stream'],
",".join(request.headers.getall('Accept', "*/*")))
if mime_type == 'text/event-stream':
return await self.event_stream(request)
elif mime_type == 'application/json':
return await self.json(request)
else:
raise NotImplementedError(mime_type)
示例4: _best_mime
# 需要导入模块: import mimeparse [as 别名]
# 或者: from mimeparse import best_match [as 别名]
def _best_mime(supported, accept_string=None):
if accept_string is None:
return None
return mimeparse.best_match(supported, accept_string)
示例5: ui_rest_response_exception_factory
# 需要导入模块: import mimeparse [as 别名]
# 或者: from mimeparse import best_match [as 别名]
def ui_rest_response_exception_factory(request, response_code, title, message, response_class=HttpResponse):
rest_mime_types = list(get_supported_mime_types(get_default_converters()))
ui_mime_types = ['text/html', 'application/xhtml+xml']
best_match = mimeparse.best_match(rest_mime_types + ui_mime_types, request.META.get('HTTP_ACCEPT', 'text/html'))
if best_match in ui_mime_types:
return ui_response_exception_factory(request, response_code, title, message, response_class)
else:
return rest_response_exception_factory(request, response_code, title, message, response_class)