This repository has been archived by the owner on Feb 25, 2022. It is now read-only.
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* add .pytest_cache to .gitignore * add URLEncodedCodec and MultiPartCodec
- Loading branch information
1 parent
3a8e199
commit df5bc01
Showing
5 changed files
with
91 additions
and
11 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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -3,6 +3,7 @@ | |
.coverage | ||
.git | ||
.mypy_cache | ||
.pytest_cache | ||
__pycache__ | ||
/*.egg-info | ||
/htmlcov | ||
|
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,11 +1,12 @@ | ||
from apistar.codecs.base import BaseCodec | ||
from apistar.codecs.download import DownloadCodec | ||
from apistar.codecs.formdata import MultiPartCodec, URLEncodedCodec | ||
from apistar.codecs.jsondata import JSONCodec | ||
from apistar.codecs.jsonschema import JSONSchemaCodec | ||
from apistar.codecs.openapi import OpenAPICodec | ||
from apistar.codecs.text import TextCodec | ||
|
||
__all__ = [ | ||
'BaseCodec', 'JSONCodec', 'JSONSchemaCodec', 'OpenAPICodec', 'TextCodec', | ||
'DownloadCodec' | ||
'DownloadCodec', 'MultiPartCodec', 'URLEncodedCodec', | ||
] |
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,36 @@ | ||
from io import BytesIO | ||
from itertools import chain | ||
|
||
from werkzeug.datastructures import ImmutableMultiDict | ||
from werkzeug.formparser import FormDataParser | ||
from werkzeug.http import parse_options_header | ||
from werkzeug.urls import url_decode | ||
|
||
from apistar.codecs.base import BaseCodec | ||
|
||
|
||
class MultiPartCodec(BaseCodec): | ||
media_type = 'multipart/form-data' | ||
|
||
def decode(self, bytestring, headers, **options): | ||
try: | ||
content_length = max(0, int(headers['content-length'])) | ||
except (KeyError, ValueError, TypeError): | ||
content_length = None | ||
|
||
try: | ||
mime_type, mime_options = parse_options_header(headers['content-type']) | ||
except KeyError: | ||
mime_type, mime_options = '', {} | ||
|
||
body_file = BytesIO(bytestring) | ||
parser = FormDataParser() | ||
stream, form, files = parser.parse(body_file, mime_type, content_length, mime_options) | ||
return ImmutableMultiDict(chain(form.items(), files.items())) | ||
|
||
|
||
class URLEncodedCodec(BaseCodec): | ||
media_type = 'application/x-www-form-urlencoded' | ||
|
||
def decode(self, bytestring, **options): | ||
return url_decode(bytestring, cls=ImmutableMultiDict) |
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