SDKs

Kotlin

Android와 JVM을 위한 Kotlin의 관용적인 패턴

JVM/Android 에서는 OkHttp + kotlinx.serialization 을, Coroutine 우선 스타일을 원한다면 Ktor 를 사용하세요. 아래는 OkHttp 예시입니다.

Gradle

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

설정

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

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

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

Android UI 작업에서는 withContext(Dispatchers.IO) 로 감싸거나 Ktor 의 suspend 클라이언트를 사용하세요. Batch 작업의 경우 제출 후 Webhook 핸들러에서 처리를 기다리세요 —— Webhooks 를 참고하세요.

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