X-Git-Url: https://code.kerkeslager.com/?p=fwx;a=blobdiff_plain;f=phial.py;h=bc07b5fe77039069621ddcda9030f5945ffc7231;hp=f4da0ddfc1e2cf70614ec519954e9b2cd935414e;hb=f4de177de239a52c4780ca49a03935e36e262399;hpb=fd77e7cd13caaf823abf3bad2ebfdd6bab5f1bd1 diff --git a/phial.py b/phial.py index f4da0dd..bc07b5f 100644 --- a/phial.py +++ b/phial.py @@ -81,9 +81,45 @@ class TextResponse(Response): **kwargs, ) +_RedirectResponse = collections.namedtuple( + 'RedirectResponse', + ( + 'location', + 'permanent', + ), +) + +class RedirectResponse(_RedirectResponse): + def __new__(cls, location, **kwargs): + assert isinstance(location, str) + + permanent = kwargs.pop('permanent', True) + assert isinstance(permanent, bool) + assert len(kwargs) == 0 + + return super().__new__( + cls, + location=location, + permanent=permanent, + ) + + @property + def status(self): + return 308 if self.permanent else 307 + + @property + def headers(self): + return (('Location', self.location),) + + @property + def content(self): + return (b'',) + def _get_status(response): return { 200: '200 OK', + 307: '307 Temporary Redirect', + 308: '308 Permanent Redirect', }[response.status] def _get_headers(response):