exrequester icon indicating copy to clipboard operation
exrequester copied to clipboard

Create a way to define module level headers

Open nsomar opened this issue 8 years ago • 2 comments

At the moment, if you have the same headers passed in all the functions of the same module, we need to duplicate these headers with each method.

There should be a way to create headers that are reused for each function in the same module.

nsomar avatar Mar 12 '16 11:03 nsomar

One way of doing it at the moment is like so

defmodule HTTPBinSample do
  use EXRequester

  @shared_headers [
    key1: :value1
  ]

  @get "/status"
  @headers [Authorization: :auth] ++ @shared_headers
  defreq get_status do
    response.headers |> IO.inspect
  end

  @get "/other"
  @headers [Authorization: :auth] ++ @shared_headers
  defreq get_other do
    response.headers |> IO.inspect
  end
end

It leverage the fact that modules attributes can still be treated as elixir instructions. This might be one way of doing it, however there maybe some consideration on why this is not a good idea.

nsomar avatar Mar 12 '16 11:03 nsomar

the example you provided seems like a good workaround. Perhaps having @shared_headers and @headers merged could be part of the defreq macro and have it handled transparently but still have the flexibility of custom headers for a request when needed?

peck avatar Mar 13 '16 17:03 peck