Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
34 changes: 34 additions & 0 deletions lib/mailjex/api/contact_list.ex
Original file line number Diff line number Diff line change
Expand Up @@ -6,4 +6,38 @@ defmodule Mailjex.Api.ContactList do
request(:post, "/REST/contactslist/#{id}/managecontact", body)
|> decode_json
end

def manage_many_contacts_in_list(list_id, body) do
request(:post, "/REST/contactslist/#{list_id}/managemanycontacts", body)
|> decode_json
end

def create_contact_list(name, is_deleted = false) do
request(:post, "/REST/contactslist", %{
Name: name,
IsDeleted: is_deleted
})
|> decode_json
end

def get_all_contact_lists(options \\ []) do
request(:get, "/REST/contactslist", options)
|> decode_json
end

def get_contact_list(list_id, options \\ []) do
request(:get, "/REST/contactslist/#{list_id}", options)
|> decode_json
end

def update_contact_list(list_id, body) do
request(:put, "/REST/contactslist/#{list_id}", body)
|> decode_json
end

def delete_contact_list(list_id, options \\ []) do
request(:delete, "/REST/contactslist/#{list_id}", options)
|> decode_json
end

end
114 changes: 113 additions & 1 deletion lib/mailjex/contact_list.ex
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ defmodule Mailjex.ContactList do

@doc """
Allows you to manage the presence and subscription
status of a contact to a specific contact list.
status of a contact to a specific contact list.

## Examples

Expand All @@ -37,11 +37,123 @@ defmodule Mailjex.ContactList do
GenServer.call(__MODULE__, {:manage_contact, id, body})
end


@doc """
Allows you to manage the presence and subscription
status of a contact to a specific contact list.

## Examples

iex> body = %{
...> "Action": "addnoforce",
...> "Contacts": [
...> "Email": "[email protected]",
...> "Name": "MrSmith",
...> "IsExcludedFromCapaign": false,
...> "Properties": %{
...> "property1": "value",
...> "propertyN": "valueN"
...> }
...> ]
...>}
iex> Mailjex.ContactList.manage_many_contacts_in_list(12345, body)
"""
def manage_many_contacts_in_list(list_id, body) do
GenServer.call(__MODULE__, {:manage_many_contacts_in_list, list_id, body})
end


@doc """
Allows you to create a contact list. When is_deleted is true, the contact list will be marked as Deleted. Deleted lists can later be reinstated by updating this value to False.

## Examples

iex> name = "testing"
iex> is_deleted = false
iex> Mailjex.ContactList.create_contact_list(name, is_deleted)
"""
def create_contact_list(name, is_deleted = false) do
GenServer.call(__MODULE__, {:create_contact_list, name, is_deleted})
end

@doc """
Retrieve details for all contact lists - name, subscriber count, creation timestamp, deletion status. Options available here: https://dev.mailjet.com/email/reference/contacts/contact-list/#v3_get_contactslist

## Examples

iex> Mailjex.ContactList.get_all_contact_lists()
"""
def get_all_contact_lists(options \\ []) do
GenServer.call(__MODULE__, {:get_all_contact_lists, options})
end

@doc """
Retrieve details for a specific contact list - name, subscriber count, creation timestamp, deletion status.

## Examples

iex> Mailjex.ContactList.get_contact_list(12345)
"""
def get_contact_list(list_id, options \\ []) do
GenServer.call(__MODULE__, {:get_contact_list, list_id, options})
end

@doc """
Update a specific contact list by changing its name and / or deletion status. Using PUT you can delete lists, as well as reinstate previously deleted ones.

## Examples

iex> body = %{
...> "IsDeleted": false,
...> "Name": "testing"
...>}
iex> Mailjex.ContactList.update_contact_list(12345, body)
"""
def update_contact_list(list_id, body) do
GenServer.call(__MODULE__, {:update_contact_list, list_id, body})
end

@doc """
Delete a contact list. The ContactsList object will continue to exist with Deleted status for 30 days, and can be reinstated by changing the value of IsDeleted to false via PUT /contactslist/{list_ID}.

## Examples

iex> Mailjex.ContactList.manage_contact(12345)
"""
def delete_contact_list(list_id, options \\ []) do
GenServer.call(__MODULE__, {:delete_contact_list, list_id, options})
end

##########################
# GenServer Callbacks
##########################

def handle_call({:manage_contact, id, body}, _from, state) do
{:reply, ContactList.manage_contact(id, body), state}
end

def handle_call({:manage_contacts_in_list, list_id, body}, _from, state) do
{:reply, ContactList.manage_contacts_in_list(list_id, body), state}
end

def handle_call({:create_contact_list, name, is_deleted}, _from, state) do
{:reply, ContactList.create_contact_list(name, is_deleted), state}
end

def handle_call({:get_all_contact_lists, options}, _from, state) do
{:reply, ContactList.get_all_contact_lists(options), state}
end

def handle_call({:get_contact_list, list_id, options}, _from, state) do
{:reply, ContactList.get_contact_list(list_id, options), state}
end

def handle_call({:delete_contact_list, list_id, options}, _from, state) do
{:reply, ContactList.delete_contact_list(list_id, options), state}
end

def handle_call({:update_contact_list, list_id, body}, _from, state) do
{:reply, ContactList.update_contact_list(list_id, body), state}
end

end
23 changes: 23 additions & 0 deletions lib/mailjex/utils/comms.ex
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,29 @@ defmodule Mailjex.Utils.Comms do
|> HTTPotion.get(headers())
end

def request(:get, path, options) do
options = options ++ headers()
path
|> api_url
|> HTTPotion.get(options)
end

def request(:put, path, body) do
options = [body: Poison.encode!(body)]
|> headers()

path
|> api_url
|> HTTPotion.put(options)
end

def request(:delete, path, options) do
options = options ++ headers()
path
|> api_url
|> HTTPotion.delete(options)
end

def request(method, path, body, public_key \\ nil, private_key \\ nil)
def request(:post, path, body, public_key, private_key) do
hdrs = [body: Poison.encode!(body)]
Expand Down