Example #1
0
def make_app(config_args: Optional[List] = None):
    """
    Read config and launch asterios server.
    """
    config = get_config(config_args)
    for level_package in config["level_package"]:
        MetaLevel.load_level(level_package)

    app = web.Application(middlewares=[error_middleware])
    setup_routes(app)
    app["config"] = config
    app["model"] = Model()

    if config.get("authentication"):
        user_map = {
            config["authentication"]["superuser"]["login"]: {
                "login": config["authentication"]["superuser"]["login"],
                "password": config["authentication"]["superuser"]["password"],
                "role": "superuser",
            }
        }
        setup_security(app, BasicAuthIdentityPolicy(user_map),
                       AuthorizationPolicy(user_map))

    oas.setup(app)
    return app
Example #2
0
async def generated_oas(aiohttp_client, loop) -> web.Application:
    app = web.Application()
    app.router.add_view("/pets", PetCollectionView)
    app.router.add_view("/pets/{id}", PetItemView)
    app.router.add_view("/simple-type", ViewResponseReturnASimpleType)
    oas.setup(app)

    return await ensure_content_durability(await aiohttp_client(app))
Example #3
0
async def generated_oas(aiohttp_client, loop) -> web.Application:
    app = web.Application()
    app.router.add_view("/pets", PetCollectionView)
    app.router.add_view("/pets/{id}", PetItemView)
    app.router.add_view("/simple-type", ViewResponseReturnASimpleType)
    oas.setup(app)

    client = await aiohttp_client(app)
    response_1 = await client.get("/oas/spec")
    assert response_1.content_type == "application/json"
    assert response_1.status == 200
    content_1 = await response_1.json()

    # Reload the page to ensure that content is always the same
    # note: pydantic can return a cached dict, if a view updates
    # the dict the output will be incoherent
    response_2 = await client.get("/oas/spec")
    content_2 = await response_2.json()
    assert content_1 == content_2

    return content_2
Example #4
0
async def test_use_parameters_group_should_not_impact_the_oas(aiohttp_client):
    class PetCollectionView1(PydanticView):
        async def get(self,
                      page: int = 1,
                      page_size: int = 20) -> r200[List[Pet]]:
            return web.json_response()

    class PetCollectionView2(PydanticView):
        async def get(self, pagination: Pagination) -> r200[List[Pet]]:
            return web.json_response()

    app1 = web.Application()
    app1.router.add_view("/pets", PetCollectionView1)
    oas.setup(app1)

    app2 = web.Application()
    app2.router.add_view("/pets", PetCollectionView2)
    oas.setup(app2)

    assert await ensure_content_durability(
        await aiohttp_client(app1)
    ) == await ensure_content_durability(await aiohttp_client(app2))
Example #5
0
from aiohttp.web import Application, json_response, middleware

from aiohttp_pydantic import oas

from .model import Model
from .view import PetCollectionView, PetItemView


@middleware
async def pet_not_found_to_404(request, handler):
    try:
        return await handler(request)
    except Model.NotFound as key:
        return json_response({"error": f"Pet {key} does not exist"},
                             status=404)


app = Application(middlewares=[pet_not_found_to_404])
oas.setup(app, version_spec="1.0.1", title_spec="My App")

app["model"] = Model()
app.router.add_view("/pets", PetCollectionView)
app.router.add_view("/pets/{id}", PetItemView)