diff --git a/lib/sendgrid.ex b/lib/sendgrid.ex index 5269c81..7f8208d 100644 --- a/lib/sendgrid.ex +++ b/lib/sendgrid.ex @@ -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() @@ -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() diff --git a/lib/sendgrid/marketing_campaigns/contacts/lists.ex b/lib/sendgrid/marketing_campaigns/contacts/lists.ex index bb21235..b961a63 100644 --- a/lib/sendgrid/marketing_campaigns/contacts/lists.ex +++ b/lib/sendgrid/marketing_campaigns/contacts/lists.ex @@ -82,6 +82,21 @@ defmodule SendGrid.Contacts.Lists do end end + @doc """ + 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. diff --git a/lib/sendgrid/marketing_campaigns/contacts/recipients.ex b/lib/sendgrid/marketing_campaigns/contacts/recipients.ex index 0f3e1b1..3595c08 100644 --- a/lib/sendgrid/marketing_campaigns/contacts/recipients.ex +++ b/lib/sendgrid/marketing_campaigns/contacts/recipients.ex @@ -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" => "test@example.com", + "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"])