aboutsummaryrefslogtreecommitdiff
path: root/lib/account.ex
diff options
context:
space:
mode:
Diffstat (limited to 'lib/account.ex')
-rw-r--r--lib/account.ex41
1 files changed, 36 insertions, 5 deletions
diff --git a/lib/account.ex b/lib/account.ex
index 586d274..7e24858 100644
--- a/lib/account.ex
+++ b/lib/account.ex
@@ -1,10 +1,41 @@
defmodule Mailchimp.Account do
- import Mailchimp.HTTPClient
+ alias HTTPoison.Response
+ alias Mailchimp.HTTPClient
+ alias Mailchimp.Link
+ alias Mailchimp.List
- def get_details(config) do
- map_header = %{"Authorization" => "apikey #{config.apikey}"}
- url = config.apiroot
- get(url, map_header)
+ defstruct account_id: nil, account_name: nil, contact: nil, last_login: nil, total_subscribers: 0, links: []
+
+ def new(attributes) do
+ %__MODULE__{
+ account_id: attributes[:account_id],
+ account_name: attributes[:account_name],
+ contact: attributes[:contact],
+ last_login: attributes[:last_login],
+ total_subscribers: attributes[:total_subscribers],
+ links: Link.get_links_from_attributes(attributes)
+ }
+ end
+
+ def get do
+ {:ok, response} = HTTPClient.get("/")
+ case response do
+ %Response{status_code: 200, body: body} ->
+ {:ok, __MODULE__.new(body)}
+
+ %Response{status_code: _, body: body} ->
+ {:error, body}
+ end
end
+ def lists(%__MODULE__{links: %{"lists" => %Link{href: href}}}) do
+ {:ok, response} = HTTPClient.get(href)
+ case response do
+ %Response{status_code: 200, body: body} ->
+ {:ok, Enum.map(body.lists, &List.new(&1))}
+
+ %Response{status_code: _, body: body} ->
+ {:error, body}
+ end
+ end
end