def test_is_mimetype_a(self): """Testing djblets.util.http.is_mimetype_a""" self.assertTrue(is_mimetype_a('application/json', 'application/json')) self.assertTrue( is_mimetype_a('application/vnd.foo+json', 'application/json')) self.assertFalse(is_mimetype_a('application/xml', 'application/json')) self.assertFalse(is_mimetype_a('foo/vnd.bar+json', 'application/json'))
def test_is_mimetype_a(self): """Testing djblets.util.http.is_mimetype_a""" self.assertTrue(is_mimetype_a('application/json', 'application/json')) self.assertTrue(is_mimetype_a('application/vnd.foo+json', 'application/json')) self.assertFalse(is_mimetype_a('application/xml', 'application/json')) self.assertFalse(is_mimetype_a('foo/vnd.bar+json', 'application/json'))
def _get_content(self): """Returns the API response content in the appropriate format. This is an overridden version of HttpResponse._get_content that generates the resulting content when requested, rather than generating it up-front in the constructor. This is used so that the @webapi decorator can set the appropriate API format before the content is generated, but after the response is created. """ class MultiEncoder(WebAPIEncoder): def __init__(self, encoders): self.encoders = encoders def encode(self, *args, **kwargs): for encoder in self.encoders: result = encoder.encode(*args, **kwargs) if result is not None: return result return None if not self.content_set: adapter = None encoder = MultiEncoder(self.encoders) # See the note above about the check for text/plain. if (self.mimetype == 'text/plain' or is_mimetype_a(self.mimetype, 'application/json')): adapter = JSONEncoderAdapter(encoder) elif is_mimetype_a(self.mimetype, "application/xml"): adapter = XMLEncoderAdapter(encoder) else: assert False content = adapter.encode(self.api_data, request=self.request, **self.encoder_kwargs) if self.callback is not None: content = "%s(%s);" % (self.callback, content) self.content = content self.content_set = True return super(WebAPIResponse, self).content
def build_example(data, mimetype): if not data: return None language = None for base_mimetype, lang in MIMETYPE_LANGUAGES: if is_mimetype_a(mimetype, base_mimetype): language = lang break if language == 'javascript': code = json.dumps(json.loads(data), sort_keys=True, indent=2) else: code = data return nodes.literal_block(code, code, language=language)
def build_example(headers, data, mimetype): if not data: return None language = None for base_mimetype, lang in MIMETYPE_LANGUAGES: if is_mimetype_a(mimetype, base_mimetype): language = lang break if language == "javascript": code = json.dumps(json.loads(data), sort_keys=True, indent=2) else: code = data return nodes.literal_block(code, code, language=language or "text", classes=["example-payload"])