Kepler.Transport behaviour (Kepler v0.1.0)

Copy Markdown View Source

How Kepler.Sink.Webhook makes an HTTP request.

Kepler's only runtime dependency is :telemetry, so the default transport is Kepler.Transport.Httpc — OTP's own client, which is already in your release. It is not the fastest client available, but a webhook that fires once every few minutes does not need to be, and the alternative is making every host application take on an HTTP dependency it may already have a different opinion about.

If you already run Finch, Req, or Mint, implement this behaviour and pass it as transport::

defmodule MyApp.KeplerTransport do
  @behaviour Kepler.Transport

  @impl true
  def post(url, headers, body, _opts) do
    case Finch.build(:post, url, headers, body) |> Finch.request(MyApp.Finch) do
      {:ok, %{status: status}} -> {:ok, status}
      {:error, reason} -> {:error, reason}
    end
  end
end

config :kepler,
  sinks: [{Kepler.Sink.Webhook, url: ..., transport: MyApp.KeplerTransport}]

Summary

Callbacks

post(url, headers, body, opts)

@callback post(
  url :: String.t(),
  headers :: [{String.t(), String.t()}],
  body :: iodata(),
  opts :: keyword()
) :: {:ok, status :: non_neg_integer()} | {:error, term()}

POSTs body to url.

Returns the response status — Kepler treats 2xx as delivered and anything else as a failure. Do not retry inside a transport; a wedged endpoint should produce drops, not a growing queue.