Lingara Lingara दस्तावेज़ मार्गदर्शिकाएँ API लाइब्रेरी ऐप बनाएं वेब ऐप
भाषा: हिन्दी

Java

यह पेज अंग्रेज़ी से अनुवादित है। अगर दोनों में अंतर हो, तो अंग्रेज़ी पेज सही है। अंग्रेज़ी पेज पढ़ें

Java लाइब्रेरी Maven Central पर com.getlingara:lingara-java है। इसके लिए Java 17 या उसके बाद का संस्करण चाहिए और यह JDK का अपना java.net.http.HttpClient इस्तेमाल करती है, JSON के लिए Jackson के साथ।

इसे कैसे इस्तेमाल करें

  • कॉल सिंक्रोनस होती हैं। एक साथ कई कॉल के लिए, उन्हें virtual threads या किसी executor पर चलाएँ।
  • स्ट्रीम एक EventStream है, जो Iterable भी है और AutoCloseable भी: इसे try-with-resources के भीतर for लूप से पढ़ें। इवेंट एक sealed interface के अंतर्गत records हैं, इसलिए उन पर switch संपूर्ण होता है।
  • रद्द करने के लिए, थ्रेड को इंटरप्ट करें या किसी दूसरे थ्रेड से close() कॉल करें। तब कॉल CancellationException फेंकती है, जो लाइब्रेरी के एरर में से एक नहीं है।
  • एरर LingaraException को एक्सटेंड करते हैं, जो एक unchecked exception है। retryAfter() एक Optional<Duration> लौटाता है।
  • LingaraClient.builder() से क्लाइंट बनाएँ। जो कॉल स्ट्रीम नहीं करती, उसकी तब तक कोई टाइमआउट नहीं होती जब तक आप requestTimeout सेट न करें।

नीचे दिए उदाहरण डिपेंडेंसी जोड़ते हैं, क्लाइंट बनाते हैं, शब्दावली सूची स्ट्रीम करते हैं और एक एरर संभालते हैं।

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.

यह पेज क्या रैप करता है