Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add JSONResponse #599

Merged
merged 2 commits into from
Oct 31, 2015
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 20 additions & 1 deletion aiohttp/web_reqrep.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,9 @@
from .streams import EOF_MARKER


__all__ = ('ContentCoding', 'Request', 'StreamResponse', 'Response')
__all__ = (
'ContentCoding', 'Request', 'StreamResponse', 'Response', 'JSONResponse'
)


sentinel = object()
Expand Down Expand Up @@ -810,3 +812,20 @@ def write_eof(self):
if body is not None:
self.write(body)
yield from super().write_eof()


def json_dumps(data):
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I suggest

class JSONResponse(Response):

    def __init__(self, data, *, body=None, status=200,
                 reason=None, headers=None, content_type='application/json',
                 dumps=json.dumps):
        super().__init__(text=text, status=status, reason=reason,
                         content_type=content_type)

return json.dumps(data).encode('utf-8')


class JSONResponse(Response):
content_type = 'application/json'

def __init__(self, data, *, body=None, status=200,
reason=None, headers=None, content_type=None,
dumps=json_dumps):
self.data = data
body = dumps(self.data)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What happens when both data & body are specified?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good point. See the updated proposal in comments

content_type = content_type or self.content_type
super().__init__(body=body, status=status, reason=reason,
content_type=content_type)
32 changes: 31 additions & 1 deletion tests/test_web_response.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,13 @@
import datetime
import json
import pytest
import re
from unittest import mock
from aiohttp import hdrs, signals
from aiohttp.multidict import CIMultiDict
from aiohttp.web import ContentCoding, Request, StreamResponse, Response
from aiohttp.web import (
ContentCoding, Request, StreamResponse, Response, JSONResponse
)
from aiohttp.protocol import HttpVersion, HttpVersion11, HttpVersion10
from aiohttp.protocol import RawRequestMessage

Expand Down Expand Up @@ -836,3 +839,30 @@ def test_text_with_empty_payload():
resp = Response(status=200)
assert resp.body is None
assert resp.text is None


class TestJSONResponse:

def test_data_attribute(self):
resp = JSONResponse('jaysawhn')
assert 'jaysawhn' == resp.data

def test_content_type_is_application_json_by_default(self):
resp = JSONResponse('')
assert 'application/json' == resp.content_type

def test_body_is_json_encoded(self):
resp = JSONResponse({'foo': 42})
assert json.dumps({'foo': 42}).encode('utf-8') == resp.body

def test_content_type_is_overrideable(self):
resp = JSONResponse({'foo': 42},
content_type='application/vnd.json+api')
assert 'application/vnd.json+api' == resp.content_type

def test_content_type_is_overrideable_as_class_var(self):
class MyJSONResponse(JSONResponse):
content_type = 'application/vnd.json+api'

resp = MyJSONResponse('jaysawhn')
assert 'application/vnd.json+api' == resp.content_type