Esempio n. 1
0
async def delete_pet(request: web.Request) -> web.Response:
    with openapi_context(request) as context:
        pet = get_pet_or_404(
            request.app["pets"], context.parameters.path["id"]
        )
        request.app["pets"] = [
            item for item in request.app["pets"] if item != pet
        ]
        return web.json_response(status=204)
Esempio n. 2
0
async def create_pet(request: web.Request) -> web.Response:
    with openapi_context(request) as context:
        new_pet = NewPet(
            name=context.data["name"], tag=context.data.get("tag")
        )

        pet = new_pet.to_pet(len(request.app["pets"]) + 1)
        request.app["pets"].append(pet)

    return web.json_response(attr.asdict(pet))
Esempio n. 3
0
async def retrieve_repository(request: web.Request) -> web.Response:
    with openapi_context(request) as context:
        owner = context.parameters.path["owner"]
        repository = (GITHUB_REPOSITORIES.get(owner) or {}).get(
            context.parameters.path["name"]
        )

        if not repository:
            raise ObjectDoesNotExist("Repository")

        return web.json_response(repository)
Esempio n. 4
0
async def list_pets(request: web.Request) -> web.Response:
    with openapi_context(request) as context:
        limit = context.parameters.query.get("limit")
        tags = set(context.parameters.query.get("pets") or [])

        found = [
            item
            for item in request.app["pets"]
            if not tags or (tags and item.tag in tags)
        ]

        return web.json_response([attr.asdict(item) for item in found[:limit]])
Esempio n. 5
0
    async def decorator(request: web.Request) -> web.StreamResponse:
        with openapi_context(request) as context:
            username = context.parameters.header["X-GitHub-Username"]

            basic_auth = context.security.get("basic")
            if basic_auth is not None:
                personal_token = basic_auth.password
            else:
                personal_token = (context.security.get("personalToken")
                                  or context.security["jwt"])

            authenticate(username=username, personal_token=personal_token)

        return await handler(request)
Esempio n. 6
0
async def retrieve_pet(request: web.Request) -> web.Response:
    with openapi_context(request) as context:
        pet = get_pet_or_404(
            request.app["pets"], context.parameters.path["id"]
        )
        return web.json_response(attr.asdict(pet))
Esempio n. 7
0
async def list_repositories(request: web.Request) -> web.Response:
    with openapi_context(request) as context:
        username = context.parameters.header["X-GitHub-Username"]
        return web.json_response(
            list((GITHUB_REPOSITORIES.get(username) or {}).values())
        )
Esempio n. 8
0
async def retrieve_array_from_request_body(
        request: web.Request) -> web.Response:
    with openapi_context(request) as context:
        return web.json_response(context.data)
Esempio n. 9
0
async def hello_world(request: web.Request) -> web.Response:
    with openapi_context(request) as context:
        name = context.parameters.query.get("name") or "world"
        return web.json_response({"message": f"Hello, {name}!"})