Skip to content

Commit

Permalink
Pass Hackney options from config (beam-community#506)
Browse files Browse the repository at this point in the history
  • Loading branch information
marcinbiegun authored and jalexander committed Nov 20, 2024
1 parent af81806 commit d96153f
Show file tree
Hide file tree
Showing 3 changed files with 59 additions and 0 deletions.
8 changes: 8 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,14 @@ Moreover, if you are using Poison instead of Jason, you can configure the librar
config :stripity_stripe, json_library: Poison
```

### 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

0 comments on commit d96153f

Please sign in to comment.