Skip to content

Commit

Permalink
0.6.3: fix json parser
Browse files Browse the repository at this point in the history
  • Loading branch information
joway committed Jul 30, 2019
1 parent 54425f7 commit d5b08d3
Show file tree
Hide file tree
Showing 4 changed files with 13 additions and 19 deletions.
6 changes: 3 additions & 3 deletions lemon/parsers.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,11 +30,11 @@ def get_content_length(headers: dict) -> typing.Optional[int]:
raise RequestHeadersParserError


def json_parser(body: bytes, *args) -> ImmutableMultiDict:
def json_parser(body: bytes, *args) -> dict:
if not body:
raise RequestBodyParserError
try:
return ImmutableMultiDict(json.loads(body.decode('utf-8')))
return json.loads(body.decode('utf-8'))
except json.JSONDecodeError:
raise RequestBodyParserError

Expand Down Expand Up @@ -63,7 +63,7 @@ def multi_part_parser(body: bytes, headers: dict = {}) -> ImmutableMultiDict:
}


def parse_http_body(headers: dict, body: bytes) -> typing.Optional[ImmutableMultiDict]:
def parse_http_body(headers: dict, body: bytes) -> typing.Optional[typing.Any]:
content_type, _ = get_mimetype_and_options(headers=headers)
if content_type in DEFAULT_PARSERS_MAPPING:
return DEFAULT_PARSERS_MAPPING[content_type](body, headers)
Expand Down
10 changes: 1 addition & 9 deletions lemon/request.py
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ def __init__(
query_string: bytes,
headers: HttpHeaders,
body: bytes,
data: typing.Optional[ImmutableMultiDict],
data: typing.Optional[typing.Dict],
client: tuple,
server: tuple,
) -> None:
Expand Down Expand Up @@ -102,14 +102,6 @@ def query(self) -> dict:
def form(self) -> typing.Optional[ImmutableMultiDict]:
return self.data

@property
def json(self) -> dict:
"""Transform request body to dict when content_type is 'application/json'
:return: dict
"""
d = self.data.to_dict(flat=False)
return {k: d[k][0] if len(d[k]) <= 1 else d[k] for k in d.keys()}

@property
def cookies(self) -> dict:
return parse_cookie(self.headers.get('cookie'))
2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
from setuptools import setup

PACKAGE_VERSION = '0.6.2'
PACKAGE_VERSION = '0.6.3'
PACKAGE_REQUIRES = [
'uvicorn==0.8.4',
'kua==0.2',
Expand Down
14 changes: 8 additions & 6 deletions tests/test_basic.py
Original file line number Diff line number Diff line change
Expand Up @@ -40,19 +40,21 @@ async def handle(ctx: Context):

async def test_post_json(self):
async def handle(ctx: Context):
ctx.body = ctx.req.json
ctx.body = ctx.req.data
assert ctx.req.form['int'] == 1
assert ctx.req.form['str'] == 'xxx'
assert ctx.req.json['int'] == 1
assert ctx.req.json['str'] == 'xxx'
assert ctx.req.json['list'][0] == 'item1'
assert ctx.req.json['list'][1] == 'item2'
assert ctx.req.data['int'] == 1
assert ctx.req.data['str'] == 'xxx'
assert ctx.req.data['list'][0] == 'item1'
assert ctx.req.data['list'][1] == 'item2'
assert ctx.req.data['list1'][0] == 'item1'

self.app.use(handle)
req = await self.post('/', data={
'int': 1,
'str': 'xxx',
'list': ['item1', 'item2'],
'list1': ['item1'],
})
assert req.status_code == 200
data = req.json()
Expand All @@ -61,7 +63,7 @@ async def handle(ctx: Context):

async def test_post_form(self):
async def handle(ctx: Context):
data = ctx.req.json
data = ctx.req.data
ctx.body = {
'hi': data['hi'],
}
Expand Down

0 comments on commit d5b08d3

Please sign in to comment.