Skip to content

Commit de2e238

Browse files
committed
add multiple recipients functions
1 parent 459e92c commit de2e238

File tree

2 files changed

+49
-0
lines changed

2 files changed

+49
-0
lines changed

lib/sendgrid/marketing_campaigns/contacts/lists.ex

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,22 @@ defmodule SendGrid.Contacts.Lists do
6262
end
6363
end
6464

65+
@doc """
66+
Adds a recipient to an email list.
67+
68+
:ok = add_recipient(123, "recipient_id")
69+
70+
"""
71+
@spec add_multiple_recipients(integer, [String.t()]) :: :ok | :error
72+
def add_multiple_recipients(list_id, recipient_ids) when is_list(recipient_ids) do
73+
url = @base_api_url <> "/#{list_id}/recipients"
74+
75+
case SendGrid.post(url, recipient_ids) do
76+
{:ok, %{status_code: 201}} -> :ok
77+
_ -> :error
78+
end
79+
end
80+
6581
@doc """
6682
Deletes a recipient from an email list.
6783

lib/sendgrid/marketing_campaigns/contacts/recipients.ex

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,39 @@ defmodule SendGrid.Contacts.Recipients do
2424
|> handle_recipient_result
2525
end
2626

27+
@doc """
28+
Adds or updates multiple recipients in contacts list.
29+
Recipients param must be in format required by Sendgrid:
30+
[
31+
%{
32+
"email" => "[email protected]",
33+
"name" => "John Doe",
34+
etc...
35+
}
36+
]
37+
"""
38+
@spec add_multiple([]) :: { :ok, [] } | { :ok, String.t } | { :error, list(String.t) }
39+
def add_multiple(recipients) when is_list(recipients) do
40+
SendGrid.patch(@base_api_url, recipients)
41+
|> handle_recipient_result
42+
end
43+
44+
@doc """
45+
Allows you to perform a search on all of your Marketing Campaigns recipients
46+
47+
{:ok, recipients} = search(%{"first_name" => "test"})
48+
"""
49+
@spec search(map) :: { :ok, list(map) } | { :error, list(String.t) }
50+
def search(opts) do
51+
query = URI.encode_query(opts)
52+
SendGrid.get("#{@base_api_url}/search?#{query}")
53+
|> handle_search_result
54+
end
55+
56+
# Handles the result when there are multiple persisted recipients.
57+
defp handle_recipient_result({:ok, %{body: %{"persisted_recipients" => recipients}}}) when is_list(recipients) and length(recipients) > 1 do
58+
{ :ok, recipients }
59+
end
2760
# Handles the result when errors are present.
2861
defp handle_recipient_result({:ok, %{body: body = %{"error_count" => count}}}) when count > 0 do
2962
errors =

0 commit comments

Comments
 (0)