SDKs

Elixir

Phoenix 與 OTP 上的 Elixir 地道寫法

Req(現代 Elixir HTTP client)—— 建構於 Finch 之上,內建重試與連線池。

mix.exs

defp deps do
  [
    {:req, "~> 0.5"}
  ]
end

設定

defmodule Thunderbit do
  @api "https://openapi.thunderbit.com/openapi/v1"

  defp client do
    Req.new(
      base_url: @api,
      auth: {:bearer, System.fetch_env!("THUNDERBIT_API_KEY")},
      receive_timeout: 60_000
    )
  end
end

Distill 一個頁面

def distill(url) do
  client()
  |> Req.post!(url: "/distill", json: %{url: url})
  |> Map.fetch!(:body)
  |> get_in(["data", "markdown"])
end

Extract 結構化資料

def extract(url, schema) do
  client()
  |> Req.post!(url: "/extract", json: %{url: url, schema: schema})
  |> Map.fetch!(:body)
  |> Map.fetch!("data")
end

extract("https://example.com/product/iphone-15-pro", %{
  type: "object",
  properties: %{
    name: %{type: "string"},
    price: %{type: "number"}
  },
  required: ["name", "price"]
})

用 Oban 做批次

非同步散發時,用 Oban 把提交丟進佇列,讓你的 Phoenix endpoint 處理 Webhook 回呼:

def submit_batch(urls) do
  client()
  |> Req.post!(url: "/batch/distill", json: %{
    urls: urls,
    webhook: %{
      url: "#{MyApp.Endpoint.url()}/webhooks/distill",
      secret: System.fetch_env!("WEBHOOK_SECRET")
    }
  })
end

在你的 Phoenix controller 裡驗證 Webhook 簽名 —— 參見 Webhooks

官方 Elixir SDK 開發中 —— 敬請期待。