2013-04-25 15:47:34 +04:00
|
|
|
"""
|
|
|
|
Content negotiation deals with selecting an appropriate renderer given the
|
|
|
|
incoming request. Typically this will be based on the request's Accept header.
|
|
|
|
"""
|
2012-10-29 21:08:38 +04:00
|
|
|
from django.http import Http404
|
2015-06-18 16:38:29 +03:00
|
|
|
|
2022-07-14 15:20:36 +03:00
|
|
|
from rest_framework import exceptions
|
2012-09-20 16:06:27 +04:00
|
|
|
from rest_framework.settings import api_settings
|
2015-06-18 16:38:29 +03:00
|
|
|
from rest_framework.utils.mediatypes import (
|
2015-06-25 23:55:51 +03:00
|
|
|
_MediaType, media_type_matches, order_by_precedence
|
2015-06-18 16:38:29 +03:00
|
|
|
)
|
2012-09-20 16:06:27 +04:00
|
|
|
|
|
|
|
|
2019-04-30 18:53:44 +03:00
|
|
|
class BaseContentNegotiation:
|
2012-10-18 01:58:18 +04:00
|
|
|
def select_parser(self, request, parsers):
|
|
|
|
raise NotImplementedError('.select_parser() must be implemented')
|
2012-09-20 16:06:27 +04:00
|
|
|
|
2012-10-18 01:58:18 +04:00
|
|
|
def select_renderer(self, request, renderers, format_suffix=None):
|
|
|
|
raise NotImplementedError('.select_renderer() must be implemented')
|
2012-09-20 16:06:27 +04:00
|
|
|
|
2012-10-18 01:58:18 +04:00
|
|
|
|
|
|
|
class DefaultContentNegotiation(BaseContentNegotiation):
|
2012-09-20 16:06:27 +04:00
|
|
|
settings = api_settings
|
|
|
|
|
2012-10-18 01:58:18 +04:00
|
|
|
def select_parser(self, request, parsers):
|
2012-10-05 17:48:33 +04:00
|
|
|
"""
|
|
|
|
Given a list of parsers and a media type, return the appropriate
|
|
|
|
parser to handle the incoming request.
|
|
|
|
"""
|
|
|
|
for parser in parsers:
|
2012-10-18 01:58:18 +04:00
|
|
|
if media_type_matches(parser.media_type, request.content_type):
|
2012-10-05 17:48:33 +04:00
|
|
|
return parser
|
|
|
|
return None
|
|
|
|
|
2012-10-18 01:58:18 +04:00
|
|
|
def select_renderer(self, request, renderers, format_suffix=None):
|
2012-09-20 16:06:27 +04:00
|
|
|
"""
|
|
|
|
Given a request and a list of renderers, return a two-tuple of:
|
|
|
|
(renderer, media type).
|
|
|
|
"""
|
|
|
|
# Allow URL style format override. eg. "?format=json
|
2012-10-18 01:58:18 +04:00
|
|
|
format_query_param = self.settings.URL_FORMAT_OVERRIDE
|
2014-09-26 13:46:52 +04:00
|
|
|
format = format_suffix or request.query_params.get(format_query_param)
|
2012-09-20 16:06:27 +04:00
|
|
|
|
|
|
|
if format:
|
|
|
|
renderers = self.filter_renderers(renderers, format)
|
|
|
|
|
|
|
|
accepts = self.get_accept_list(request)
|
|
|
|
|
|
|
|
# Check the acceptable media types against each renderer,
|
|
|
|
# attempting more specific media types first
|
|
|
|
# NB. The inner loop here isn't as bad as it first looks :)
|
|
|
|
# Worst case is we're looping over len(accept_list) * len(self.renderers)
|
|
|
|
for media_type_set in order_by_precedence(accepts):
|
|
|
|
for renderer in renderers:
|
|
|
|
for media_type in media_type_set:
|
2012-10-05 13:23:47 +04:00
|
|
|
if media_type_matches(renderer.media_type, media_type):
|
|
|
|
# Return the most specific media type as accepted.
|
2015-04-23 13:38:15 +03:00
|
|
|
media_type_wrapper = _MediaType(media_type)
|
2014-08-19 16:28:07 +04:00
|
|
|
if (
|
|
|
|
_MediaType(renderer.media_type).precedence >
|
2015-04-23 13:38:15 +03:00
|
|
|
media_type_wrapper.precedence
|
2014-08-19 16:28:07 +04:00
|
|
|
):
|
2012-10-05 13:23:47 +04:00
|
|
|
# Eg client requests '*/*'
|
|
|
|
# Accepted media type is 'application/json'
|
2015-04-23 13:38:15 +03:00
|
|
|
full_media_type = ';'.join(
|
|
|
|
(renderer.media_type,) +
|
2022-07-14 15:20:36 +03:00
|
|
|
tuple(
|
|
|
|
'{}={}'.format(key, value)
|
|
|
|
for key, value in media_type_wrapper.params.items()
|
|
|
|
)
|
|
|
|
)
|
2015-04-23 13:38:15 +03:00
|
|
|
return renderer, full_media_type
|
2012-10-05 13:23:47 +04:00
|
|
|
else:
|
|
|
|
# Eg client requests 'application/json; indent=8'
|
|
|
|
# Accepted media type is 'application/json; indent=8'
|
2013-05-18 20:21:43 +04:00
|
|
|
return renderer, media_type
|
2012-09-20 16:06:27 +04:00
|
|
|
|
|
|
|
raise exceptions.NotAcceptable(available_renderers=renderers)
|
|
|
|
|
|
|
|
def filter_renderers(self, renderers, format):
|
|
|
|
"""
|
|
|
|
If there is a '.json' style format suffix, filter the renderers
|
|
|
|
so that we only negotiation against those that accept that format.
|
|
|
|
"""
|
|
|
|
renderers = [renderer for renderer in renderers
|
2012-10-05 13:23:47 +04:00
|
|
|
if renderer.format == format]
|
2012-09-20 16:06:27 +04:00
|
|
|
if not renderers:
|
2012-10-29 21:08:38 +04:00
|
|
|
raise Http404
|
2012-09-20 16:06:27 +04:00
|
|
|
return renderers
|
|
|
|
|
|
|
|
def get_accept_list(self, request):
|
|
|
|
"""
|
2016-08-08 11:32:22 +03:00
|
|
|
Given the incoming request, return a tokenized list of media
|
2012-09-20 16:06:27 +04:00
|
|
|
type strings.
|
|
|
|
"""
|
|
|
|
header = request.META.get('HTTP_ACCEPT', '*/*')
|
|
|
|
return [token.strip() for token in header.split(',')]
|