Aurinko.Webhook.Handler behaviour (Aurinko v0.2.1)

Copy Markdown View Source

Behaviour for implementing Aurinko webhook event handlers.

Usage

defmodule MyApp.AurinkoHandler do
  @behaviour Aurinko.Webhook.Handler

  @impl true
  def handle_event("email.new", payload, _meta) do
    MyApp.Mailbox.process_new_email(payload)
    :ok
  end

  def handle_event("calendar.event.updated", payload, _meta) do
    MyApp.Calendar.sync_event(payload)
    :ok
  end

  def handle_event(_event, _payload, _meta), do: :ok
end

Then in your router/controller, after verifying the signature:

Aurinko.Webhook.Handler.dispatch(MyApp.AurinkoHandler, raw_body)

Summary

Callbacks

Handle a parsed Aurinko webhook event.

Functions

Parse, verify, and dispatch a raw webhook body to a handler module.

Types

event_type()

@type event_type() :: String.t()

meta()

@type meta() :: %{raw_body: binary(), verified: boolean()}

payload()

@type payload() :: map()

result()

@type result() :: :ok | {:error, term()}

Callbacks

handle_event(event_type, payload, meta)

@callback handle_event(event_type(), payload(), meta()) :: result()

Handle a parsed Aurinko webhook event.

Functions

dispatch(handler, raw_body, signature \\ nil, opts \\ [])

@spec dispatch(module(), binary(), String.t() | nil, keyword()) ::
  :ok | {:error, term()}

Parse, verify, and dispatch a raw webhook body to a handler module.

Returns {:error, :invalid_signature} if signature verification fails.