Skip to content

Commit 9035124

Browse files
author
Ashu Goel
committed
[suppression_list] add ability to insert/update a single entry
1 parent fc0c4cc commit 9035124

File tree

4 files changed

+64
-1
lines changed

4 files changed

+64
-1
lines changed

lib/suppression_list.ex

Lines changed: 30 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,20 +2,49 @@ defmodule SparkPost.SuppressionList do
22
@moduledoc """
33
The SparkPost Suppression List API for working with suppression lists.
44
Use `SparkPost.SuppressionList.delete/1` to delete a single entry from a list,
5-
`SparkPost.SuppressionList.search/1` to search through your account's suppression list.
5+
`SparkPost.SuppressionList.update_one/3` to insert or update a single list entry,
6+
or `SparkPost.SuppressionList.search/1` to search through your account's suppression list.
67
78
Check out the documentation for each function
89
or use the [SparkPost API reference](https://developers.sparkpost.com/api/suppression_list.html) for details.
910
1011
Returned by `SparkPost.SuppressionList.delete/1`:
1112
- nothing (empty body)
1213
14+
Returned by `SparkPost.SuppressionList.update_one/3`:
15+
- message (A success message string)
16+
1317
Returned by `SparkPost.SuppressionList.search/1`.
1418
- %SparkPost.SuppressionList.SearchResult{}
1519
"""
1620

1721
alias SparkPost.Endpoint
1822

23+
@doc """
24+
Insert or update a single entry in the suppression list.
25+
Returns a single string with the success message if the entry
26+
was updated or inserted. Returns a %SparkPost.Endpoint.Error{} with a 400
27+
if the type is invalid.
28+
29+
Parameters:
30+
- recipient: the email to insert or update in the suppression list
31+
- type: one of "transactional" or "non_transactional"
32+
- description (optional): optional description of this entry in the suppression list
33+
"""
34+
def update_one(recipient, type, description \\ nil) do
35+
body = if description == nil do
36+
%{type: type}
37+
else
38+
%{type: type, description: description}
39+
end
40+
response = Endpoint.request(:put, "suppression-list/#{recipient}", body)
41+
case response do
42+
%SparkPost.Endpoint.Response{status_code: 200, results: results} ->
43+
Map.get(results, :message, "")
44+
_ -> response
45+
end
46+
end
47+
1948
@doc """
2049
Deletes a specific entry from the list. Returns an empty string if
2150
the deletion was successful. Returns a %SparkPost.Endpoint.Error{} with a 404
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
{
2+
"results": {
3+
"message": "Test response message"
4+
}
5+
}
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
{
2+
"errors": [
3+
{
4+
"message": "Type must be one of: 'transactional', 'non_transactional'"
5+
}
6+
]
7+
}

test/suppression_list_test.exs

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,28 @@ defmodule SparkPost.SuppressionListTest do
88

99
import Mock
1010

11+
test_with_mock "SuppressionList.update_one succeeds with message",
12+
HTTPoison, [request: fn (method, url, body, headers, opts) ->
13+
assert method == :put
14+
fun = MockServer.mk_http_resp(200, MockServer.get_json("suppressionlistupdate"))
15+
fun.(method, url, body, headers, opts)
16+
end] do
17+
resp = SuppressionList.update_one("test@marketing.com", "non_transactional", "test description")
18+
assert resp == "Test response message"
19+
end
20+
21+
test_with_mock "SuppressionList.update_one fails with invalid type",
22+
HTTPoison, [request: fn (method, url, body, headers, opts) ->
23+
assert method == :put
24+
fun = MockServer.mk_http_resp(400, MockServer.get_json("suppressionupdate_fail"))
25+
fun.(method, url, body, headers, opts)
26+
end] do
27+
resp = SuppressionList.update_one("test@marketing.com", "bad_type")
28+
assert %SparkPost.Endpoint.Error{} = resp
29+
assert resp.status_code == 400
30+
assert resp.errors == [%{message: "Type must be one of: 'transactional', 'non_transactional'"}]
31+
end
32+
1133
test_with_mock "SuppressionList.delete succeeds with empty body",
1234
HTTPoison, [request: fn (method, url, body, headers, opts) ->
1335
assert method == :delete

0 commit comments

Comments
 (0)