Source code for luna_configurator.app.handlers.puller_handler
# -*- coding: utf-8 -*-
"""Puller Handler
Module realize puller handler.Allows to get the corresponding JSON scheme
"""
from sanic.response import HTTPResponse
from app.handlers.schemas import TAGS_SCHEMA
from crutches_on_wheels.errors.errors import Error
from crutches_on_wheels.errors.exception import VLException
from .base_handler import BaseRequestHandler
from .functions import jsonToConfigFileConverter
[docs]class PullerHandler(BaseRequestHandler):
    """
    Handler to get settings for exact service.
    """
[docs]    async def post(self, serviceName: str) -> HTTPResponse:
        """
        Request to receive most relevant settings for exact service. See `spec getPuller`_.
        .. _`spec getPuller`:
            _static/api.html#operation/getPuller
        """
        settingNames = [
            limitation["limitation_name"]
            for limitation in (await self.dbContext.getLimitations())
            if serviceName in limitation["services"]
        ]
        if self.request.content_type == "application/json":
            settingNameAndTags = self.request.json
            self.validateJson(settingNameAndTags, TAGS_SCHEMA, useJsonSchema=False)
        else:
            settingNameAndTags = {}
        settings = await self.dbContext.getSettingsForPuller(settingNames, settingNameAndTags)
        acceptHeaders = self.request.headers.get("Accept", "application/json")
        if "application/x-vl-config-file" in acceptHeaders:
            fileBody = jsonToConfigFileConverter(settings)
            return self.success(body=fileBody, contentType="application/x-vl-config-file", statusCode=200)
        elif "application/json" in acceptHeaders or "*/*" in acceptHeaders:
            return self.success(outputJson=settings, statusCode=200, contentType="application/json; charset=utf-8")
        else:
            raise VLException(
                Error.NotAcceptable.format("application/x-vl-config-file, " "application/json"),
                406,
                isCriticalError=False,
            )