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
8 changes: 4 additions & 4 deletions lib/sendgrid.ex
Original file line number Diff line number Diff line change
Expand Up @@ -62,9 +62,9 @@ defmodule SendGrid do
* `:api_key` - API key to use with the request.
* `:query` - Keyword list of query params to use with the request.
"""
@spec post(path :: String.t(), body :: map(), options :: options()) ::
@spec post(path :: String.t(), body :: Tesla.Env.body(), options :: options()) ::
{:ok, Response.t()} | {:error, any()}
def post(path, body, opts \\ []) when is_map(body) and is_list(opts) do
def post(path, body, opts \\ []) when is_list(opts) do
opts
|> api_key()
|> build_client()
Expand All @@ -80,9 +80,9 @@ defmodule SendGrid do
* `:api_key` - API key to use with the request.
* `:query` - Keyword list of query params to use with the request.
"""
@spec patch(path :: String.t(), body :: map(), options :: options()) ::
@spec patch(path :: String.t(), body :: Tesla.Env.body(), options :: options()) ::
{:ok, Response.t()} | {:error, any()}
def patch(path, body, opts \\ []) when is_map(body) and is_list(opts) do
def patch(path, body, opts \\ []) when is_list(opts) do
opts
|> api_key()
|> build_client()
Expand Down
15 changes: 15 additions & 0 deletions lib/sendgrid/marketing_campaigns/contacts/lists.ex
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,21 @@ defmodule SendGrid.Contacts.Lists do
end
end

@doc """
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The docs don't match the functionality here.

Suggested change
@doc """
@doc """
Adds multiple recipients to an email list.
:ok = add_recipienst(123, ["recipient_id_1"])
"""

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

done

Adds multiple recipients to an email list.

:ok = add_recipients(123, ["recipient_id_1", "recipient_id_2"])

"""
@spec add_recipients(integer(), [String.t()], [SendGrid.api_key()]) :: :ok | {:error, any()}
def add_recipients(list_id, recipient_ids, opts \\ []) when is_list(recipient_ids) do
url = "#{@base_api_url}/#{list_id}/recipients"

with {:ok, %{status: 201}} <- SendGrid.post(url, recipient_ids, opts) do
:ok
end
end

@doc """
Deletes a recipient from an email list.

Expand Down
23 changes: 23 additions & 0 deletions lib/sendgrid/marketing_campaigns/contacts/recipients.ex
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,29 @@ defmodule SendGrid.Contacts.Recipients do
end
end

@doc """
Adds or updates multiple recipients in contacts list.
Recipients param must be in format required by Sendgrid:
[
%{
"email" => "[email protected]",
"name" => "John Doe",
etc...
}
]
"""
@spec add_multiple([]) :: {:ok, [String.t]} | {:ok, map()} | {:error, list(String.t)}
def add_multiple(recipients) when is_list(recipients) do
with {:ok, response} <- SendGrid.patch(@base_api_url, recipients) do
handle_recipient_result(response)
end
end

# Handles the result when there are multiple persisted recipients.
defp handle_recipient_result(%{body: %{"persisted_recipients" => recipients} = body}) when is_list(recipients) and length(recipients) > 1 do
{:ok, body}
end

# Handles the result when errors are present.
defp handle_recipient_result(%{body: %{"error_count" => count} = body}) when count > 0 do
errors = Enum.map(body["errors"], & &1["message"])
Expand Down