Skip to content

Instantly share code, notes, and snippets.

@timbuchwaldt
Created September 11, 2016 12:43
Show Gist options
  • Save timbuchwaldt/5d089b827bd1c00411465b1169e4e406 to your computer and use it in GitHub Desktop.
Save timbuchwaldt/5d089b827bd1c00411465b1169e4e406 to your computer and use it in GitHub Desktop.
defmodule MyAPI do
use Tesla
adapter Tesla.Adapter.Hackney
plug Tesla.Middleware.BaseUrl, "http://127.0.0.1:4000"
def foo(client, login) do
get(client, "/foo")
end
def client(%{resp_headers: resp_headers}) do
{"x-request-id", id} = resp_headers
|> List.keyfind("x-request-id", 0)
Tesla.build_client [
{PhoenixRequestIDMiddleware, id}
]
end
end
defmodule PhoenixRequestIDMiddleware do
def call(env, next, opts) do
env
|> add_request_id(opts)
|> IO.inspect
|> Tesla.run(next)
end
def add_request_id(env, id) do
Map.update!(env, :headers, &Map.merge(&1, %{"x-request-id" => id}))
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment