Skip to content

Commit 9a8f9fd

Browse files
committed
add finch client module and deps
1 parent 616f07e commit 9a8f9fd

File tree

2 files changed

+54
-0
lines changed

2 files changed

+54
-0
lines changed

lib/sentry/finch_client.ex

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
defmodule Sentry.FinchClient do
2+
@behaviour Sentry.HTTPClient
3+
4+
@moduledoc """
5+
The built-in HTTP client.
6+
This client implements the `Sentry.HTTPClient` behaviour.
7+
It's based on the [Finch](https://github.com/sneako/finch) Elixir HTTP client,
8+
which is an *optional dependency* of this library. If you wish to use another
9+
HTTP client, you'll have to implement your own `Sentry.HTTPClient`. See the
10+
documentation for `Sentry.HTTPClient` for more information.
11+
Finch is built on top of [NimblePool](https://github.com/dashbitco/nimble_pool). If you need to set other pool configuration options,
12+
see "Pool Configuration Options" in the Finch documentation for details on the possible map values.
13+
[finch configuration options](https://hexdocs.pm/finch/Finch.html#start_link/1-pool-configuration-options)
14+
"""
15+
@impl true
16+
def child_spec do
17+
if Code.ensure_loaded?(Finch) do
18+
case Application.ensure_all_started(:finch) do
19+
{:ok, _apps} -> :ok
20+
{:error, reason} -> raise "failed to start the :finch application: #{inspect(reason)}"
21+
end
22+
23+
Finch.child_spec(
24+
name: __MODULE__,
25+
pools: %{
26+
:default => [
27+
size: Sentry.Config.max_finch_connections(),
28+
conn_max_idle_time: Sentry.Config.finch_timeout()
29+
]
30+
}
31+
)
32+
else
33+
raise """
34+
cannot start the :sentry application because the HTTP client is set to \
35+
Sentry.FinchClient (which is the default), but the :finch library is not loaded. \
36+
Add :finch to your dependencies to fix this.
37+
"""
38+
end
39+
end
40+
41+
@impl true
42+
def post(url, headers, body) do
43+
request = Finch.build(:post, url, headers, body)
44+
45+
case Finch.request(request, __MODULE__) do
46+
{:ok, %Finch.Response{status: status, headers: headers, body: body}} ->
47+
{:ok, status, headers, body}
48+
49+
{:error, error} ->
50+
{:error, error}
51+
end
52+
end
53+
end

mix.exs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -100,6 +100,7 @@ defmodule Sentry.Mixfile do
100100

101101
# Optional dependencies
102102
{:hackney, "~> 1.8", optional: true},
103+
{:finch, "~> 0.19", optional: true},
103104
{:jason, "~> 1.1", optional: true},
104105
{:phoenix, "~> 1.6", optional: true},
105106
{:phoenix_live_view, "~> 0.20 or ~> 1.0", optional: true},

0 commit comments

Comments
 (0)