SDKs
Swift
iOS / macOS アプリのための Swift イディオムなパターン
組み込みの URLSession + Codable を使います。全体を通じて async/await。iOS 15+ / macOS 12+ がターゲットです。
Configure
import Foundation
let api = URL(string: "https://openapi.thunderbit.com/openapi/v1")!
let apiKey = ProcessInfo.processInfo.environment["THUNDERBIT_API_KEY"]!
func request(path: String, body: Data? = nil) -> URLRequest {
var req = URLRequest(url: api.appendingPathComponent(path))
req.httpMethod = body == nil ? "GET" : "POST"
req.httpBody = body
req.timeoutInterval = 60
req.setValue("Bearer \(apiKey)", forHTTPHeaderField: "Authorization")
req.setValue("application/json", forHTTPHeaderField: "Content-Type")
return req
}ページを Distill する
struct DistillBody: Encodable { let url: String }
struct DistillResponse: Decodable {
struct Data: Decodable { let markdown: String }
let data: Data
}
let body = try JSONEncoder().encode(DistillBody(url: "https://thunderbit.com/playground"))
let (data, _) = try await URLSession.shared.data(for: request(path: "distill", body: body))
let result = try JSONDecoder().decode(DistillResponse.self, from: data)
print(result.data.markdown)構造化データを Extract する
struct Product: Decodable { let name: String; let price: Double }
struct ExtractResponse: Decodable { let data: Product }
let payload: [String: Any] = [
"url": "https://example.com/product/iphone-15-pro",
"schema": [
"type": "object",
"properties": [
"name": ["type": "string"],
"price": ["type": "number"]
],
"required": ["name", "price"]
]
]
let body = try JSONSerialization.data(withJSONObject: payload)
let (data, _) = try await URLSession.shared.data(for: request(path: "extract", body: body))
let product = try JSONDecoder().decode(ExtractResponse.self, from: data).dataBatch + Webhook
投入したらサーバー側の Webhook ハンドラに任せましょう —— iOS アプリ側でポーリングすべきではありません。Webhooks を参照。
公式 Swift SDK は開発中です —— もう少しお待ちください。