SDKs
Go
Thunderbit Open API를 위한 Go의 관용적인 패턴
net/http + encoding/json 을 사용하세요. SDK 가 필요 없습니다. 높은 동시성을 원한다면 Goroutine 워커 풀과 짝지어 사용하거나 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
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)팁
- 단일
*http.Client을 Goroutine 간에 재사용하세요 —— 안전하고 connection pooling 이 동작합니다 - 응답 body 를 항상 닫으세요(
defer resp.Body.Close()) —— 그렇지 않으면 FD 가 누수됩니다 - URL 이 10 개 이상이라면
/batch/distill을 사용하세요 —— Batch Job Lifecycle 을 참고하세요
공식 Go SDK 가 개발 중입니다 —— 곧 다시 확인해 주세요.