X-Git-Url: https://code.kerkeslager.com/?p=fwx;a=blobdiff_plain;f=phial.py;fp=phial.py;h=f4da0ddfc1e2cf70614ec519954e9b2cd935414e;hp=f76a5dcbb464657556bc30db4a17566b46575adc;hb=fd77e7cd13caaf823abf3bad2ebfdd6bab5f1bd1;hpb=f80262bbe18c56836e612e664d9824ff72d2ca1d diff --git a/phial.py b/phial.py index f76a5dc..f4da0dd 100644 --- a/phial.py +++ b/phial.py @@ -1,4 +1,5 @@ import collections +import json Request = collections.namedtuple( 'Request', @@ -44,9 +45,35 @@ class Response(_Response): ('Content-Type', self.content_type), ) +class HTMLResponse(Response): + def __new__(cls, content, **kwargs): + assert 'content_type' not in kwargs + + return super().__new__( + cls, + content, + content_type='text/html', + **kwargs, + ) + +class JSONResponse(Response): + def __new__(cls, content_json, **kwargs): + assert 'content_type' not in kwargs + assert 'content' not in kwargs + + self = super().__new__( + cls, + content=json.dumps(content_json), + content_type='application/json', + **kwargs, + ) + self.content_json = content_json + return self + class TextResponse(Response): def __new__(cls, content, **kwargs): assert 'content_type' not in kwargs + return super().__new__( cls, content,