Skip to content

Commit 1689a98

Browse files
author
Ashu Goel
committed
[suppression_list] add API to delete an entry
1 parent e20f6da commit 1689a98

File tree

4 files changed

+57
-5
lines changed

4 files changed

+57
-5
lines changed

lib/endpoint.ex

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -36,9 +36,9 @@ defmodule SparkPost.Endpoint do
3636
url = Application.get_env(:sparkpost, :api_endpoint, @default_endpoint) <> endpoint
3737

3838
{:ok, request_body} = encode_request_body(body)
39-
39+
4040
request_headers = if method in [:get, :delete] do
41-
headers
41+
headers
4242
else
4343
Map.merge(headers, %{"Content-Type": "application/json"})
4444
end
@@ -73,9 +73,9 @@ defmodule SparkPost.Endpoint do
7373
defp handle_response({:ok, %HTTPoison.Response{status_code: code, body: body}}, decode_results) when code >= 200 and code < 300 do
7474
decoded_body = decode_response_body(body)
7575
if decode_results do
76-
%SparkPost.Endpoint.Response{status_code: 200, results: decoded_body.results}
76+
%SparkPost.Endpoint.Response{status_code: code, results: decoded_body.results}
7777
else
78-
%SparkPost.Endpoint.Response{status_code: 200, results: decoded_body}
78+
%SparkPost.Endpoint.Response{status_code: code, results: decoded_body}
7979
end
8080
end
8181

@@ -100,6 +100,7 @@ defmodule SparkPost.Endpoint do
100100
body |> Washup.filter |> Poison.encode
101101
end
102102

103+
defp decode_response_body(body) when byte_size(body) == 0, do: ""
103104
defp decode_response_body(body) do
104105
# TODO: [key: :atoms] is unsafe for open-ended structures such as
105106
# metadata and substitution_data

lib/suppression_list.ex

Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,39 @@
11
defmodule SparkPost.SuppressionList do
22
@moduledoc """
33
The SparkPost Suppression List API for working with suppression lists.
4-
Use `SparkPost.SuppressionList.search/1` to search through your account's suppression list.
4+
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.
56
67
Check out the documentation for each function
78
or use the [SparkPost API reference](https://developers.sparkpost.com/api/suppression_list.html) for details.
89
10+
Returned by `SparkPost.SuppressionList.delete/1`:
11+
- nothing (empty body)
12+
913
Returned by `SparkPost.SuppressionList.search/1`.
1014
- %SparkPost.SuppressionList.SearchResult{}
1115
"""
1216

1317
alias SparkPost.Endpoint
1418

19+
@doc """
20+
Deletes a specific entry from the list. Returns an empty string if
21+
the deletion was successful. Returns a %SparkPost.Endpoint.Error{} with a 404
22+
if the specified entry is not in the list. Returns a %SparkPost.Endpoint.Error{}
23+
with a 403 if the entry could not be removed for any reason (such as Compliance).
24+
25+
Parameters:
26+
recipient: the entry to delete from the suppression list.
27+
"""
28+
def delete(recipient) do
29+
response = Endpoint.request(:delete, "suppression-list/#{recipient}", %{}, %{}, [], false)
30+
case response do
31+
%SparkPost.Endpoint.Response{status_code: 204} ->
32+
""
33+
_ -> response
34+
end
35+
end
36+
1537
@doc """
1638
Execute a search of the suppression list based on the provided
1739
parameters.
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
{
2+
"errors": [
3+
{
4+
"message": "Recipient could not be found"
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.delete succeeds with empty body",
12+
HTTPoison, [request: fn (method, url, body, headers, opts) ->
13+
assert method == :delete
14+
fun = MockServer.mk_http_resp(204, "")
15+
fun.(method, url, body, headers, opts)
16+
end] do
17+
resp = SuppressionList.delete("test@marketing.com")
18+
assert resp == ""
19+
end
20+
21+
test_with_mock "SuppressionList.delete fails 404",
22+
HTTPoison, [request: fn (method, url, body, headers, opts) ->
23+
assert method == :delete
24+
fun = MockServer.mk_http_resp(404, MockServer.get_json("suppressiondelete_fail"))
25+
fun.(method, url, body, headers, opts)
26+
end] do
27+
resp = SuppressionList.delete("test@marketing.com")
28+
assert %SparkPost.Endpoint.Error{} = resp
29+
assert resp.status_code == 404
30+
assert resp.errors == [%{message: "Recipient could not be found"}]
31+
end
32+
1133
test_with_mock "SuppressionList.search succeeds with SuppressionList.SearchResult",
1234
HTTPoison, [request: fn (method, url, body, headers, opts) ->
1335
assert method == :get

0 commit comments

Comments
 (0)