aboutsummaryrefslogtreecommitdiff
path: root/lib/httpclient.ex
blob: 2b1594b646f2ed3f71808dbb8bc76748679168da (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
27
28
29
30
31
defmodule Mailchimp.HTTPClient do

  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

  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
  end

  def process_response_body(body) do
    body
    |> Poison.decode!
    |> Enum.map(fn({k, v}) -> {String.to_atom(k), v} end)
  end

end