SDKs

Elixir

Phoenix 与 OTP 上的地道 Elixir 写法

Req(现代的 Elixir HTTP 客户端)—— 基于 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 做 Batch

异步扇出场景,用 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 正在开发中,敬请期待。