Move get_encoding to descriptor.

This commit is contained in:
Carlton Gibson 2017-08-23 11:13:30 +02:00
parent aad9289ae9
commit 63f6377dff

View File

@ -282,10 +282,8 @@ class APIViewSchemaDescriptor(object):
fields += self.get_pagination_fields(path, method) fields += self.get_pagination_fields(path, method)
fields += self.get_filter_fields(path, method) fields += self.get_filter_fields(path, method)
# TEMP: now we proxy back to the generator
if fields and any([field.location in ('form', 'body') for field in fields]): if fields and any([field.location in ('form', 'body') for field in fields]):
encoding = generator.get_encoding(path, method, view) encoding = self.get_encoding(path, method)
else: else:
encoding = None encoding = None
@ -456,6 +454,29 @@ class APIViewSchemaDescriptor(object):
fields += filter_backend().get_schema_fields(view) fields += filter_backend().get_schema_fields(view)
return fields return fields
def get_encoding(self, path, method):
"""
Return the 'encoding' parameter to use for a given endpoint.
"""
view = self.view
# Core API supports the following request encodings over HTTP...
supported_media_types = set((
'application/json',
'application/x-www-form-urlencoded',
'multipart/form-data',
))
parser_classes = getattr(view, 'parser_classes', [])
for parser_class in parser_classes:
media_type = getattr(parser_class, 'media_type', None)
if media_type in supported_media_types:
return media_type
# Raw binary uploads are supported with "application/octet-stream"
if media_type == '*/*':
return 'application/octet-stream'
return None
# TODO: Where should this live? # TODO: Where should this live?
# - We import APIView here. So we can't import the descriptor into `views` # - We import APIView here. So we can't import the descriptor into `views`
# - APIView is only used by SchemaView. # - APIView is only used by SchemaView.
@ -640,29 +661,6 @@ class SchemaGenerator(object):
field_name = 'id' field_name = 'id'
return path.replace('{pk}', '{%s}' % field_name) return path.replace('{pk}', '{%s}' % field_name)
# Methods for generating each individual `Link` instance...
def get_encoding(self, path, method, view):
"""
Return the 'encoding' parameter to use for a given endpoint.
"""
# Core API supports the following request encodings over HTTP...
supported_media_types = set((
'application/json',
'application/x-www-form-urlencoded',
'multipart/form-data',
))
parser_classes = getattr(view, 'parser_classes', [])
for parser_class in parser_classes:
media_type = getattr(parser_class, 'media_type', None)
if media_type in supported_media_types:
return media_type
# Raw binary uploads are supported with "application/octet-stream"
if media_type == '*/*':
return 'application/octet-stream'
return None
# Method for generating the link layout.... # Method for generating the link layout....
def get_keys(self, subpath, method, view): def get_keys(self, subpath, method, view):