From bc216d6f1191caaaf0468aba098e245e927d6111 Mon Sep 17 00:00:00 2001 From: Marcin Biegun Date: Tue, 28 May 2019 18:22:25 +0200 Subject: [PATCH 1/2] Pass Hackney options from config --- README.md | 8 ++++++++ lib/stripe/api.ex | 11 +++++++++++ test/stripe/api_test.exs | 36 ++++++++++++++++++++++++++++++++++++ 3 files changed, 55 insertions(+) diff --git a/README.md b/README.md index fe2801ee..5d7df656 100644 --- a/README.md +++ b/README.md @@ -80,6 +80,14 @@ Moreover, if you are using Jason instead of Poison, you can configure the librar config :stripity_stripe, json_library: Jason ``` +### Timeout + +To set timeouts, pass opts for the http client. The default one is Hackney. + +```ex +config :stripity_stripe, hackney_opts: [{:connect_timeout, 1000}, {:recv_timeout, 5000}]) +``` + ## Note: Object Expansion Some Stripe API endpoints support returning related objects via the object expansion query parameter. To take advantage of this feature, stripity_stripe accepts diff --git a/lib/stripe/api.ex b/lib/stripe/api.ex index b99a1280..a5ef5f04 100644 --- a/lib/stripe/api.ex +++ b/lib/stripe/api.ex @@ -135,6 +135,15 @@ defmodule Stripe.API do end end + @spec add_options_from_config(list) :: list + defp add_options_from_config(opts) do + if is_list(Stripe.Config.resolve(:hackney_opts)) do + opts ++ Stripe.Config.resolve(:hackney_opts) + else + opts + end + end + @doc """ A low level utility function to make a direct request to the Stripe API @@ -233,6 +242,7 @@ defmodule Stripe.API do [] |> add_default_options() |> add_pool_option() + |> add_options_from_config() http_module().request(method, req_url, req_headers, req_body, req_opts) |> handle_response() @@ -255,6 +265,7 @@ defmodule Stripe.API do opts |> add_default_options() |> add_pool_option() + |> add_options_from_config() http_module().request(method, req_url, req_headers, body, req_opts) |> handle_response() diff --git a/test/stripe/api_test.exs b/test/stripe/api_test.exs index eb658feb..eb6f8d18 100644 --- a/test/stripe/api_test.exs +++ b/test/stripe/api_test.exs @@ -39,4 +39,40 @@ defmodule Stripe.APITest do {:ok, body} = Stripe.API.oauth_request(:post, "token", %{}) assert Map.keys(body) |> Enum.member?("Authorization") == false end + + test "reads hackney timeout opts from config" do + # Return request opts as response body + defmodule HackneyMock do + def request(_, _, headers, _, opts) do + kv_opts = + opts + |> Enum.reduce(%{}, fn opt, acc -> + case opt do + {k, v} -> + Map.put(acc, k, v) + _ -> + Map.put(acc, opt, opt) + end + end) + + {:ok, 200, headers, Poison.encode!(kv_opts)} + end + end + + Application.put_env(:stripity_stripe, :http_module, HackneyMock) + + {:ok, request_opts} = Stripe.API.request(%{}, :get, "/", %{}, []) + refute Map.has_key?(request_opts, "connect_timeout") + refute Map.has_key?(request_opts, "recv_timeout") + + Application.put_env(:stripity_stripe, :hackney_opts, [{:connect_timeout, 1000}, {:recv_timeout, 5000}]) + + {:ok, request_opts} = Stripe.API.oauth_request(:post, "token", %{}) + assert request_opts["connect_timeout"] == 1000 + assert request_opts["recv_timeout"] == 5000 + + {:ok, request_opts} = Stripe.API.request(%{}, :get, "/", %{}, []) + assert request_opts["connect_timeout"] == 1000 + assert request_opts["recv_timeout"] == 5000 + end end From 0e4c2136658e788337e648125d31b298c3ddb671 Mon Sep 17 00:00:00 2001 From: Marcin Biegun Date: Wed, 29 May 2019 11:30:51 +0200 Subject: [PATCH 2/2] Mix format --- test/stripe/api_test.exs | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/test/stripe/api_test.exs b/test/stripe/api_test.exs index eb6f8d18..6ead13cf 100644 --- a/test/stripe/api_test.exs +++ b/test/stripe/api_test.exs @@ -50,6 +50,7 @@ defmodule Stripe.APITest do case opt do {k, v} -> Map.put(acc, k, v) + _ -> Map.put(acc, opt, opt) end @@ -65,7 +66,10 @@ defmodule Stripe.APITest do refute Map.has_key?(request_opts, "connect_timeout") refute Map.has_key?(request_opts, "recv_timeout") - Application.put_env(:stripity_stripe, :hackney_opts, [{:connect_timeout, 1000}, {:recv_timeout, 5000}]) + Application.put_env(:stripity_stripe, :hackney_opts, [ + {:connect_timeout, 1000}, + {:recv_timeout, 5000} + ]) {:ok, request_opts} = Stripe.API.oauth_request(:post, "token", %{}) assert request_opts["connect_timeout"] == 1000