Search in sources :

Example 26 with IamAuthenticator

use of com.ibm.cloud.sdk.core.security.IamAuthenticator in project cloudant-java-sdk by IBM.

the class SdkTimeoutTest method testIAMAuthTimeout.

@Test
void testIAMAuthTimeout() throws NoSuchFieldException, IllegalAccessException {
    IamAuthenticator iamAuth = new IamAuthenticator.Builder().apikey("apikey").url("http://" + mockedServer.getHostName() + ":" + mockedServer.getPort()).build();
    CloudantBaseService myService = createCloudantBaseService(iamAuth, "https://cloudant.example");
    Request testRequest = createTestRequest(myService);
    for (int i = 0; i < 2; i++) {
        setupIamAuthResponse();
        if (i == 1) {
            setCustomTimeout(myService, testCases[i]);
        }
        RealCall testRequestCall = createServiceCall(myService, testRequest);
        assertEquals(TimeUnit.MILLISECONDS.toSeconds(testRequestCall.getClient().readTimeoutMillis()), testCases[i]);
    }
}
Also used : RealCall(okhttp3.internal.connection.RealCall) IamAuthenticator(com.ibm.cloud.sdk.core.security.IamAuthenticator) Request(okhttp3.Request) Test(org.testng.annotations.Test)

Example 27 with IamAuthenticator

use of com.ibm.cloud.sdk.core.security.IamAuthenticator in project nlp4j by oyahiroki.

the class WD2DocumentImporter method importDocument.

@Override
public void importDocument(Document doc) throws IOException {
    String fileName;
    if (doc.getAttribute("filename") != null) {
        fileName = doc.getAttributeAsString("filename");
    } else {
        fileName = "hello" + System.currentTimeMillis() + ".json";
    }
    JsonObject jsonObj = DocumentUtil.toJsonObject(doc);
    jsonObj.remove("keywords");
    {
        IamAuthenticator authenticator = new IamAuthenticator(this.DISCOVERY_APIKEY);
        Discovery v2Discovery = new Discovery("2020-08-30", authenticator);
        v2Discovery.setServiceUrl(this.DISCOVERY_URL);
        AddDocumentOptions options = // 
        new AddDocumentOptions.Builder().projectId(// 
        this.projectId).collectionId(// 
        this.collectionId).file(// 
        new ByteArrayInputStream(jsonObj.toString().getBytes(StandardCharsets.UTF_8))).filename(// 
        fileName).fileContentType(// 
        "application/json").build();
        DocumentAccepted response = v2Discovery.addDocument(options).execute().getResult();
        System.err.println(response.getStatus());
        System.err.println(response);
    }
}
Also used : DocumentAccepted(com.ibm.watson.discovery.v2.model.DocumentAccepted) AddDocumentOptions(com.ibm.watson.discovery.v2.model.AddDocumentOptions) ByteArrayInputStream(java.io.ByteArrayInputStream) IamAuthenticator(com.ibm.cloud.sdk.core.security.IamAuthenticator) Discovery(com.ibm.watson.discovery.v2.Discovery) JsonObject(com.google.gson.JsonObject)

Example 28 with IamAuthenticator

use of com.ibm.cloud.sdk.core.security.IamAuthenticator in project java-sdk by watson-developer-cloud.

the class AssistantToneAnalyzerIntegrationExample method main.

public static void main(String[] args) throws Exception {
    // instantiate the assistant service
    Authenticator assistantAuthenticator = new IamAuthenticator("<iam_api_key>");
    final Assistant assistantService = new Assistant("2019-02-28", assistantAuthenticator);
    // instantiate the tone analyzer service
    Authenticator toneAuthenticator = new IamAuthenticator("<iam_api_key>");
    ToneAnalyzer toneService = new ToneAnalyzer("2017-09-21", toneAuthenticator);
    // workspace id
    final String workspaceId = "<workspace-id>";
    // maintain history in the context variable - will add a history variable to
    // each of the emotion, social
    // and language tones
    final boolean maintainHistory = false;
    /**
     * Input for the Assistant service: text (String): an input string (the user's conversation
     * turn) and context (Context): any context that needs to be maintained - either added by the
     * client app or passed in the response from the Assistant service on the previous conversation
     * turn.
     */
    final String text = "I am happy";
    final Context context = new Context();
    // UPDATE CONTEXT HERE IF CONTINUING AN ONGOING CONVERSATION
    // set local context variable to the context from the last response from the
    // Assistant Service
    // (see the getContext() method of the MessageResponse class in
    // com.ibm.watson.assistant.v1.model)
    // async call to Tone Analyzer
    ToneOptions toneOptions = new ToneOptions.Builder().text(text).build();
    toneService.tone(toneOptions).enqueue(new ServiceCallback<ToneAnalysis>() {

        @Override
        public void onResponse(Response<ToneAnalysis> toneResponsePayload) {
            // update context with the tone data returned by the Tone Analyzer
            context.setSystem(ToneDetection.updateUserTone(context, toneResponsePayload.getResult(), maintainHistory));
            // create input for message
            MessageInput input = new MessageInput();
            input.setText(text);
            // call Assistant Service with the input and tone-aware context
            MessageOptions messageOptions = new MessageOptions.Builder(workspaceId).input(input).context(context).build();
            assistantService.message(messageOptions).enqueue(new ServiceCallback<MessageResponse>() {

                @Override
                public void onResponse(Response<MessageResponse> response) {
                    System.out.println(response.getResult());
                }

                @Override
                public void onFailure(Exception e) {
                }
            });
        }

        @Override
        public void onFailure(Exception e) {
        }
    });
}
Also used : Context(com.ibm.watson.assistant.v1.model.Context) ToneAnalysis(com.ibm.watson.tone_analyzer.v3.model.ToneAnalysis) IamAuthenticator(com.ibm.cloud.sdk.core.security.IamAuthenticator) MessageInput(com.ibm.watson.assistant.v1.model.MessageInput) ToneOptions(com.ibm.watson.tone_analyzer.v3.model.ToneOptions) MessageResponse(com.ibm.watson.assistant.v1.model.MessageResponse) Response(com.ibm.cloud.sdk.core.http.Response) ServiceCallback(com.ibm.cloud.sdk.core.http.ServiceCallback) ToneAnalyzer(com.ibm.watson.tone_analyzer.v3.ToneAnalyzer) MessageOptions(com.ibm.watson.assistant.v1.model.MessageOptions) Assistant(com.ibm.watson.assistant.v1.Assistant) Authenticator(com.ibm.cloud.sdk.core.security.Authenticator) IamAuthenticator(com.ibm.cloud.sdk.core.security.IamAuthenticator)

Example 29 with IamAuthenticator

use of com.ibm.cloud.sdk.core.security.IamAuthenticator in project java-sdk by watson-developer-cloud.

the class NaturalLanguageClassifierExample method main.

public static void main(String[] args) {
    Authenticator authenticator = new IamAuthenticator("<iam_api_key>");
    NaturalLanguageClassifier service = new NaturalLanguageClassifier(authenticator);
    ClassifyOptions classifyOptions = new ClassifyOptions.Builder().classifierId("<classifierId>").text("Is it sunny?").build();
    Classification classification = service.classify(classifyOptions).execute().getResult();
    System.out.println(classification);
}
Also used : IamAuthenticator(com.ibm.cloud.sdk.core.security.IamAuthenticator) ClassifyOptions(com.ibm.watson.natural_language_classifier.v1.model.ClassifyOptions) Classification(com.ibm.watson.natural_language_classifier.v1.model.Classification) Authenticator(com.ibm.cloud.sdk.core.security.Authenticator) IamAuthenticator(com.ibm.cloud.sdk.core.security.IamAuthenticator)

Example 30 with IamAuthenticator

use of com.ibm.cloud.sdk.core.security.IamAuthenticator in project java-sdk by watson-developer-cloud.

the class ToneAnalyzerExample method main.

public static void main(String[] args) {
    Authenticator authenticator = new IamAuthenticator("<iam_api_key>");
    ToneAnalyzer service = new ToneAnalyzer("2017-09-21", authenticator);
    String text = "I know the times are difficult! Our sales have been " + "disappointing for the past three quarters for our data analytics " + "product suite. We have a competitive data analytics product " + "suite in the industry. But we need to do our job selling it! " + "We need to acknowledge and fix our sales challenges. " + "We can’t blame the economy for our lack of execution! " + "We are missing critical sales opportunities. " + "Our product is in no way inferior to the competitor products. " + "Our clients are hungry for analytical tools to improve their " + "business outcomes. Economy has nothing to do with it.";
    // Call the service and get the tone
    ToneOptions toneOptions = new ToneOptions.Builder().text(text).build();
    ToneAnalysis tone = service.tone(toneOptions).execute().getResult();
    System.out.println(tone);
}
Also used : ToneAnalysis(com.ibm.watson.tone_analyzer.v3.model.ToneAnalysis) IamAuthenticator(com.ibm.cloud.sdk.core.security.IamAuthenticator) ToneOptions(com.ibm.watson.tone_analyzer.v3.model.ToneOptions) Authenticator(com.ibm.cloud.sdk.core.security.Authenticator) IamAuthenticator(com.ibm.cloud.sdk.core.security.IamAuthenticator)

Aggregations

IamAuthenticator (com.ibm.cloud.sdk.core.security.IamAuthenticator)32 Authenticator (com.ibm.cloud.sdk.core.security.Authenticator)30 Before (org.junit.Before)15 SpeechRecognitionResults (com.ibm.watson.speech_to_text.v1.model.SpeechRecognitionResults)5 File (java.io.File)4 Response (com.ibm.cloud.sdk.core.http.Response)2 MessageInput (com.ibm.watson.assistant.v1.model.MessageInput)2 MessageOptions (com.ibm.watson.assistant.v1.model.MessageOptions)2 MessageResponse (com.ibm.watson.assistant.v1.model.MessageResponse)2 TranslateOptions (com.ibm.watson.language_translator.v3.model.TranslateOptions)2 TranslationResult (com.ibm.watson.language_translator.v3.model.TranslationResult)2 RecognizeWithWebsocketsOptions (com.ibm.watson.speech_to_text.v1.model.RecognizeWithWebsocketsOptions)2 BaseRecognizeCallback (com.ibm.watson.speech_to_text.v1.websocket.BaseRecognizeCallback)2 ToneAnalysis (com.ibm.watson.tone_analyzer.v3.model.ToneAnalysis)2 ToneOptions (com.ibm.watson.tone_analyzer.v3.model.ToneOptions)2 ByteArrayInputStream (java.io.ByteArrayInputStream)2 InputStream (java.io.InputStream)2 JsonObject (com.google.gson.JsonObject)1 HttpConfigOptions (com.ibm.cloud.sdk.core.http.HttpConfigOptions)1 ServiceCallback (com.ibm.cloud.sdk.core.http.ServiceCallback)1