Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Pass Hackney options from config #506

Merged
merged 2 commits into from
Jul 12, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
11 changes: 11 additions & 0 deletions lib/stripe/api.ex
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down Expand Up @@ -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()
Expand All @@ -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()
Expand Down
40 changes: 40 additions & 0 deletions test/stripe/api_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -39,4 +39,44 @@ 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