SDKs

Go

Idiomatische Go-Patterns für die Thunderbit Open API

Verwende net/http + encoding/json. Kein SDK nötig. Für hohe Nebenläufigkeit kombinierst du es mit einem Goroutine-Worker-Pool oder nutzt golang.org/x/sync/errgroup.

Client

package thunderbit

import (
    "bytes"
    "encoding/json"
    "fmt"
    "io"
    "net/http"
    "os"
)

const API = "https://openapi.thunderbit.com/openapi/v1"

type Client struct {
    HTTP *http.Client
    Key  string
}

func New() *Client {
    return &Client{HTTP: http.DefaultClient, Key: os.Getenv("THUNDERBIT_API_KEY")}
}

func (c *Client) post(path string, body any, out any) error {
    raw, _ := json.Marshal(body)
    req, _ := http.NewRequest("POST", API+path, bytes.NewReader(raw))
    req.Header.Set("Authorization", "Bearer "+c.Key)
    req.Header.Set("Content-Type", "application/json")
    resp, err := c.HTTP.Do(req)
    if err != nil { return err }
    defer resp.Body.Close()
    if resp.StatusCode >= 400 {
        b, _ := io.ReadAll(resp.Body)
        return fmt.Errorf("thunderbit: %s: %s", resp.Status, b)
    }
    return json.NewDecoder(resp.Body).Decode(out)
}

Eine Seite distillen

type DistillResp struct {
    Data struct { Markdown string `json:"markdown"` } `json:"data"`
}

c := New()
var out DistillResp
if err := c.post("/distill",
    map[string]any{"url": "https://thunderbit.com/playground"}, &out); err != nil {
    panic(err)
}
fmt.Println(out.Data.Markdown)

Tipps

  • Nutze einen einzelnen *http.Client über alle Goroutinen hinweg — das ist sicher und poolt Verbindungen
  • Schließe Response-Bodies immer (defer resp.Body.Close()), sonst leakst du File Descriptors
  • Bei 10+ URLs verwende lieber /batch/distill — siehe Batch Job Lifecycle

Ein offizielles Go SDK ist in Entwicklung — schau bald wieder rein.