Aino.Token (aino v0.1.0)
The token is what flows through the entire web request
This module contains helper functions for dealing with the token, setting common fields for responses or looking up request fields.
At the end of a middleware chain, the token must contain three keys:
:response_status
:response_headers
:response_body
These keys are used for generating the request's response.
Link to this section Summary
Functions
Start a token from an :elli
request
Reduce a token over a set of middleware.
Get a response header from the token
Set the response body
Append a response header to the token
Set all response headers on the token
Set a response status on the token
Link to this section Functions
from_request(request)
Start a token from an :elli
request
The token gains the keys [:request]
iex> request = %Aino.Request{}
iex> token = Token.from_request(request)
iex> token.request == request
true
reduce(token, middleware)
Reduce a token over a set of middleware.
Takes a list of middleware, that may be either another list of middleware or a function that has an arity of 1.
For example
middleware = [
Aino.Middleware.common(),
&Aino.Middleware.Routes.routes(&1, routes),
&Aino.Middleware.Routes.match_route/1,
&Aino.Middleware.params/1,
&Aino.Middleware.Routes.handle_route/1,
]
reduce(token, middleware)
request_header(token, request_header)
Get a response header from the token
This must be used with Aino.Middleware.headers/1
since that middleware sets
up the token to include a :headers
key that is downcased.
The request header that is searched for is lower cased and compared against request headers, filtering down to matching headers.
iex> token = %{headers: [{"content-type", "text/html"}, {"location", "/"}]}
iex> Token.request_header(token, "Content-Type")
["text/html"]
response_body(token, body)
Set the response body
When setting a response body, you should also set a Content-Type
header.
This way the client can know what type of data it received.
The token gains or modifies the keys [:response_body]
iex> token = %{}
iex> Token.response_body(token, "html")
%{response_body: "html"}
response_header(token, key, value)
Append a response header to the token
Response headers default to an empty list if this is the first header set
The token gains or modifies the keys [:response_headers]
iex> token = %{}
iex> Token.response_header(token, "Content-Type", "application/json")
%{response_headers: [{"Content-Type", "application/json"}]}
iex> token = %{response_headers: [{"Content-Type", "text/html"}]}
iex> Token.response_header(token, "Location", "/")
%{response_headers: [{"Content-Type", "text/html"}, {"Location", "/"}]}
response_headers(token, headers)
Set all response headers on the token
If response headers are present, they are cleared. This directly sets the
:response_headers
key on the token.
The token gains or modifies the keys [:response_headers]
iex> token = %{}
iex> Token.response_headers(token, [{"Content-Type", "application/json"}])
%{response_headers: [{"Content-Type", "application/json"}]}
iex> token = %{response_headers: [{"Content-Type", "text/html"}]}
iex> Token.response_headers(token, [{"Location", "/"}])
%{response_headers: [{"Location", "/"}]}
response_status(token, status)
Set a response status on the token
The token gains the keys [:response_status]
iex> token = %{}
iex> Token.response_status(token, 200)
%{response_status: 200}