Skip to content

Commit d8efa11

Browse files
author
Ashu Goel
committed
[suppression_list] minor PR updates. start moving towards more idiomatic {:ok | :error, resp} tuples
1 parent 9035124 commit d8efa11

File tree

2 files changed

+16
-16
lines changed

2 files changed

+16
-16
lines changed

lib/suppression_list.ex

+10-10
Original file line numberDiff line numberDiff line change
@@ -2,17 +2,17 @@ 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.update_one/3` to insert or update a single list entry,
5+
`SparkPost.SuppressionList.upsert_one/3` to insert or update a single list entry,
66
or `SparkPost.SuppressionList.search/1` to search through your account's suppression list.
77
88
Check out the documentation for each function
99
or use the [SparkPost API reference](https://developers.sparkpost.com/api/suppression_list.html) for details.
1010
1111
Returned by `SparkPost.SuppressionList.delete/1`:
12-
- nothing (empty body)
12+
- {:ok, ""}
1313
14-
Returned by `SparkPost.SuppressionList.update_one/3`:
15-
- message (A success message string)
14+
Returned by `SparkPost.SuppressionList.upsert_one/3`:
15+
- {:ok, message} (A success message string)
1616
1717
Returned by `SparkPost.SuppressionList.search/1`.
1818
- %SparkPost.SuppressionList.SearchResult{}
@@ -24,14 +24,14 @@ defmodule SparkPost.SuppressionList do
2424
Insert or update a single entry in the suppression list.
2525
Returns a single string with the success message if the entry
2626
was updated or inserted. Returns a %SparkPost.Endpoint.Error{} with a 400
27-
if the type is invalid.
27+
if there was an issue with the request format.
2828
2929
Parameters:
3030
- recipient: the email to insert or update in the suppression list
3131
- type: one of "transactional" or "non_transactional"
3232
- description (optional): optional description of this entry in the suppression list
3333
"""
34-
def update_one(recipient, type, description \\ nil) do
34+
def upsert_one(recipient, type, description \\ nil) do
3535
body = if description == nil do
3636
%{type: type}
3737
else
@@ -40,8 +40,8 @@ defmodule SparkPost.SuppressionList do
4040
response = Endpoint.request(:put, "suppression-list/#{recipient}", body)
4141
case response do
4242
%SparkPost.Endpoint.Response{status_code: 200, results: results} ->
43-
Map.get(results, :message, "")
44-
_ -> response
43+
{:ok, Map.get(results, :message, "")}
44+
_ -> {:error, response}
4545
end
4646
end
4747

@@ -58,8 +58,8 @@ defmodule SparkPost.SuppressionList do
5858
response = Endpoint.request(:delete, "suppression-list/#{recipient}", %{}, %{}, [], false)
5959
case response do
6060
%SparkPost.Endpoint.Response{status_code: 204} ->
61-
""
62-
_ -> response
61+
{:ok, ""}
62+
_ -> {:error, response}
6363
end
6464
end
6565

test/suppression_list_test.exs

+6-6
Original file line numberDiff line numberDiff line change
@@ -8,23 +8,23 @@ defmodule SparkPost.SuppressionListTest do
88

99
import Mock
1010

11-
test_with_mock "SuppressionList.update_one succeeds with message",
11+
test_with_mock "SuppressionList.upsert_one succeeds with message",
1212
HTTPoison, [request: fn (method, url, body, headers, opts) ->
1313
assert method == :put
1414
fun = MockServer.mk_http_resp(200, MockServer.get_json("suppressionlistupdate"))
1515
fun.(method, url, body, headers, opts)
1616
end] do
17-
resp = SuppressionList.update_one("test@marketing.com", "non_transactional", "test description")
17+
{:ok, resp} = SuppressionList.upsert_one("test@marketing.com", "non_transactional", "test description")
1818
assert resp == "Test response message"
1919
end
2020

21-
test_with_mock "SuppressionList.update_one fails with invalid type",
21+
test_with_mock "SuppressionList.upsert_one fails with invalid type",
2222
HTTPoison, [request: fn (method, url, body, headers, opts) ->
2323
assert method == :put
2424
fun = MockServer.mk_http_resp(400, MockServer.get_json("suppressionupdate_fail"))
2525
fun.(method, url, body, headers, opts)
2626
end] do
27-
resp = SuppressionList.update_one("test@marketing.com", "bad_type")
27+
{:error, resp} = SuppressionList.upsert_one("test@marketing.com", "bad_type")
2828
assert %SparkPost.Endpoint.Error{} = resp
2929
assert resp.status_code == 400
3030
assert resp.errors == [%{message: "Type must be one of: 'transactional', 'non_transactional'"}]
@@ -36,7 +36,7 @@ defmodule SparkPost.SuppressionListTest do
3636
fun = MockServer.mk_http_resp(204, "")
3737
fun.(method, url, body, headers, opts)
3838
end] do
39-
resp = SuppressionList.delete("test@marketing.com")
39+
{:ok, resp} = SuppressionList.delete("test@marketing.com")
4040
assert resp == ""
4141
end
4242

@@ -46,7 +46,7 @@ defmodule SparkPost.SuppressionListTest do
4646
fun = MockServer.mk_http_resp(404, MockServer.get_json("suppressiondelete_fail"))
4747
fun.(method, url, body, headers, opts)
4848
end] do
49-
resp = SuppressionList.delete("test@marketing.com")
49+
{:error, resp} = SuppressionList.delete("test@marketing.com")
5050
assert %SparkPost.Endpoint.Error{} = resp
5151
assert resp.status_code == 404
5252
assert resp.errors == [%{message: "Recipient could not be found"}]

0 commit comments

Comments
 (0)