Chalice

class Chalice(app_name)

This class represents a chalice application. It provides:

  • The ability to register routes using the route() method.
  • Within a few function, the ability to introspect the current request using the current_request attribute which is an instance of the Request class.
current_request

An object of type Request. This value is only set when a view function is being called. This attribute can be used to introspect the current HTTP request.

debug

A boolean value that enables debugging. By default, this value is False. If debugging is true, then internal errors are returned back to the client. Additionally, debug log messages generated by the framework will show up in the cloudwatch logs. Example usage:

from chalice import Chalice

app = Chalice(app_name="appname")
app.debug = True
route(path, *[, methods[, name], authorization_type, authorizer_id, api_key_required])

Register a view function for a particular URI path. This method is intended to be used as a decorator for a view function. For example:

from chalice import Chalice

app = Chalice(app_name="appname")

@app.route('/resource/{value}', methods=['PUT'])
def viewfunction(value):
    pass
Parameters
  • path (str) -- The path to associate with the view function. The path should only contain [a-zA-Z0-9._-] chars and curly braces for parts of the URL you would like to capture. The path should not end in a trailing slash, otherwise a validation error will be raised during deployment.
  • methods (list) -- Optional parameter that indicates which HTTP methods this view function should accept. By default, only GET requests are supported. If you only wanted to support POST requests, you would specify methods=['POST']. If you support multiple HTTP methods in a single view function (methods=['GET', 'POST']), you can check the app.current_request.method attribute to see which HTTP method was used when making the request.
  • name (str) -- Optional parameter to specify the name of the view function. You generally do not need to set this value. The name of the view function is used as the default value for the view name.
  • authorization_type (str) -- Optional parameter to specify the type of authorization used for the view.
  • authorizer_id (str) -- Optional parameter to specify the identifier of an Authorizer to use on this view, if the authorization_type is CUSTOM.
  • api_key_required (boolean) -- Optional parameter to specify whether the method required a valid ApiKey

Request

class Request

A class that represents the current request. This is mapped to the app.current_request object.

query_params

A dict of the query params for the request.

headers

A dict of the request headers.

uri_params

A dict of the captured URI params.

method

The HTTP method as a string.

json_body

The parsed JSON body (json.loads(raw_body)).

raw_body

The raw HTTP body as bytes. This is useful if you need to calculate a checksum of the HTTP body.

context

A dict of additional context information.

stage_vars

A dict of configuration for the API Gateway stage.

Response

class Response(body, headers=None, status_code=200)

A class that represents the response for the view function. You can optionally return an instance of this class from a view function if you want complete control over the returned HTTP response.

New in version 0.6.0.

body

The HTTP response body to send back. This value must be a string.

headers

An optional dictionary of HTTP headers to send back. This is a dictionary of header name to header value, e.g {'Content-Type': 'text/plain'}

status_code

The integer HTTP status code to send back in the HTTP response.