Skip to content

Commit

Permalink
AAD and subscription key authentication both works
Browse files Browse the repository at this point in the history
  • Loading branch information
mssfang committed Dec 6, 2019
1 parent 9cceb44 commit 150b623
Show file tree
Hide file tree
Showing 3 changed files with 60 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -539,4 +539,18 @@ Mono<Response<DocumentResultCollection<TextSentimentResult>>> analyzeBatchSentim
new MultiLanguageBatchInput().setDocuments(document), options.getModelVersion(), options.showStatistics(),
context).map(response -> new SimpleResponse<>(response, null));
}


// @ServiceMethod(returns = ReturnType.SINGLE)
//// public List<DocumentLanguage> test(LanguageBatchInput languageBatchInput) {
//// Mono<SimpleResponse<LanguageResult>> 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<DocumentLanguage> documentLanguages = result.block().getValue().getDocuments();
////
//// return documentLanguages;
//// }

}
Original file line number Diff line number Diff line change
Expand Up @@ -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<HttpPipelinePolicy> policies;
Expand Down Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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);
Expand All @@ -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()
Expand All @@ -67,7 +70,7 @@ public void test() {
.pipeline(pipeline)
.build();

System.out.println("Endpoint = " + ta.getEndpoint());
System.out.println("Endpoint22 = " + ta.getEndpoint());

final List<DetectLanguageInput> inputs = Arrays.asList(
new DetectLanguageInput("1", "This is written in English","US"),
Expand All @@ -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<DetectLanguageInput> 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<DocumentLanguage> 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()));
// }
//
//
//
// }
}

0 comments on commit 150b623

Please sign in to comment.