diff options
Diffstat (limited to 'lib/httpclient.ex')
-rw-r--r-- | lib/httpclient.ex | 39 |
1 files changed, 16 insertions, 23 deletions
diff --git a/lib/httpclient.ex b/lib/httpclient.ex index bd58b92..433b57c 100644 --- a/lib/httpclient.ex +++ b/lib/httpclient.ex @@ -1,33 +1,26 @@ defmodule Mailchimp.HTTPClient do + use HTTPoison.Base - def get(url, header) do - case HTTPoison.get(url, header) do - {:ok, %HTTPoison.Response{status_code: 200, body: body}} -> - process_response_body body - {:ok, %HTTPoison.Response{status_code: 404}} -> - "Not found :(" - {:error, %HTTPoison.Error{reason: reason}} -> - reason - end - end + alias Mailchimp.Config - def post(url, body, header) do - case HTTPoison.post(url, body, header) do - {:ok, %HTTPoison.Response{status_code: 200, body: body}} -> - process_response_body body - {:ok, %HTTPoison.Response{status_code: 400, body: body}} -> - process_response_body body - {:ok, %HTTPoison.Response{status_code: 404}} -> - "Not found :(" - {:error, %HTTPoison.Error{reason: reason}} -> - reason + 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 - body - |> Poison.decode! - |> Enum.map(fn({k, v}) -> {String.to_atom(k), v} end) + 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 |