Source code for luna_handlers.app.handlers.handler_handler
""" Handler for a certain handler. """
from sanic.response import HTTPResponse
from app.handlers.base_handler import BaseHandler
from classes.schemas.handler import CreateHandlerModel
from crutches_on_wheels.errors.errors import Error
from crutches_on_wheels.web.query_getters import uuidGetter
[docs]class AccountHandlerHandler(BaseHandler):
    """
    Handler for work with the handler.
    Resource: "/{api_version}/handlers/{handlerId}"
    """
[docs]    async def put(self, handlerId: str) -> HTTPResponse:  # pylint: disable-msg=W0221
        """
        Update the handler. See `spec_put_handler`_.
        .. _spec_put_handler:
            _static/api.html#operation/putHandler
        Args:
            handlerId: handler id
        Returns:
            Response with empty body
        """
        handler = self.loadDataFromJson(self.request.json, CreateHandlerModel)
        if handler.isDynamic:
            policies = None
        else:
            await handler.policies.checkListsAvailability(self.luna3Client, accountId=handler.accountId)
            policies = handler.policies.asDict()
        res = await self.dbContext.putHandler(
            handlerId=handlerId,
            policies=policies,
            description=handler.description,
            accountId=str(handler.accountId),
            isDynamic=handler.isDynamic,
        )
        if res:
            return self.success(204)
        return self.error(404, Error.HandlerNotFound.format(handlerId)) 
[docs]    async def get(self, handlerId: str) -> HTTPResponse:
        """
        Get handler by id. See `spec_get_handler`_.
        .. _spec_get_handler:
            _static/api.html#operation/getHandler
        Args:
            handlerId: handler id
        Returns:
            handler in response
        """
        accountId = self.getQueryParam("account_id", uuidGetter, require=False)
        handler = await self.dbContext.getHandler(handlerId, accountId=accountId)
        return self.success(200, outputJson=handler) 
[docs]    async def head(self, handlerId: str) -> HTTPResponse:
        """
        Check a handler with id=handlerId existence. See `spec_head_handler`_.
        .. _spec_head_handler:
            _static/api.html#operation/checkHandler
        Args:
            handlerId: handler id
        Returns:
            response with status code 200, if handler exist and 404 if handler does not exist
        """
        accountId = self.getQueryParam("account_id", uuidGetter, require=False)
        handlerExist = await self.dbContext.doesHandlerExist(handlerId, accountId=accountId)
        if handlerExist:
            return self.success(200, extraHeaders={"Content-Type": "application/json"})
        return self.error(404, Error.HandlerNotFound.format(handlerId)) 
[docs]    async def delete(self, handlerId: str) -> HTTPResponse:
        """
        Remove handler by id. See `spec_delete_handler`_.
        .. _spec_delete_handler:
            _static/api.html#operation/removeHandler
        Args:
            handlerId: handler id
        Returns:
            response with status code 204
        """
        accountId = self.getQueryParam("account_id", uuidGetter, require=False)
        if await self.dbContext.deleteHandler(handlerId, accountId):
            return self.success(204)
        else:
            return self.error(404, Error.HandlerNotFound.format(handlerId))