tesla v1.2.0 Tesla.Middleware behaviour View Source
The middleware specification
Middleware is an extension of basic Tesla functionality. It is a module that must
export call/3 function.
Middleware options
Options can be passed to middleware in second param of Tesla.Builder.plug/2 macro:
plug Tesla.Middleware.BaseUrl, "https://example.com"
or inside tuple in case of dynamic middleware (Tesla.client/1)
Tesla.client([{Tesla.Middleware.BaseUrl, "https://example.com"}])
Writing custom middleware
Writing custom middleware is as simple as creating a module with call/3 function that:
- (optionally) read and/or writes request data
- calls
Tesla.run/2 - (optionally) read and/or writes response data
call/3 params:
- env -
Tesla.Envstruct that stores request/response data - stack - middlewares that should be called after current one
- options - middleware options provided by user
Example
defmodule MyProject.InspectHeadersMiddleware do
@behaviour Tesla.Middleware
@impl true
def call(env, next, options) do
env
|> inspect_headers(options)
|> Tesla.run(next)
|> inspect_headers(options)
end
defp inspect_headers(env, options) do
IO.inspect(env.headers, options)
end
end
Link to this section Summary
Link to this section Callbacks
Link to this callback
call(env, next, options)
View Source
call(env :: Tesla.Env.t(), next :: Tesla.Env.stack(), options :: any()) :: Tesla.Env.result()