API Reference

Jinja2 API

static(<filename>) is a function which resolves static contents path. A Chalice route should be set which matches ContentManager.set_static_handler_prefix()’s value.

Python API

chalice_http_toolkit.cloudfront module

class chalice_http_toolkit.cloudfront.CloudFront(content_manager: chalice_http_toolkit.content.ContentManager, app: Union[chalice.Chalice, chalice.Blueprint], default_cache_control='no-cache')[source]

Bases: object

Supports caching via CloudFront for the following usecases: - Jinja2 Templates by exploring dependancy trees of templates and their arguments. - Static content by extracting modified dates from static content.

Parameters
  • content_manager – challice_http_toolkit.ContentManager

  • app – Chalice or Blueprint

  • default_cache_control – Cache control to use if none specified

Returns

CloudFront instance

asset(get_asset: Callable[], bytes], get_etag: Callable[], str], status_code: int = 200, headers: Optional[dict] = None, cache_control: Optional[str] = None)chalice.Response[source]

Calls get_etag() to generate a tag for the asset, and if it matches the If-None-Match value, we return a 304. Otherwise get_asset() is called and returned.

Parameters
Returns

Chalice Response object

static(filename: str, status_code: int = 200, headers: Optional[dict] = None, cache_control: Optional[str] = None)chalice.Response[source]

If CloudFront supplies If-Modified-Since header in request, then we can check against the modified date of the file to inform CloudFront if its cache needs to be updated or not.

Parameters
Returns

Chalice Response object

template(filename: str, status_code: int = 200, headers: Optional[dict] = None, cache_control: Optional[str] = None, **kwargs)chalice.Response[source]

If CloudFront supplies If-None-Match header in request, then we can check against a hash of the template, its dependancies, and its arguments to inform CloudFront if its cache needs to be updated or not.

Parameters
Returns

Chalice Response object

chalice_http_toolkit.content module

class chalice_http_toolkit.content.ContentManager(templates_dir: str, static_dir: str, app: Union[chalice.Chalice, chalice.Blueprint], magic: Optional[magic.Magic] = None)[source]

Bases: object

ACCEPTS2PIL = {'image/jpeg': 'JPEG', 'image/jpg': 'JPEG', 'image/png': 'PNG', 'image/webp': 'WEBP'}

Creates a ContentManager instance which allows rendering templates, returning static content and dynamic assets plus more. Generally after a ContentManager is called, set_static_handler_prefix() should be called directlty after to setup your static() handler in Jinja templates.

Parameters
  • templates_dir – Base path for templates

  • static_dir – Base path for static content

  • app – Chalice app

  • magic – Optional python-magic instance, often required given Lambda hosts dont have libmagic installed.

Returns

ContentManager instance

asset(body: bytes, status_code: int = 200, headers: Optional[dict] = None)chalice_http_toolkit.response_with_binary.ResponseWithBinary[source]

Useful for returning dynamic static content such as images loaded from a database.

Parameters
  • body – Bytes representing content to return

  • status_code – Status code to return content with

  • headers – Additional headers to return

Returns

Chalice Response object

convert_by_accepts(img: bytes, accepts: str, default='WEBP')bytes[source]

Converts an images type based on the accepts format (ie image/jpeg) using Pillow. If default=None, will throw an exception if no conversion found.

Parameters
  • img – Bytes of image

  • accepts – Mimetype format, current supports: image/jpeg, image/jpg, image/webp, image/png, image/gif

  • default – Default format if cant convert, prevents an exception being thrown if cant convert.

Returns

Converted image

Raises

Exception if cant find matching type to convert to

html(body: str, status_code: int = 200, headers: Optional[dict] = None)chalice.Response[source]

Useful for returning html content that has been rendered already using render_template().

Parameters
  • body – String containing rendered template

  • status_code – Status code to return content with

  • headers – Additional headers to return

Returns

Chalice Response object

json(body: str, status_code: int = 200, headers: Optional[dict] = None)chalice.Response[source]

Useful for returning json content.

Parameters
  • body – String containing json

  • status_code – Status code to return content with

  • headers – Additional headers to return

Returns

Chalice Response object

redirect(url: str, status_code: int = 301)chalice.Response[source]

By default generates a 301 redirect Response.

Parameters

url – URL to redirect to

Returns

Chalice Response object

render_template(filename: str, **kwargs)str[source]

Renders a template using the existing Jinja2 environment.

Parameters
  • filename – Path to template

  • kwargs – Additional kwargs to be passed into template render

Returns

Rendered template as a string

set_static_handler_prefix(path: str)[source]

Defines a function static() to allow dynamic resolution of static content in templates. The path argument should correlate with a Chalice endpoint to fetch the static content.

Parameters

path – Path to match chalice endpoint in which static() calls will match in templates.

Returns

None

static(filename: str, status_code: int = 200, headers: Optional[dict] = None)chalice_http_toolkit.response_with_binary.ResponseWithBinary[source]

Useful for returning static content in the configured static directory.

Parameters
  • filename – Path relative from static_dir to content

  • status_code – Status code to return content with

  • headers – Additional headers to return

Returns

Chalice Response object

xml(body: str, status_code: int = 200, headers: Optional[dict] = None)chalice.Response[source]

Useful for returning xml content that has been rendered already using render_template().

Parameters
  • body – String containing rendered template

  • status_code – Status code to return content with

  • headers – Additional headers to return

Returns

Chalice Response object

chalice_http_toolkit.multipart module

chalice_http_toolkit.multipart.ex_multipart(app: Union[chalice.Chalice, chalice.Blueprint])dict[source]

Decodes content_type=’multipart/form-data’ and returns a dictionary of parts in the format: {

“<name>”: {“data”: <content>, “filename”: <filename>}

} :returns: Dictionary of decoded content

chalice_http_toolkit.response_with_binary module

class chalice_http_toolkit.response_with_binary.ResponseWithBinary(*args: Any, **kwargs: Any)[source]

Bases: chalice.

Wrapper class to override isBase64Encoded behaviour in the Reponse class. Setting isBase64Encoded is usually done to encode Binary content for API Gateway so that it knows to decode it.

isBase64Encoded = False
isLocal = True
to_dict(binary_types=None)dict[source]

chalice_http_toolkit.tasking module