SDKs

Swift

Idiomatische Swift-Patterns für iOS- und macOS-Apps

Verwende den eingebauten URLSession + Codable. Durchgehend async/await. Targets iOS 15+, macOS 12+.

Konfigurieren

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
}

Eine Seite distillen

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)

Strukturierte Daten extrahieren

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).data

Batch + Webhook

Reiche ein und lass deinen serverseitigen Webhook-Handler die Schwerarbeit erledigen — die meisten iOS-Apps sollten nicht pollen. Siehe Webhooks.

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