Skip to content

Commit

Permalink
Allow httpoison request options in config
Browse files Browse the repository at this point in the history
  • Loading branch information
PJUllrich committed Oct 26, 2023
1 parent 87a22f2 commit 9eaed79
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 5 deletions.
3 changes: 3 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,9 @@ import Config
config :lemon_ex,
api_key: System.get_env("LEMONSQUEEZY_API_KEY"),
webhook_secret: System.get_env("LEMONSQUEEZY_WEBHOOK_SECRET")
# (Optional) You can provide HTTPoision options which are added to every request.
# See all options here: https://hexdocs.pm/httpoison/HTTPoison.Request.html#content
request_optionts: [timeout: 10_000]
```

If you don't provide a valid API key, you will receive `401: Unauthorized` error responses.
Expand Down
3 changes: 2 additions & 1 deletion config/config.exs
Original file line number Diff line number Diff line change
Expand Up @@ -3,5 +3,6 @@ import Config
if Mix.env() in [:dev, :test] do
config :lemon_ex,
api_key: System.get_env("LEMONSQUEEZY_API_KEY", "foobar"),
webhook_secret: System.get_env("LEMONSQUEEZY_WEBHOOK_SECRET", "barfoo")
webhook_secret: System.get_env("LEMONSQUEEZY_WEBHOOK_SECRET", "barfoo"),
request_options: [recv_timeout: 5000]
end
16 changes: 12 additions & 4 deletions lib/lemon_ex/request.ex
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,8 @@ defmodule LemonEx.Request do
headers = get_headers()
url = "#{@api_base_url}#{url}"
payload = prepare_payload(payload)
response = HTTPoison.post(url, payload, headers)
opts = request_options()
response = HTTPoison.post(url, payload, headers, opts)
handle_response(response)
end

Expand All @@ -15,7 +16,8 @@ defmodule LemonEx.Request do
headers = get_headers()
url = "#{@api_base_url}#{url}"
filter = prepare_filter(params)
response = HTTPoison.get(url, headers, params: filter)
opts = [params: filter] ++ request_options()
response = HTTPoison.get(url, headers, opts)
handle_response(response)
end

Expand All @@ -24,22 +26,28 @@ defmodule LemonEx.Request do
headers = get_headers()
url = "#{@api_base_url}#{url}"
payload = prepare_payload(payload)
response = HTTPoison.patch(url, payload, headers)
opts = request_options()
response = HTTPoison.patch(url, payload, headers, opts)
handle_response(response)
end

@spec delete(binary()) :: :ok | {:ok, map()} | {:error, any()} | {:error, integer(), any()}
def delete(url) do
headers = get_headers()
url = "#{@api_base_url}#{url}"
response = HTTPoison.delete(url, headers)
opts = request_options()
response = HTTPoison.delete(url, headers, opts)
handle_response(response)
end

defp api_key do
Application.get_env(:lemon_ex, :api_key, "")
end

defp request_options do
Application.get_env(:lemon_ex, :request_options, [])
end

defp get_headers do
[
{"Authorization", "Bearer #{api_key()}"},
Expand Down

0 comments on commit 9eaed79

Please sign in to comment.