from typing import Any
from cow.web.handlers import BaseHandler
from sanic.response import HTTPResponse, JSONResponse
from luna_lambda_tools.private.request import BaseStandaloneLambdaRequest
[docs]
class LambdaUserHandler(BaseHandler):
"""Base class for user handlers"""
route: str
request: BaseStandaloneLambdaRequest
def sendResponse(
self, statusCode: int = 200, json: dict | None = None, body: Any | None = None, headers: dict | None = None
):
"""
Send response
Args:
statusCode: response status code, range(200, 300), default 200
body: pure body
json: json body
headers: respnse headers
Returns:
aiohttp.web.Response object
Raises:
ValueError: if 'body' and 'json' set at once
"""
if json is not None and body is not None:
raise ValueError("'json' and 'body' parameters can`t be specified at once")
if json is not None:
reply = JSONResponse(body=json, status=statusCode, content_type="application/json", headers=headers)
else:
reply = HTTPResponse(body=body, status=statusCode, headers=headers)
return reply