Lingara Lingara Belge Rêbername API Pirtûkxane Sepan Avakirin Sepana webê
Ziman: Kurdî

Kotlin

Ev rûpel ji Îngilîzî hatiye wergerandin. Heke her du ji hev cuda bin, rûpela Îngilîzî rast e. Rûpela Îngilîzî bixwîne

Pirtûkxaneya Kotlin com.getlingara:lingara-kotlin e li ser Maven Central. Ew li ser JVM dixebite, Java 17 û Kotlin 2.2 an nûtir hewce dike, û kotlinx.coroutines û kotlinx.serialization bi kar tîne.

Çawa tê bikaranîn

  • Her bang fonksiyoneke suspend e. Ji bo betalkirina bangê coroutine-ê betal bike, an ji bo dema dawî withTimeout bi kar bîne; tu CancellationException-a xwe dibînî, qet yeke pêçayî nabînî.
  • Herikînek EventStream e, Flow-ek ku tu carekê kom dikî. Wê di hundirê use de kom bike, da ku dema tu qedandî were girtin. Bûyer data class in di bin sealed interface-ekê de, bi when têne hevberkirin.
  • Xeletî sealed class-a LingaraException dirêj dikin, ji ber vê yekê when-ek li ser wan temam e. retryAfter Duration-ek e, an dema ku API tu sekinandinek nebêje null e.
  • Xerîdarekî bi avakerê LingaraClient { } biafirîne. Ew HttpClient-a JDK-yê bi kar tîne, ku tu dikarî bi ya xwe biguherînî.

Mînakên li jêr girêdayîbûnê zêde dikin, xerîdarekî diafirînin, lîsteyeke ferhengê diherikînin û xeletiyekê birêve dibin.

Kotlin

implementation("com.getlingara:lingara-kotlin:0.1.0-alpha.6")

Kotlin

import com.getlingara.kotlin.LingaraClient

val client =
    LingaraClient {
        clientCredentials(System.getenv("LINGARA_CLIENT_ID"), System.getenv("LINGARA_CLIENT_SECRET"))
    }

Kotlin

import com.getlingara.kotlin.LingaraClient
import com.getlingara.kotlin.model.GenerateVocabularyEvent
import com.getlingara.kotlin.model.VocabRequest

val request = VocabRequest(level = 2, sourceLang = "en", targetLang = "zh", count = 8)
// The request is sent here; collect the stream once, inside use {}.
client.generateVocabulary(request).use { stream ->
    stream.collect { event ->
        when (event) {
            is GenerateVocabularyEvent.Item -> println("${event.data.word} ${event.data.translation}")
            is GenerateVocabularyEvent.Started -> Unit
        }
    }
}

Kotlin

try {
    client.getUsage()
} catch (e: LingaraException) {
    when (e) {
        // A refusal from the API: status, code (stable) and message (localised).
        is ApiException -> println("${e.status} ${e.code} ${e.message}; retry after ${e.retryAfter}")
        // The token endpoint refused the credentials or the scopes.
        is OAuthException -> println("${e.status} ${e.error} ${e.description}")
        is MaintenanceException -> println("under maintenance; retry after ${e.retryAfter}")
        // No usable answer: CONNECT, TLS, RESET, TIMEOUT, and so on.
        is TransportException -> println("transport: ${e.kind}")
    }
}
// A cancelled call throws the caller's own CancellationException instead.

Ev rûpel çi dipêçe