|
| 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 |
0 commit comments