Skip to content

Commit

Permalink
chore(openapi): Provide 2 shortcut functions to get validated data / …
Browse files Browse the repository at this point in the history
…parameters.

It might be useful to get validate data / parameters from request with
`openapi_context` context manager or `get_openapi_context` function, but
via high order shortcut function.
  • Loading branch information
playpauseandstop committed Jan 22, 2020
1 parent c668ca8 commit 9c06295
Show file tree
Hide file tree
Showing 5 changed files with 43 additions and 20 deletions.
2 changes: 2 additions & 0 deletions docs/api.rst
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@ OpenAPI
.. autofunction:: rororo.openapi.get_openapi_context
.. autofunction:: rororo.openapi.get_openapi_schema
.. autofunction:: rororo.openapi.get_openapi_spec
.. autofunction:: rororo.openapi.get_validated_data
.. autofunction:: rororo.openapi.get_validated_parameters

Settings
========
Expand Down
25 changes: 10 additions & 15 deletions examples/petstore/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
from aiohttp import web

from rororo import openapi_context, OperationTableDef
from rororo.openapi import get_validated_data, get_validated_parameters
from .data import NewPet
from .shortcuts import get_pet_or_404

Expand All @@ -16,27 +17,21 @@

@operations.register("addPet")
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")
)
data = get_validated_data(request)
new_pet = NewPet(name=data["name"], tag=data.get("tag"))

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

return web.json_response(pet.to_dict())
return web.json_response(pet.to_dict())


@operations.register("deletePet")
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)
pet_id = get_validated_parameters(request).path["id"]
pet = get_pet_or_404(request.app["pets"], pet_id)
request.app["pets"] = [item for item in request.app["pets"] if item != pet]
return web.json_response(status=204)


@operations.register("findPets")
Expand Down
10 changes: 9 additions & 1 deletion rororo/openapi/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,13 @@

from .contexts import openapi_context
from .openapi import OperationTableDef, setup_openapi
from .utils import get_openapi_context, get_openapi_schema, get_openapi_spec
from .utils import (
get_openapi_context,
get_openapi_schema,
get_openapi_spec,
get_validated_data,
get_validated_parameters,
)
from .views import default_error_handler


Expand All @@ -19,6 +25,8 @@
"get_openapi_context",
"get_openapi_schema",
"get_openapi_spec",
"get_validated_data",
"get_validated_parameters",
"openapi_context",
"OperationTableDef",
"setup_openapi",
Expand Down
22 changes: 20 additions & 2 deletions rororo/openapi/utils.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
from typing import Optional, Union
from typing import Any, Optional, Union

from aiohttp import web
from aiohttp.helpers import ChainMapProxy
Expand All @@ -9,7 +9,7 @@
APP_OPENAPI_SPEC_KEY,
REQUEST_OPENAPI_CONTEXT_KEY,
)
from .data import OpenAPIContext, OpenAPIOperation
from .data import OpenAPIContext, OpenAPIOperation, OpenAPIParameters
from .exceptions import ConfigurationError, ContextError, OperationError
from ..annotations import DictStrAny

Expand Down Expand Up @@ -101,3 +101,21 @@ def get_openapi_operation(
f'Unable to find operation "{operation_id}" in provided OpenAPI '
"Schema."
)


def get_validated_data(request: web.Request) -> Any:
"""Shortcut to get validated data (request body) for given request.
In case when current request has no valid OpenAPI context attached -
``ContextError`` will be raised.
"""
return get_openapi_context(request).data


def get_validated_parameters(request: web.Request) -> OpenAPIParameters:
"""Shortcut to get validated parameters for given request.
In case when current request has no valid OpenAPI context attached -
``ContextError`` will be raised.
"""
return get_openapi_context(request).parameters
4 changes: 2 additions & 2 deletions tests/test_openapi.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
setup_openapi,
setup_settings,
)
from rororo.openapi import get_validated_data
from rororo.openapi.exceptions import ConfigurationError, OperationError
from rororo.openapi.mappings import enforce_dicts

Expand Down Expand Up @@ -70,8 +71,7 @@ async def hello_world(request: web.Request) -> web.Response:
async def retrieve_any_object_from_request_body(
request: web.Request,
) -> web.Response:
with openapi_context(request) as context:
return web.json_response(enforce_dicts(context.data))
return web.json_response(enforce_dicts(get_validated_data(request)))


@operations.register
Expand Down

0 comments on commit 9c06295

Please sign in to comment.