SDKs

Rust

Thunderbit Open API를 위한 Rust의 관용적인 패턴

reqwest + serde + tokio 를 사용하세요. 기본적으로 비동기이며, 분산 처리를 위해 futures::stream::iter 와 함께 사용할 수 있습니다.

Cargo.toml

[dependencies]
reqwest = { version = "0.12", features = ["json"] }
serde = { version = "1", features = ["derive"] }
serde_json = "1"
tokio = { version = "1", features = ["full"] }

설정

use reqwest::Client;
use serde_json::json;

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

fn client() -> Client {
    Client::builder()
        .timeout(std::time::Duration::from_secs(60))
        .build()
        .unwrap()
}

fn auth() -> String {
    format!("Bearer {}", std::env::var("THUNDERBIT_API_KEY").unwrap())
}

페이지 Distill

#[tokio::main]
async fn main() -> Result<(), Box<dyn std::error::Error>> {
    let res: serde_json::Value = client()
        .post(format!("{API}/distill"))
        .header("Authorization", auth())
        .json(&json!({ "url": "https://thunderbit.com/playground" }))
        .send()
        .await?
        .error_for_status()?
        .json()
        .await?;
    println!("{}", res["data"]["markdown"]);
    Ok(())
}

구조화된 데이터 Extract

let res: serde_json::Value = client()
    .post(format!("{API}/extract"))
    .header("Authorization", auth())
    .json(&json!({
        "url": "https://example.com/product/iphone-15-pro",
        "schema": {
            "type": "object",
            "properties": {
                "name":  { "type": "string" },
                "price": { "type": "number" }
            },
            "required": ["name", "price"]
        }
    }))
    .send().await?.error_for_status()?.json().await?;

Batch 분산

batch 엔드포인트 대신 단일 URL distill 의 고동시성 처리가 필요하다면, 경계가 있는 JoinSet 을 사용하세요.

use tokio::task::JoinSet;

let mut set = JoinSet::new();
for url in urls {
    let c = client();
    set.spawn(async move {
        c.post(format!("{API}/distill"))
            .header("Authorization", auth())
            .json(&json!({ "url": url }))
            .send().await?.error_for_status()?
            .json::<serde_json::Value>().await
    });
}
while let Some(res) = set.join_next().await { /* … */ }

URL 이 10 개 이상이라면 분산 처리 대신 /batch/distill 을 사용하세요 —— Batch Job Lifecycle 을 참고하세요.

공식 Rust SDK 가 개발 중입니다 —— 곧 다시 확인해 주세요.