Source code for ourobori.services.http

"""
This module contains the ROUTES and basic apis to use for all http-services
based on ourobori.
The custom additional apis should be placed in the ``service.py``-file
of a service based on ourobori.
This module also defines the ``BASE_APISPECS`` to be used in your
``service.py`` file to define the apispecs used by the creation of the
swagger-definition at calling the
:func:`ourobori.docs.swagger.create_swagger_definition`.
Each service based on ourobori should also use the decorators
:func:`ourobori.services.apis.decorators.transform_input` and
:func:`ourobori.services.apis.decorators.transform_output` in its api-methods
to transform the input- and output-data of your apis.

Attributes:
    ROUTES: The routes to be used by the aiohttp.web.Application for services
        using ourobori. Services using ourobori should use this route as their
        routes-definition and add additional routes to this route.
    BASE_APISPECS: The apispecs to use for the creation of the
        swagger-documentation. Includes the basic route ``ping``.
        Services using ourobori should use this BASE_APISPECS and extend it
        with their additional swagger-definitions.
"""


from aiohttp import web
from aiohttp.web_request import Request

from ourobori_apis_schemata.schemata import SIPing
from ourobori_apis_schemata.schemata import SOPing

from ourobori.docs.swagger import APISpec
from ourobori.docs.swagger import OutputDefinition
from ourobori.docs.swagger_errors import ODInvalidInputException
from ourobori.docs.swagger_errors import ODInvalidOutputException
from ourobori.services.apis.decorators import input_transform
from ourobori.services.apis.decorators import output_transform


ROUTES = web.RouteTableDef()


[docs]@ROUTES.post('/ping') @input_transform(schema=SIPing) @output_transform(schema=SOPing) async def api_ping(request: Request) -> str: """ Basic ping-api using the decorators :func:`input_transform` and :func:`output_transform` to transform the input- and output-data from and to json. The decorator :func:`input-transform` adds two additional attributes to the request-object: request.api_id (the id for this api-call) and request.api_params (the extracted request-params). Parameters: request: The request-object passed to this api-method Returns: result: The result converted to json using :class:`SOPing` """ result = dict(pong=request.api_params.ping, status=200) return result
# This is the base apispec-definition to be used and extended by services using # ourobori BASE_APISPECS = [ APISpec( api_url='/ping', description='some description', input_description='input for the ping-api', input_schema=SIPing, output_description='output from the ping-api', output_schema=SOPing, additional_output_definitions=[ ODInvalidInputException, ODInvalidOutputException] ) ] __all__ = ['api_ping', 'BASE_APISPECS', 'ROUTES', ]