Skip to content

Commit

Permalink
refactor: dedup headers via Enum.uniq_by/2
Browse files Browse the repository at this point in the history
  • Loading branch information
fishtreesugar committed Aug 30, 2023
1 parent 4eb68b5 commit dc95ae4
Showing 1 changed file with 3 additions and 8 deletions.
11 changes: 3 additions & 8 deletions lib/http_client_builder.ex
Original file line number Diff line number Diff line change
Expand Up @@ -96,8 +96,9 @@ defmodule HttpClientBuilder do

headers =
if unquote(client_opts)[:runtime_headers_getter] do
(unquote(client_opts)[:runtime_headers_getter].() ++ compile_time_headers)
|> dedup_headers()
compile_time_headers
|> Kernel.++(unquote(client_opts)[:runtime_headers_getter].())
|> Enum.uniq_by(&elem(&1, 0))
else
compile_time_headers
end
Expand All @@ -115,12 +116,6 @@ defmodule HttpClientBuilder do
base_url <> url_or_path <> query
end

defp dedup_headers(headers) do
headers
|> Enum.reduce(%{}, fn {k, v}, acc -> Map.put(acc, k, v) end)
|> Map.to_list()
end

defoverridable get: 2, post: 2, put: 2, delete: 2, patch: 2, do_request: 3, build_url: 2
end
end
Expand Down

0 comments on commit dc95ae4

Please sign in to comment.