aboutsummaryrefslogtreecommitdiff
path: root/lib/httpclient.ex
blob: 433b57ce2b8f79d8e35f82c587e85912c234f23d (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
defmodule Mailchimp.HTTPClient do
  use HTTPoison.Base

  alias Mailchimp.Config

  def process_url(url) do
    root_endpoint = Config.root_endpoint
    if String.starts_with?(url, root_endpoint) do
      url
    else
      root_endpoint <> url
    end
  end

  def process_response_body(body) do
    Poison.decode!(body, keys: :atoms)
  end

  def process_request_headers(headers) do
    encoded_api = Base.encode64(":#{Config.api_key}")
    headers
    |> Enum.into(%{})
    |> Map.put("Authorization", "Basic #{encoded_api}")
    |> Enum.into([])
  end
end