async def _handle_metrics(self, request): """Handler for metrics.""" if self._update_handler: await self._update_handler(self.registry.get_metrics()) response = Response(body=self.registry.generate_metrics()) response.content_type = CONTENT_TYPE_LATEST return response
def test_set_text_with_content_type() -> None: resp = Response() resp.content_type = "text/html" resp.text = "text" assert "text" == resp.text assert b"text" == resp.body assert "text/html" == resp.content_type
def test_set_text_with_content_type(self): resp = Response() resp.content_type = "text/html" resp.text = "text" self.assertEqual("text", resp.text) self.assertEqual(b"text", resp.body) self.assertEqual("text/html", resp.content_type)
def test_set_text_with_content_type(): resp = Response() resp.content_type = "text/html" resp.text = "text" assert "text" == resp.text assert b"text" == resp.body assert "text/html" == resp.content_type
def test_set_text_with_charset() -> None: resp = Response() resp.content_type = "text/plain" resp.charset = "KOI8-R" resp.text = "текст" assert "текст" == resp.text assert "текст".encode("koi8-r") == resp.body assert "koi8-r" == resp.charset
def test_set_text_with_charset(): resp = Response() resp.content_type = 'text/plain' resp.charset = "KOI8-R" resp.text = "текст" assert "текст" == resp.text assert "текст".encode('koi8-r') == resp.body assert "koi8-r" == resp.charset
def test_set_text_with_charset(self): resp = Response() resp.content_type = 'text/plain' resp.charset = "KOI8-R" resp.text = "текст" self.assertEqual("текст", resp.text) self.assertEqual("текст".encode('koi8-r'), resp.body) self.assertEqual("koi8-r", resp.charset)
async def transform_client_exception_to_json(request: web.Request, response: web.Response) -> None: if 400 <= response.status < 500: response.content_type = "application/json" try: json.loads(response.body) except json.JSONDecodeError: response.body = HTTPClientErrorSchema().dumps({ "detail": response.body.decode("utf-8") }).encode("utf-8")
async def post(self, request): payload = await request.json() jsonschema.validate(payload, AUTHENTICATE_CREATE, format_checker=jsonschema.draft4_format_checker) payload = payload.get('data') user = await self.users.check_credentials(payload) result = await self.sessions.create(user) response = Response(text=json.dumps(result)) response.content_type = 'application/json' response.set_status(201) response.set_cookie('SESSION', result['data']['id'], httponly=True) return response
async def get(self): try: registry = CollectorRegistry() multiprocess.MultiProcessCollector(registry) response = Response() response.body = generate_latest(registry) response.content_type = CONTENT_TYPE_LATEST return response except Exception as ex: logging.warning(str(ex)) traceback.print_exc() raise web.HTTPBadRequest(body=json.dumps({'message': str(ex)}))
async def change_body(request): resp = Response() resp.body = b"Body changed" resp.content_type = 'text/plain' return resp
async def hrprometheus_view(request): resp = Response(body=generate_latest()) resp.content_type = CONTENT_TYPE_LATEST return resp