zoom.response module

zoom.response

Various common web responses.

Note: We have chosen to use a dict for the headers even though technically the HTTP spec allows for multiple values by the same name because the uses cases for this seem to be very obscure and the benefits of not duplicating header entries that the dict provides seem to outweigh supporting obscure and generally not recommend use cases. The only use case where this is more commonly used is in cookies, but we deal with that special case in the cookie module.

class zoom.response.BinaryResponse(content, max_age=86400)

Bases: zoom.response.Response

Generic binary response

use max_age=0 to avoid caching

>>> response = BinaryResponse(b'binary data')
>>> expected = (
...     b'Status: 200 OK\n'
...     b'Content-type: application/octet-stream\n'
...     b'Cache-Control: max-age=86400\n'
...     b'ETag: e1a49b59e\n'
...     b'Content-length: 11\n\n'
...     b'binary data'
... )
>>> response.render() == expected
True
class zoom.response.CSSResponse(content, max_age=86400)

Bases: zoom.response.Response

CSS response

>>> response = CSSResponse(b'mycss')
>>> expected = (
...     b'Status: 200 OK\n'
...     b'Content-type: text/css;charset=utf-8\n'
...     b'Cache-Control: max-age=86400\n'
...     b'ETag: 12a586855\n'
...     b'Content-length: 5\n\n'
...     b'mycss'
... )
>>> response.render() == expected
True
class zoom.response.FileResponse(filename, content=None)

Bases: zoom.response.Response

File download response

>>> response = FileResponse('file.txt', content=b'mydata')
>>> expected = (
...     b'Status: 200 OK\n'
...     b'Content-type: application/octet-stream\n'
...     b'Content-Disposition: attachment; filename="file.txt"\n'
...     b'Cache-Control: no-cache\n'
...     b'Content-length: 6\n\n'
...     b'mydata'
... )
>>> response.render() == expected
True
class zoom.response.GIFResponse(content)

Bases: zoom.response.Response

GIF image response

>>> response = GIFResponse(b'myimage')
>>> expected = (
...     b'Status: 200 OK\n'
...     b'Content-type: image/gif\n'
...     b'Content-length: 7\n\n'
...     b'myimage'
... )
>>> response.render() == expected
True
class zoom.response.HTMLResponse(content='', status='200 OK')

Bases: zoom.response.TextResponse

HTML response

>>> HTMLResponse('test123').render() == (
...     b'Status: 200 OK\n'
...     b'Content-type: text/html\n'
...     b'Cache-Control: no-cache\n'
...     b'X-FRAME-OPTIONS: DENY\n'
...     b'Content-length: 7\n\n'
...     b'test123'
... )
True
>>> HTMLResponse('test123').as_wsgi() == (
...    '200 OK',
...    [
...       ('Content-type', 'text/html'),
...       ('Cache-Control', 'no-cache'),
...       ('X-FRAME-OPTIONS', 'DENY'),
...       ('Content-length', '7')
...    ],
...    b'test123'
... )
True
class zoom.response.ICOResponse(content, max_age=86400)

Bases: zoom.response.Response

ICO image response

>>> response = ICOResponse(b'myicon')
>>> expected = (
...     b'Status: 200 OK\n'
...     b'Content-type: image/x-icon\n'
...     b'Cache-Control: max-age=86400\n'
...     b'ETag: 78d2485ff\n'
...     b'Content-length: 6\n\n'
...     b'myicon'
... )
>>> response.render() == expected
True
render_doc()

Renders the payload

class zoom.response.JPGResponse(content)

Bases: zoom.response.Response

JPG image response

>>> response = JPGResponse(b'myimage')
>>> expected = (
...     b'Status: 200 OK\n'
...     b'Content-type: image/jpeg\n'
...     b'Content-length: 7\n\n'
...     b'myimage'
... )
>>> response.render() == expected
True
class zoom.response.JSONResponse(content, indent=4, sort_keys=True, ensure_ascii=False, **kwargs)

Bases: zoom.response.TextResponse

JSON response

>>> response = JavascriptResponse(b'myjson')
>>> expected = (
...     b'Status: 200 OK\n'
...     b'Content-type: application/javascript\n'
...     b'Cache-Control: max-age=86400\n'
...     b'ETag: f97258d47\n'
...     b'Content-length: 6\n\n'
...     b'myjson'
... )
>>> response.render() == expected
True
class zoom.response.JavascriptResponse(content, max_age=86400)

Bases: zoom.response.Response

Javascript response

>>> response = JavascriptResponse(b'myjs')
>>> expected = (
...     b'Status: 200 OK\n'
...     b'Content-type: application/javascript\n'
...     b'Cache-Control: max-age=86400\n'
...     b'ETag: 8be4a11f3\n'
...     b'Content-length: 4\n\n'
...     b'myjs'
... )
>>> response.render() == expected
True
class zoom.response.PDFResponse(filename, content=None)

Bases: zoom.response.FileResponse

PDF file download response

>>> response = PDFResponse('file.pdf', content=b'mydata')
>>> expected = (
...     b'Status: 200 OK\n'
...     b'Content-type: application/pdf\n'
...     b'Cache-Control: no-cache\n'
...     b'Content-length: 6\n\n'
...     b'mydata'
... )
>>> response.render() == expected
True
class zoom.response.PNGResponse(content, max_age=86400)

Bases: zoom.response.Response

PNG image response

>>> response = PNGResponse(b'myimage')
>>> expected = (
...     b'Status: 200 OK\n'
...     b'Content-type: image/png\n'
...     b'Cache-Control: max-age=86400\n'
...     b'ETag: b1a9acaf2\n'
...     b'Content-length: 7\n\n'
...     b'myimage'
... )
>>> response.render() == expected
True
class zoom.response.RedirectResponse(url)

Bases: zoom.response.TextResponse

Redirect response

>>> response = RedirectResponse('/')
>>> response.as_wsgi()
('302 Found', [('Location', '/'), ('Content-length', '0')], b'')
class zoom.response.Response(content=b'', status='200 OK', headers=None)

Bases: object

web response

>>> response = Response(b'this is it')
>>> response.render()
b'Status: 200 OK\nContent-length: 10\n\nthis is it'
>>> response.as_wsgi()
('200 OK', [('Content-length', '10')], b'this is it')
as_wsgi()

Render the entire response

render()

Renders the entire response

render_doc()

Renders the payload

class zoom.response.SiteNotFoundResponse(request)

Bases: zoom.response.HTMLResponse

Site 404 Not Found response

>>> request = zoom.utils.Bunch(
...     protocol='http',
...     host='localhost',
...     path='/',
...     ip_address='127.0.0.1',
...     module='index',
...     request_id=1234,
... )
>>> response = SiteNotFoundResponse(request)
>>> 'ZOOM' in str(response.render())
True
class zoom.response.TTFResponse(content, max_age=86400)

Bases: zoom.response.Response

True Type Font response

>>> response = TTFResponse(b'myfont')
>>> expected = (
...     b'Status: 200 OK\n'
...     b'Content-type: application/font-sfnt\n'
...     b'Cache-Control: max-age=86400\n'
...     b'ETag: 794c8f9c8\n'
...     b'Content-length: 6\n\n'
...     b'myfont'
... )
>>> response.render() == expected
True
class zoom.response.TextResponse(content='', status='200 OK')

Bases: zoom.response.Response

Plan text response

>>> response = TextResponse('mytext')
>>> expected = (
...     b'Status: 200 OK\n'
...     b'Content-type: text\n'
...     b'Cache-Control: no-cache\n'
...     b'Content-length: 6\n\n'
...     b'mytext'
... )
>>> response.render() == expected
True
render_doc()

Renders the payload

class zoom.response.WOFF2Response(content, max_age=86400)

Bases: zoom.response.Response

Web Open Font 2 Format response

>>> response = WOFF2Response(b'myfont')
>>> expected = (
...     b'Status: 200 OK\n'
...     b'Content-type: font/woff2\n'
...     b'Cache-Control: max-age=86400\n'
...     b'ETag: 794c8f9c8\n'
...     b'Content-length: 6\n\n'
...     b'myfont'
... )
>>> response.render() == expected
True
class zoom.response.WOFFResponse(content, max_age=86400)

Bases: zoom.response.Response

Web Open Font Format response

>>> response = WOFFResponse(b'myfont')
>>> expected = (
...     b'Status: 200 OK\n'
...     b'Content-type: application/font-woff\n'
...     b'Cache-Control: max-age=86400\n'
...     b'ETag: 794c8f9c8\n'
...     b'Content-length: 6\n\n'
...     b'myfont'
... )
>>> response.render() == expected
True
class zoom.response.XMLResponse(content='')

Bases: zoom.response.Response

XML response

>>> response = XMLResponse('myxml')
>>> expected = (
...     b'Status: 200 OK\n'
...     b'Content-type: text/xml\n'
...     b'Cache-Control: no-cache\n'
...     b'Content-length: 26\n\n'
...     b'<?xml version="1.0"?>myxml'
... )
>>> response.render() == expected
True
render_doc()

Renders the payload