aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorRawane Zossou <dev@raw1z.fr>2015-09-19 00:42:16 +0200
committerRawane Zossou <dev@raw1z.fr>2015-09-19 00:42:16 +0200
commit39f3c3ab41d3c2c342a8ee70a3d734bbcd5b273e (patch)
tree1ecb15a352ae6d2d785f0559738431da620c23bd
parent271615ed72e3229581c625267f788a260bed88d9 (diff)
downloadmailchimp-39f3c3ab41d3c2c342a8ee70a3d734bbcd5b273e.tar.gz
mailchimp-39f3c3ab41d3c2c342a8ee70a3d734bbcd5b273e.tar.xz
refactor the http client
-rw-r--r--lib/httpclient.ex35
1 files changed, 13 insertions, 22 deletions
diff --git a/lib/httpclient.ex b/lib/httpclient.ex
index 2b1594b..e34312f 100644
--- a/lib/httpclient.ex
+++ b/lib/httpclient.ex
@@ -1,31 +1,22 @@
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: 404}} ->
- "Not found :("
- {:error, %HTTPoison.Error{reason: reason}} ->
- reason
- end
+ def process_url(url) do
+ Config.root_endpoint <> url
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