-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- add file upload - apidocs generation fix for form
- Loading branch information
Showing
12 changed files
with
151 additions
and
51 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,6 @@ | ||
from .form import form_payload, form_schema | ||
from .header import header, header_schema | ||
from .json import json_payload, json_schema | ||
from .path import path, path_schema | ||
from .query import query, query_schema | ||
from .form import form_payload, form_schema | ||
from .upload import upload |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,76 @@ | ||
import abc | ||
from typing import Optional, Any, Sequence, Mapping, Type | ||
|
||
from marshmallow import ValidationError | ||
from starlette.datastructures import UploadFile | ||
from starlette.requests import Request | ||
|
||
from .base import Parser | ||
|
||
__all__ = ('upload',) | ||
|
||
|
||
class UploadSequence(Sequence[UploadFile], metaclass=abc.ABCMeta): | ||
pass | ||
|
||
|
||
def upload(*args: str, | ||
description: Optional[str] = None, | ||
required: bool = False) -> Type[UploadSequence]: | ||
def helper() -> Any: | ||
return UploadParser(args, description=description, required=required) | ||
|
||
return helper() | ||
|
||
|
||
class UploadParser(Parser): | ||
|
||
def __init__(self, file_names: Sequence[str] = (), *, | ||
description: Optional[str] = None, | ||
required: bool = False): | ||
self.files_names = frozenset(file_names) | ||
self.description = description | ||
self.required = required | ||
|
||
@property | ||
def parser(self): | ||
return self | ||
|
||
@property | ||
def media_type(self): | ||
return 'multipart/form-data' | ||
|
||
@property | ||
def location(self): | ||
return 'formData' | ||
|
||
async def parse(self, request: Request): | ||
form = await request.form() | ||
res = [] | ||
for key, val in form.items(): | ||
if not isinstance(val, UploadFile): | ||
continue | ||
|
||
if not self.files_names or key in self.files_names: | ||
res.append(val) | ||
|
||
if self.required and not res: | ||
raise ValidationError(message='Missing required file', field_name='form') | ||
|
||
return res | ||
|
||
def get_spec(self): | ||
if self.files_names: | ||
for name in sorted(self.files_names): | ||
yield self._create_spec(name) | ||
else: | ||
yield self._create_spec('upfile') | ||
|
||
def _create_spec(self, name: str) -> Mapping: | ||
return { | ||
'in': 'formData', | ||
'type': 'file', | ||
'description': self.description or '', | ||
'name': name, | ||
'required': self.required | ||
} |