| 1 |
import copy |
| 2 |
from functools import partial |
| 3 |
|
| 4 |
|
| 5 |
VALID_SCHEMA_LOCATIONS = ( |
| 6 |
"cookies", |
| 7 |
"files", |
| 8 |
"form", |
| 9 |
"headers", |
| 10 |
"json", |
| 11 |
"match_info", |
| 12 |
"path", |
| 13 |
"query", |
| 14 |
"querystring", |
| 15 |
) |
| 16 |
|
| 17 |
|
| 18 |
def request_schema( |
| 19 |
schema, location="json", put_into=None, example=None, add_to_refs=False, **kwargs |
| 20 |
): |
| 21 |
""" |
| 22 |
Add request info into the swagger spec and |
| 23 |
prepare injection keyword arguments from the specified |
| 24 |
webargs arguments into the decorated view function in |
| 25 |
request['data'] for validation_middleware validation middleware. |
| 26 |
|
| 27 |
Usage: |
| 28 |
|
| 29 |
.. code-block:: python |
| 30 |
|
| 31 |
from aiohttp import web |
| 32 |
from marshmallow import Schema, fields |
| 33 |
|
| 34 |
|
| 35 |
class RequestSchema(Schema): |
| 36 |
id = fields.Int() |
| 37 |
name = fields.Str(description='name') |
| 38 |
|
| 39 |
@request_schema(RequestSchema(strict=True)) |
| 40 |
async def index(request): |
| 41 |
# aiohttp_apispec_middleware should be used for it |
| 42 |
data = request['data'] |
| 43 |
return web.json_response({'name': data['name'], |
| 44 |
'id': data['id']}) |
| 45 |
|
| 46 |
:param schema: :class:`Schema <marshmallow.Schema>` class or instance |
| 47 |
:param location: Default request locations to parse |
| 48 |
:param put_into: name of the key in Request object |
| 49 |
where validated data will be placed. |
| 50 |
If None (by default) default key will be used |
| 51 |
:param dict example: Adding example for current schema |
| 52 |
:param bool add_to_refs: Working only if example not None, |
| 53 |
if True, add example for ref schema. |
| 54 |
Otherwise add example to endpoint. |
| 55 |
Default False |
| 56 |
""" |
| 57 |
|
| 58 |
if location not in VALID_SCHEMA_LOCATIONS: |
| 59 |
raise ValueError(f"Invalid location argument: {location}") |
| 60 |
|
| 61 |
if callable(schema): |
| 62 |
schema = schema() |
| 63 |
|
| 64 |
options = {"required": kwargs.pop("required", False)} |
| 65 |
|
| 66 |
def wrapper(func): |
| 67 |
if not hasattr(func, "__apispec__"): |
| 68 |
func.__apispec__ = {"schemas": [], "responses": {}, "parameters": []} |
| 69 |
func.__schemas__ = [] |
| 70 |
|
| 71 |
_example = copy.copy(example) or {} |
| 72 |
if _example: |
| 73 |
_example['add_to_refs'] = add_to_refs |
| 74 |
func.__apispec__["schemas"].append( |
| 75 |
{ |
| 76 |
"schema": schema, |
| 77 |
"location": location, |
| 78 |
"options": options, |
| 79 |
"example": _example, |
| 80 |
} |
| 81 |
) |
| 82 |
|
| 83 |
|
| 84 |
|
| 85 |
if location == "json" and any( |
| 86 |
func_schema["location"] == "json" for func_schema in func.__schemas__ |
| 87 |
): |
| 88 |
raise RuntimeError("Multiple json locations are not allowed") |
| 89 |
|
| 90 |
func.__schemas__.append( |
| 91 |
{"schema": schema, "location": location, "put_into": put_into} |
| 92 |
) |
| 93 |
|
| 94 |
return func |
| 95 |
|
| 96 |
return wrapper |
| 97 |
|
| 98 |
|
| 99 |
|
| 100 |
use_kwargs = request_schema |
| 101 |
|
| 102 |
|
| 103 |
match_info_schema = partial( |
| 104 |
request_schema, location="match_info", put_into="match_info" |
| 105 |
) |
| 106 |
querystring_schema = partial( |
| 107 |
request_schema, location="querystring", put_into="querystring" |
| 108 |
) |
| 109 |
form_schema = partial(request_schema, location="form", put_into="form") |
| 110 |
json_schema = partial(request_schema, location="json", put_into="json") |
| 111 |
headers_schema = partial(request_schema, location="headers", put_into="headers") |
| 112 |
cookies_schema = partial(request_schema, location="cookies", put_into="cookies") |