SDKs
Go
Pattern Go idiomatici per la Open API di Thunderbit
Usa net/http + encoding/json. Nessun SDK richiesto. Per alta concorrenza, abbinalo a un worker pool di Goroutine o usa 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)
}Distill di una pagina
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)Suggerimenti
- Riutilizza un singolo
*http.Clienttra le Goroutine — è sicuro e fa pooling delle connessioni - Chiudi sempre i body delle risposte (
defer resp.Body.Close()) o farai leak di FD - Per più di 10 URL, preferisci
/batch/distill— vedi Ciclo di vita dei batch job
Un SDK Go ufficiale è in sviluppo — torna presto a controllare.