Source code for luna_tasks.crutches_on_wheels.web.configs_handler
# -*- coding: utf-8 -*-
"""Configs Handler
Module realizes configs handler.
"""
from sanic.response import HTTPResponse
from ..errors.errors import Error
from ..errors.exception import VLException
from ..web.handlers import BaseHandler
from .application import LunaApplication
[docs]class ConfigsHandler(BaseHandler):
"""
Handler for getting actual service configuration
"""
@property
def app(self) -> LunaApplication:
"""
Get application
Returns:
running application
"""
return self.request.app
@property
def config(self):
"""
Get running application config
Returns:
running application config
"""
return self.request.app.ctx.serviceConfig
[docs] async def get(self) -> HTTPResponse: # pylint: disable-msg=W0221
"""
Get service configuration, see `get_config`_.
.. _get_config:
_static/api.html#operation/getConfig
Resource is reached by address '/config'
Raises:
VLException(Error.UnsupportedMediaType, 415, isCriticalError=False), if "Accept" header is incorrect
"""
acceptHeaders = [row.split(";")[0] for row in self.request.headers.get("Accept", "application/json").split(",")]
acceptJson = "application/json" in acceptHeaders
acceptText = "text/plain" in acceptHeaders
acceptAny = "*/*" in acceptHeaders
if acceptJson or acceptAny:
config: dict = self.config.getConfigAsFlattenDict()
return self.success(outputJson=config, contentType="application/json")
if acceptText:
config: str = self.config.getConfigAsText()
return self.success(body=config, contentType="text/plain")
raise VLException(Error.UnsupportedMediaType, 415, isCriticalError=False)
[docs]def addConfigHandlerRoutes(app: LunaApplication) -> None:
"""
Add standard configs handler to app
Args:
app: application
"""
app.add_route(ConfigsHandler.as_view(), rf"/{app.ctx.apiVersion}/config", strict_slashes=True)