Skip to content

Commit

Permalink
Merge pull request #18 from primait/ISSUE-17
Browse files Browse the repository at this point in the history
Add HTTP timeout option
  • Loading branch information
richleland authored Jun 16, 2016
2 parents 26c3622 + 3dcd5ba commit 270ff6d
Show file tree
Hide file tree
Showing 4 changed files with 33 additions and 17 deletions.
1 change: 1 addition & 0 deletions config/config.exs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ use Mix.Config

config :sparkpost, api_endpoint: "https://api.sparkpost.com/api/v1/"
config :sparkpost, api_key: "YOUR API KEY HERE"
config :sparkpost, http_timeout: 5000

# This configuration is loaded before any dependency and is restricted
# to this project. If another project depends on this project, this
Expand Down
2 changes: 2 additions & 0 deletions lib/endpoint.ex
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,8 @@ defmodule SparkPost.Endpoint do
]
end

reqopts = [timeout: Application.get_env(:sparkpost, :http_timeout, 5000)] ++ reqopts

%{status_code: status_code, body: json} = HTTPotion.request(method, url, reqopts)

body = decode_response_body(json)
Expand Down
32 changes: 16 additions & 16 deletions mix.lock
Original file line number Diff line number Diff line change
@@ -1,17 +1,17 @@
%{"bunt": {:hex, :bunt, "0.1.4"},
"certifi": {:hex, :certifi, "0.3.0"},
"credo": {:hex, :credo, "0.3.0-dev"},
"earmark": {:hex, :earmark, "0.2.1"},
"ex_doc": {:hex, :ex_doc, "0.11.3"},
"excoveralls": {:hex, :excoveralls, "0.4.5"},
"exjsx": {:hex, :exjsx, "3.2.0"},
"hackney": {:hex, :hackney, "1.4.8"},
"httpotion": {:hex, :httpotion, "2.1.0"},
%{"bunt": {:hex, :bunt, "0.1.6", "5d95a6882f73f3b9969fdfd1953798046664e6f77ec4e486e6fafc7caad97c6f", [:mix], []},
"certifi": {:hex, :certifi, "0.3.0", "389d4b126a47895fe96d65fcf8681f4d09eca1153dc2243ed6babad0aac1e763", [:rebar3], []},
"credo": {:hex, :credo, "0.4.3", "29fe87aa2ef3c19bf8dd909594b9b79d9e2ed2857a153c00eb3f697f41cb6782", [:mix], [{:bunt, "~> 0.1.6", [hex: :bunt, optional: false]}]},
"earmark": {:hex, :earmark, "0.2.1", "ba6d26ceb16106d069b289df66751734802777a3cbb6787026dd800ffeb850f3", [:mix], []},
"ex_doc": {:hex, :ex_doc, "0.11.3", "bb16cb3f4135d880ce25279dc19a9d70802bc4f4942f0c3de9e4862517ae3ace", [:mix], [{:earmark, "~> 0.1.17 or ~> 0.2", [hex: :earmark, optional: true]}]},
"excoveralls": {:hex, :excoveralls, "0.4.5", "1508e1c7f373f82805975c633e2468a83898b2b902acf79e7359486d71186ea3", [:mix], [{:hackney, ">= 0.12.0", [hex: :hackney, optional: false]}, {:exjsx, "~> 3.0", [hex: :exjsx, optional: false]}]},
"exjsx": {:hex, :exjsx, "3.2.0", "7136cc739ace295fc74c378f33699e5145bead4fdc1b4799822d0287489136fb", [:mix], [{:jsx, "~> 2.6.2", [hex: :jsx, optional: false]}]},
"hackney": {:hex, :hackney, "1.4.8", "c8c6977ed55cc5095e3929f6d94a6f732dd2e31ae42a7b9236d5574ec3f5be13", [:rebar3], [{:ssl_verify_hostname, "1.0.5", [hex: :ssl_verify_hostname, optional: false]}, {:mimerl, "1.0.2", [hex: :mimerl, optional: false]}, {:idna, "1.0.3", [hex: :idna, optional: false]}, {:certifi, "0.3.0", [hex: :certifi, optional: false]}]},
"httpotion": {:hex, :httpotion, "2.1.0", "3fe84fbd13d4560c2514da656d022b1191a079178ee4992d245fc3c33c01ee18", [:mix], []},
"ibrowse": {:git, "https://github.com/cmullaparthi/ibrowse.git", "ea3305d21f37eced4fac290f64b068e56df7de80", [tag: "v4.1.2"]},
"idna": {:hex, :idna, "1.0.3"},
"jsx": {:hex, :jsx, "2.6.2"},
"meck": {:hex, :meck, "0.8.4"},
"mimerl": {:hex, :mimerl, "1.0.2"},
"mock": {:hex, :mock, "0.1.1"},
"poison": {:hex, :poison, "1.5.2"},
"ssl_verify_hostname": {:hex, :ssl_verify_hostname, "1.0.5"}}
"idna": {:hex, :idna, "1.0.3", "d456a8761cad91c97e9788c27002eb3b773adaf5c893275fc35ba4e3434bbd9b", [:rebar3], []},
"jsx": {:hex, :jsx, "2.6.2", "213721e058da0587a4bce3cc8a00ff6684ced229c8f9223245c6ff2c88fbaa5a", [:mix, :rebar], []},
"meck": {:hex, :meck, "0.8.4", "59ca1cd971372aa223138efcf9b29475bde299e1953046a0c727184790ab1520", [:rebar, :make], []},
"mimerl": {:hex, :mimerl, "1.0.2", "993f9b0e084083405ed8252b99460c4f0563e41729ab42d9074fd5e52439be88", [:rebar3], []},
"mock": {:hex, :mock, "0.1.1", "e21469ca27ba32aa7b18b61699db26f7a778171b21c0e5deb6f1218a53278574", [:mix], [{:meck, "~> 0.8.2", [hex: :meck, optional: false]}]},
"poison": {:hex, :poison, "1.5.2", "560bdfb7449e3ddd23a096929fb9fc2122f709bcc758b2d5d5a5c7d0ea848910", [:mix], []},
"ssl_verify_hostname": {:hex, :ssl_verify_hostname, "1.0.5", "2e73e068cd6393526f9fa6d399353d7c9477d6886ba005f323b592d389fb47be", [:make], []}}
15 changes: 14 additions & 1 deletion test/endpoint_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@ defmodule SparkPost.EndpointTest do
respfn.(method, url, opts)
end
] do
Enum.each([:get, :post, :put, :delete], fn method ->
Enum.each([:get, :post, :put, :delete], fn method ->
Endpoint.request(method, "transmissions", []) end)
end
end
Expand All @@ -102,4 +102,17 @@ defmodule SparkPost.EndpointTest do
Endpoint.request(:put, "transmissions", [body: %{}])
end
end

test "Endpoint.request includes request timeout" do
respfn = MockServer.mk_resp
with_mock HTTPotion, [request: fn (method, url, opts) ->
assert Keyword.has_key?(opts, :timeout)
respfn.(method, url, opts)
end
] do
Endpoint.request(:post, "transmissions", [])
Endpoint.request(:put, "transmissions", [])
Endpoint.request(:get, "transmissions", [])
end
end
end

0 comments on commit 270ff6d

Please sign in to comment.