Java
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 Java com.getlingara:lingara-java e li ser Maven Central. Ew Java 17 an nûtir hewce dike û java.net.http.HttpClient-a JDK-yê bi xwe bi kar tîne, bi Jackson ji bo JSON.
Çawa tê bikaranîn
- Bang hevdem in. Ji bo gelek bangan bi hev re, wan li ser virtual thread-an an executor-ekî bixebitîne.
- Herikînek
EventStreame, ku hemIterableû hemAutoCloseablee: wê bi xelekekefordi hundirê try-with-resources de bixwîne. Bûyer record in di bin sealed interface-ekê de, ji ber vê yekêswitch-ek li ser wan temam e. - Ji bo betalkirinê, thread-ê qut bike an ji thread-eke din
close()bang bike. Wê demê bangCancellationExceptiondavêje, ku ne yek ji xeletiyên pirtûkxaneyê ye. - Xeletî
LingaraExceptiondirêj dikin, unchecked exception-ek.retryAfter()Optional<Duration>vedigerîne. - Xerîdarekî bi
LingaraClient.builder()ava bike. Bangeke ku naherike heta ku turequestTimeoutsaz nekî tu sînorê demê tune.
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.
Java
<dependency>
<groupId>com.getlingara</groupId>
<artifactId>lingara-java</artifactId>
<version>0.1.0-alpha.6</version>
</dependency>Java
import com.getlingara.client.LingaraClient;
LingaraClient client =
LingaraClient.builder()
.clientCredentials(
System.getenv("LINGARA_CLIENT_ID"), System.getenv("LINGARA_CLIENT_SECRET"))
.build();Java
import com.getlingara.client.model.GenerateVocabularyEvent;
import com.getlingara.client.model.VocabRequest;
VocabRequest request = new VocabRequest().level(2).sourceLang("en").targetLang("zh").count(8);
try (EventStream<GenerateVocabularyEvent> stream = client.generateVocabulary(request)) {
for (GenerateVocabularyEvent event : stream) {
if (event instanceof GenerateVocabularyEvent.Item item) {
System.out.println(item.data().getWord() + " " + item.data().getTranslation());
}
}
}Java
try {
client.getUsage();
} catch (LingaraException e) {
if (e instanceof ApiException api) {
// A refusal from the API: status(), code() (stable) and getMessage() (localised).
System.out.println(api.status() + " " + api.code() + " " + api.getMessage());
api.retryAfter().ifPresent(wait -> System.out.println("retry after " + wait));
} else if (e instanceof OAuthException oauth) {
// The token endpoint refused the credentials or the scopes.
System.out.println(oauth.status() + " " + oauth.error() + " " + oauth.description());
} else if (e instanceof MaintenanceException maintenance) {
System.out.println("under maintenance");
maintenance.retryAfter().ifPresent(wait -> System.out.println("retry after " + wait));
} else if (e instanceof TransportException transport) {
// No usable answer: CONNECT, TLS, RESET, TIMEOUT, and so on.
System.out.println("transport: " + transport.kind());
}
}
// An interrupted call throws java.util.concurrent.CancellationException instead.