3 Request = collections.namedtuple(
10 _Response = collections.namedtuple(
20 class Response(_Response):
21 def __new__(cls, **kwargs):
22 status = kwargs.pop('status', 200)
23 assert isinstance(status, int)
25 content_type = kwargs.pop('content_type')
26 assert isinstance(content_type, str)
28 extra_headers = kwargs.pop('extra_headers', ())
29 assert isinstance(extra_headers, tuple)
31 content = kwargs.pop('content')
33 assert len(kwargs) == 0
35 return super().__new__(
38 content_type=content_type,
39 extra_headers=extra_headers,
46 ('Content-Type', self.content_type),
49 def _get_status(response):
54 def _get_headers(response):
55 return list(response.headers)
57 def _get_content(response):
58 content = response.content
60 if isinstance(content, bytes):
63 if isinstance(content, str):
64 return (content.encode('utf-8'),)
69 def app(environ, start_fn):
70 response = handler(Request(environ))
72 start_fn(_get_status(response), _get_headers(response))
73 return _get_content(response)