diff --git a/sdk/textanalytics/azure-cs-textanalytics/src/main/java/com/azure/cs/textanalytics/TextAnalyticsAsyncClient.java b/sdk/textanalytics/azure-cs-textanalytics/src/main/java/com/azure/cs/textanalytics/TextAnalyticsAsyncClient.java index 42ff66f1cb40e..7e446fc4794e6 100644 --- a/sdk/textanalytics/azure-cs-textanalytics/src/main/java/com/azure/cs/textanalytics/TextAnalyticsAsyncClient.java +++ b/sdk/textanalytics/azure-cs-textanalytics/src/main/java/com/azure/cs/textanalytics/TextAnalyticsAsyncClient.java @@ -539,4 +539,18 @@ Mono>> analyzeBatchSentim new MultiLanguageBatchInput().setDocuments(document), options.getModelVersion(), options.showStatistics(), context).map(response -> new SimpleResponse<>(response, null)); } + + +// @ServiceMethod(returns = ReturnType.SINGLE) +//// public List test(LanguageBatchInput languageBatchInput) { +//// Mono> result = service.languagesWithRestResponseAsync(languageBatchInput, Context.NONE) +//// .doOnSubscribe(ignoredValue -> logger.info("A batch of language input - {}", languageBatchInput)) +//// .doOnSuccess(response -> logger.info("A batch of detected language output - {}", languageBatchInput)) +//// .doOnError(error -> logger.warning("Failed to detected languages - {}", languageBatchInput)) +//// .map(response -> new SimpleResponse<>(response, response.getValue())); +//// List documentLanguages = result.block().getValue().getDocuments(); +//// +//// return documentLanguages; +//// } + } diff --git a/sdk/textanalytics/azure-cs-textanalytics/src/main/java/com/azure/cs/textanalytics/TextAnalyticsClientBuilder.java b/sdk/textanalytics/azure-cs-textanalytics/src/main/java/com/azure/cs/textanalytics/TextAnalyticsClientBuilder.java index 75084d3355765..9fcbe1123e591 100644 --- a/sdk/textanalytics/azure-cs-textanalytics/src/main/java/com/azure/cs/textanalytics/TextAnalyticsClientBuilder.java +++ b/sdk/textanalytics/azure-cs-textanalytics/src/main/java/com/azure/cs/textanalytics/TextAnalyticsClientBuilder.java @@ -44,6 +44,7 @@ public final class TextAnalyticsClientBuilder { private static final String NAME = "name"; private static final String VERSION = "version"; private static final RetryPolicy DEFAULT_RETRY_POLICY = new RetryPolicy("retry-after-ms", ChronoUnit.MILLIS); + private static final String DEFAULT_SCOPE = "https://cognitiveservices.azure.com/.default"; private final ClientLogger logger = new ClientLogger(TextAnalyticsClientBuilder.class); private final List policies; @@ -99,9 +100,8 @@ public TextAnalyticsAsyncClient buildAsyncClient() { if (tokenCredential != null) { // User token based policy policies.add( - new BearerTokenAuthenticationPolicy(tokenCredential, String.format("%s/.default", buildEndpoint))); + new BearerTokenAuthenticationPolicy(tokenCredential, DEFAULT_SCOPE)); } else if (subscriptionKey != null) { - //TODO: add additional policy for subscription key if it is required. But might be not headers.put(OCP_APIM_SUBSCRIPTION_KEY, subscriptionKey); } else { // Throw exception that credential and tokenCredential cannot be null diff --git a/sdk/textanalytics/azure-cs-textanalytics/src/test/java/com.azure.cs.textanalytics/Demo.java b/sdk/textanalytics/azure-cs-textanalytics/src/test/java/com.azure.cs.textanalytics/Demo.java index 45352c9df4cb2..9916f113233e1 100644 --- a/sdk/textanalytics/azure-cs-textanalytics/src/test/java/com.azure.cs.textanalytics/Demo.java +++ b/sdk/textanalytics/azure-cs-textanalytics/src/test/java/com.azure.cs.textanalytics/Demo.java @@ -8,6 +8,7 @@ import com.azure.core.http.HttpPipelineBuilder; import com.azure.core.http.policy.AddDatePolicy; import com.azure.core.http.policy.AddHeadersPolicy; +import com.azure.core.http.policy.BearerTokenAuthenticationPolicy; import com.azure.core.http.policy.HttpPipelinePolicy; import com.azure.core.http.policy.RequestIdPolicy; import com.azure.core.http.rest.SimpleResponse; @@ -38,7 +39,9 @@ public class Demo { @Test public void test() { - String endpoint = "https://shawnjavatextanalytics.cognitiveservices.azure.com/"; +// String endpoint = "https://shawnjavatextanalytics.cognitiveservices.azure.com/"; + String endpoint = "https://javatextanalyticstestresources.cognitiveservices.azure.com/"; + String defaultScope = "https://cognitiveservices.azure.com/.default"; subscriptionKey = Configuration.getGlobalConfiguration().get(AZURE_TEXT_ANALYTICS_SUBSCRIPTION_KEY); System.out.println("Subscription Key = " + subscriptionKey); @@ -55,7 +58,7 @@ public void test() { policies.add(new AddDatePolicy()); policies.add( - new BearerTokenAuthenticationPolicy(tokenCredential, String.format("%s/.default", endpoint))); + new BearerTokenAuthenticationPolicy(tokenCredential, defaultScope)); // customized pipeline HttpPipeline pipeline = new HttpPipelineBuilder() @@ -67,7 +70,7 @@ public void test() { .pipeline(pipeline) .build(); - System.out.println("Endpoint = " + ta.getEndpoint()); + System.out.println("Endpoint22 = " + ta.getEndpoint()); final List inputs = Arrays.asList( new DetectLanguageInput("1", "This is written in English","US"), @@ -89,4 +92,42 @@ public void test() { detectedLanguage.getName(), detectedLanguage.getIso6391Name(), detectedLanguage.getScore())); } } + + +// @Test +// public void testBuilder() { +// +// subscriptionKey = Configuration.getGlobalConfiguration().get(AZURE_TEXT_ANALYTICS_SUBSCRIPTION_KEY); +// System.out.println("Subscription Key = " + subscriptionKey); +// +// TextAnalyticsClientBuilder clientBuilder = new TextAnalyticsClientBuilder(); +// +// +// +// TextAnalyticsAsyncClient client = clientBuilder +//// .endpoint("https://shawnjavatextanalytics.cognitiveservices.azure.com/") +// .endpoint("https://javatextanalyticstestresources.cognitiveservices.azure.com/") +// .subscriptionKey(subscriptionKey) +// .buildAsyncClient(); +// +// final List inputs = Arrays.asList( +// new DetectLanguageInput("1", "This is written in English","US"), +// new DetectLanguageInput("2", "Este es un document escrito en EspaƱol.", "es") +// ); +// final LanguageBatchInput languageBatchInput = new LanguageBatchInput().setDocuments(inputs); +// +// +// +// final List documentLanguages = client.test(languageBatchInput); +// for (DocumentLanguage documentLanguage : documentLanguages) { +// System.out.println("document language = " + documentLanguage.getId()); +// +// documentLanguage.getDetectedLanguages().forEach(detectedLanguage -> +// System.out.printf("detected language, name = %s, iso name = %s, score = %s.", +// detectedLanguage.getName(), detectedLanguage.getIso6391Name(), detectedLanguage.getScore())); +// } +// +// +// +// } }