-
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.
Merge pull request #3 from lazy-labs/0.0.15
0.0.15
- Loading branch information
Showing
6 changed files
with
100 additions
and
18 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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -3,6 +3,7 @@ typing_extensions | |
marshmallow>=3.0.0rc8,<4 | ||
starlette<1 | ||
apispec<4 | ||
python-multipart | ||
|
||
# Testing | ||
pytest | ||
|
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,42 @@ | ||
import types | ||
from typing import Mapping, Type, TypeVar, Union | ||
|
||
from marshmallow import EXCLUDE, Schema | ||
from starlette.requests import Request | ||
|
||
from star_resty.exceptions import DecodeError | ||
from .parser import Parser, set_parser | ||
|
||
__all__ = ('form_schema', 'form_payload', 'FormParser') | ||
|
||
P = TypeVar('P') | ||
|
||
|
||
def form_schema(schema: Union[Schema, Type[Schema]], cls: P, | ||
unknown: str = EXCLUDE) -> P: | ||
return types.new_class('FormDataInputParams', (cls,), | ||
exec_body=set_parser(FormParser.create(schema, unknown=unknown))) | ||
|
||
|
||
def form_payload(schema: Union[Schema, Type[Schema]], unknown=EXCLUDE) -> Type[Mapping]: | ||
return form_schema(schema, Mapping, unknown=unknown) | ||
|
||
|
||
class FormParser(Parser): | ||
__slots__ = () | ||
|
||
@property | ||
def location(self): | ||
return 'body' | ||
|
||
@property | ||
def media_type(self): | ||
return 'multipart/form-data' | ||
|
||
async def parse(self, request: Request): | ||
try: | ||
form_data = await request.form() | ||
form_data = {} if not form_data else form_data | ||
except Exception as e: | ||
raise DecodeError('Invalid form data: %s' % (str(e))) from e | ||
return self.schema.load(form_data, unknown=self.unknown) |