SDKs

Kotlin

Padrões idiomáticos de Kotlin para Android e JVM

Use OkHttp + kotlinx.serialization para JVM/Android, ou Ktor para o estilo coroutine-first. OkHttp mostrado abaixo.

Gradle

dependencies {
    implementation("com.squareup.okhttp3:okhttp:4.12.0")
    implementation("org.jetbrains.kotlinx:kotlinx-serialization-json:1.7.3")
}

Configurar

import okhttp3.*
import kotlinx.serialization.json.Json

private const val API = "https://openapi.thunderbit.com/openapi/v1"
private val JSON_MEDIA = "application/json".toMediaType()
private val client = OkHttpClient()

private fun request(path: String) = Request.Builder()
    .url("$API$path")
    .header("Authorization", "Bearer ${System.getenv("THUNDERBIT_API_KEY")}")

Distill de uma página

val body = """{"url": "https://thunderbit.com/playground"}"""
    .toRequestBody(JSON_MEDIA)

client.newCall(request("/distill").post(body).build()).execute().use { res ->
    if (!res.isSuccessful) error("HTTP ${'$'}{res.code}")
    println(res.body!!.string())
}

Extract de dados estruturados

val body = """
{
  "url": "https://example.com/product/iphone-15-pro",
  "schema": {
    "type": "object",
    "properties": {
      "name":  { "type": "string" },
      "price": { "type": "number" }
    },
    "required": ["name", "price"]
  }
}
""".trimIndent().toRequestBody(JSON_MEDIA)

val res = client.newCall(request("/extract").post(body).build()).execute()

Coroutine + Batch

Para trabalho de UI Android, envolva em withContext(Dispatchers.IO) ou use o cliente suspend do Ktor. Para batch jobs, submeta e depois aguarde em um handler de Webhook — veja Webhooks.

Um SDK Kotlin oficial está em desenvolvimento — volte em breve.