Warning: This document is for an old version of molten. The latest version is v0.7.3.

Components

Route Components

Route parameters are detected based on each route’s template. Each parameter is coerced to the appropriate type before the handler is called. If a value can’t be coerced, then a 400 Bad Request response is returned:

def hello(name: str, age: int) -> None:
    ...

Route("/hello/{name}/{age}", hello)

Request Components

Request gives you access to the current request:

def index(request: Request) -> None:
    print(request.path)

QueryString gives you access to the full query string:

def index(query: QueryString) -> None:
    print(query)

QueryParams gives you access to all the query parameters in the request:

def index(params: QueryParams) -> None:
    print(params.get("some_param"))
    print(params.get_all("some_param"))

QueryParam gives you access to an individual query parameter in the current request:

def index(some_param: QueryParam) -> None:
    print(some_param)

This returns a 400 Bad Request response if some_param is missing from the request. You may mark query params as optional to avoid that:

def index(some_param: Optional[QueryParam]) -> None:
    print(some_param)

Headers gives you access to all the headers in the current request:

def index(headers: Headers) -> None:
    print(headers.get("some-header"))

Header gives you access to an individual header from the current request:

def index(content_type: Header) -> None:
    print(content_type)

This returns a 400 Bad Request response if the content-type header is missing from the request. You may mark query params as optional to avoid that:

def index(content_type: Optional[Header]) -> None:
    print(content_type)

Cookies gives you access to all the cookies in the current request:

def index(cookies: Cookies) -> None:
    print(headers.get("some-header"))

RequestInput gives you access to the request body as a binary file-like object:

def index(content_length: Header, body_file: RequestInput) -> None:
    print(body_file.read(int(content_length)))

RequestBody gives you access to the request body as bytestring:

def index(body: RequestBody) -> None:
    print(body)

RequestData gives you access to the parsed request body:

def index(data: RequestData) -> None:
    print(data)

Special Components

Sometimes it may be useful for functions to gain access to the current dependency resolver instance. Any parameter whose annotation is DependencyResolver will have the current resolver injected into it:

def index(resolver: DependencyResolver) -> None:
    ...

Certain components may require access to the current parameter being resolved. In that case, they can annotate a dependency with inspect.Parameter:

def resolve(self, parameter: Parameter) -> Something:
    return Something(parameter.name)

This is how the route parameter component is implemented under the hood.