def __init__(self, service): Resource.__init__(self) self.service = service self.resources = { '/capoeira/events/similar/artist': self.service.capoeiraSimilarByArtistQuery, '/capoeira/events/similar/track': self.service.capoeiraSimilarByTrackQuery } self.isLeaf = True # default_params is used in lieu of a defined Accept field in the header default_params = AcceptParameters(ContentType("text/html"), Language("en")) acceptable = [ AcceptParameters(ContentType("text/html"), Language("en")), AcceptParameters(ContentType("text/json"), Language("en")), AcceptParameters(ContentType("application/json"), Language("en")) ] self.contentNegotiator = ContentNegotiator(default_params, acceptable) # function mapping for rendering response self.renderFns = { 'text/html': formatHTMLResponse, 'text/json': formatJSONResponse, 'application/json': formatJSONResponse }
def test_with_q_values(self): """ application/atom+xml vs application/rdf+xml with q values """ client_accept = "application/atom+xml;q=0.6, application/rdf+xml;q=0.9" server = [ AcceptParameters(ContentType("application/rdf+xml")), AcceptParameters(ContentType("application/atom+xml")), ] cn = ContentNegotiator(acceptable=server) accept_parameters = cn.negotiate(accept=client_accept) assert str(accept_parameters.content_type) == "application/rdf+xml"
def test_any_supported_by_server(self): """ */* supported by server """ client_accept = "*/*" server = [ AcceptParameters(ContentType("text/plain")), AcceptParameters(ContentType("image/png")), AcceptParameters(ContentType("image/jpeg")), ] cn = ContentNegotiator(acceptable=server) accept_parameters = cn.negotiate(accept=client_accept) assert str(accept_parameters.content_type) == "text/plain"
def determine_content_type(self, request): acceptable = ( AcceptParameters(ContentType('application/json')), AcceptParameters(ContentType('application/hal+json')), AcceptParameters(ContentType('application/vnd.siren+json')), ) negotiator = ContentNegotiator(acceptable[0], acceptable) accept = request.META.get('HTTP_ACCEPT') negotiated_type = negotiator.negotiate(accept=request.META.get('HTTP_ACCEPT')) if negotiated_type: return negotiated_type.content_type return acceptable[0].content_type
def test_mixed_q_values_most_preferred_available(self): """ application/atom+xml vs application/rdf+xml vs text/html with mixed q values, most preferred available """ client_accept = ( "application/atom+xml;q=0.6, application/rdf+xml;q=0.9, text/html" ) server = [ AcceptParameters(ContentType("application/rdf+xml")), AcceptParameters(ContentType("text/html")), ] cn = ContentNegotiator(acceptable=server) accept_parameters = cn.negotiate(accept=client_accept) assert str(accept_parameters.content_type) == "text/html"
def test_two_content_types_and_two_languages_specified(self): """ Two content types and 2 languages specified """ accept = "text/html, text/plain" accept_lang = "en, de" server = [ AcceptParameters(ContentType("text/html"), Language("de")), AcceptParameters(ContentType("text/plain"), Language("en")), ] cn = ContentNegotiator(acceptable=server) accept_parameters = cn.negotiate( accept=accept, accept_language=accept_lang ) assert str(accept_parameters.content_type) == "text/html" assert str(accept_parameters.language) == "de"
def _get_accept_params(self, obj): params = AcceptParameters() for k, v in obj.items(): if k == "content_type": params.content_type = ContentType(v) elif k == "packaging": params.packaging = v return params
def test_text_plain_only(self): """ text/plain only """ client_accept = "text/plain" server = [AcceptParameters(ContentType("text/plain"))] cn = ContentNegotiator(acceptable=server) accept_parameters = cn.negotiate(accept=client_accept) assert str(accept_parameters.content_type) == "text/plain"
def test_unsupported_by_server(self): """ text/plain only, unsupported by server """ client_accept = "text/plain" server = [AcceptParameters(ContentType("text/html"))] cn = ContentNegotiator(acceptable=server) accept_parameters = cn.negotiate(accept=client_accept) assert accept_parameters is None
def getFormat(self, request): defaultOutput = AcceptParameters( ContentType(self.defaultOutputFormat, params='q=0')) acceptable = [defaultOutput] + [ AcceptParameters(ContentType(x)) for x in self._outputFormatsPreference ] cn = ContentNegotiator(defaultOutput, acceptable) if request.requestHeaders.hasHeader("Accept"): kwargs = { "accept": request.requestHeaders.getRawHeaders("Accept")[0] } else: kwargs = {} accp = cn.negotiate(**kwargs) return str(accp.content_type) if accp else None
def test_two_content_types_and_one_language_specified_with_weights(self): """ Two content types and one language specified, with weights """ weights = { "content_type": 2.0, "language": 1.0, "charset": 1.0, "encoding": 1.0, } accept = "text/html, text/plain" accept_lang = "en" server = [ AcceptParameters(ContentType("text/html"), Language("de")), AcceptParameters(ContentType("text/plain"), Language("en")), ] cn = ContentNegotiator(acceptable=server, weights=weights) accept_parameters = cn.negotiate( accept=accept, accept_language=accept_lang ) assert str(accept_parameters.content_type) == "text/plain" assert str(accept_parameters.language) == "en"
def test_content_type_and_language_specified(self): """ content type and language specified """ accept = "text/html" accept_lang = "en" server = [AcceptParameters(ContentType("text/html"), Language("en"))] cn = ContentNegotiator(acceptable=server) accept_parameters = cn.negotiate( accept=accept, accept_language=accept_lang ) assert str(accept_parameters.content_type) == "text/html" assert str(accept_parameters.language) == "en"
def test_atom_with_type_feed_supported_by_server(self): """ application/atom+xml;type=feed supported by server """ client_accept = "application/atom+xml;type=feed" server = [ AcceptParameters(ContentType("application/atom+xml;type=feed")) ] cn = ContentNegotiator(acceptable=server) accept_parameters = cn.negotiate(accept=client_accept) assert ( str(accept_parameters.content_type) == "application/atom+xml;type=feed" )
def _init_content_negotiator(self): #TODO: use config instead default_content_type = "application/ld+json" default_accept_params = AcceptParameters( ContentType(default_content_type)) # rdf types rdf_types = set([ plugin.name for plugin in plugins(kind=Serializer) if "/" in plugin.name ]) #Blacklisted because mapped to TriX that requires a context-aware store blacklisted_types = ["application/xml"] #TODO: consider other types accepted_types = list( rdf_types.difference(blacklisted_types)) + ["application/json"] self._logger.debug("Accepted types: %s" % accepted_types) acceptable_params = [default_accept_params] + [ AcceptParameters(ContentType(ct)) for ct in accepted_types ] self._negotiator = ContentNegotiator(default_accept_params, acceptable_params)
def get(self, request): content_type_to_acceptable = lambda content_type: AcceptParameters( ContentType(content_type)) acceptable = map(content_type_to_acceptable, self.content_type_providers().keys()) preferred_content_type = self.preferred_content_type accept = request.headers.get('ACCEPT') negotiator = ContentNegotiator( content_type_to_acceptable(preferred_content_type), acceptable) negotiated_type = negotiator.negotiate(accept=accept) content_type = negotiated_type.content_type or preferred_content_type provider = self.content_type_providers()[str(content_type)] return provider()