Source code for luna_admin.app.handlers.account_handler

"""
Module realize handlers for work with an account.
"""
from sanic.response import HTTPResponse

from app.handlers.base_handler import BaseHandler
from app.handlers.schemas import NEW_ACCOUNT_SCHEMA
from crutches_on_wheels.errors.errors import Error
from crutches_on_wheels.errors.exception import VLException


[docs]class AccountHandler(BaseHandler): """ Handler for work with an account. """ # (tuple): uri params for resource "/accounts/{account_id}" uriParams = ('accountId',)
[docs] async def get(self, accountId: str) -> HTTPResponse: """ Get account info. See `spec_get_account_info`_. .. _spec_get_account_info: _static/api.html#operation/getAccount Returns: response with account id """ acc = await self.objectGetters.getAccountInfo(accountId) if acc is None: raise VLException(Error.AccountNotFound.format(accountId), isCriticalError=False, statusCode=404) return self.success(200, outputJson=acc)
[docs] async def delete(self, accountId: str) -> HTTPResponse: """ Delete account id handler. See `spec_remove_account`_. .. _spec_remove_account: _static/api.html#operation/removeAccount Args: accountId: account id Returns: response with status code 204 """ await self.dbAdminContext.removeAccount(accountId) return self.success(204)
[docs] async def put(self, accountId: str) -> HTTPResponse: """ Register a new account with the specified id in luna-admin. See `spec_put_account`_. .. _spec_put_account: _static/api.html#operation/putAccount Returns: response with account id """ data = self.request.json self.validateJson(data, NEW_ACCOUNT_SCHEMA, False) accountId = await self.dbAdminContext.createAccount(data["email"], data["organization_name"], accountId) return self.success(200, outputJson={"account_id": accountId}, extraHeaders={"Location": f"/{self.request.app.ctx.apiVersion}/accounts/{accountId}"})