Skip to content

Commit 984b4df

Browse files
author
Changjian Wang
committed
Update Content Understanding Samples to Use DefaultAzureCredential for Authentication
1 parent 92711c7 commit 984b4df

File tree

5 files changed

+87
-32
lines changed

5 files changed

+87
-32
lines changed

sdk/contentunderstanding/azure-ai-contentunderstanding/src/test/java/com/azure/ai/contentunderstanding/generated/Sample12_GetResultFile.java

Lines changed: 17 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
import com.azure.core.http.rest.Response;
1616
import com.azure.core.util.BinaryData;
1717
import com.azure.core.util.polling.SyncPoller;
18+
import com.azure.identity.DefaultAzureCredentialBuilder;
1819
import org.junit.jupiter.api.Test;
1920

2021
import java.io.IOException;
@@ -42,9 +43,14 @@ public void testGetResultFile() throws IOException {
4243
String endpoint = System.getenv("CONTENTUNDERSTANDING_ENDPOINT");
4344
String key = System.getenv("AZURE_CONTENT_UNDERSTANDING_KEY");
4445

45-
client = new ContentUnderstandingClientBuilder().endpoint(endpoint)
46-
.credential(new AzureKeyCredential(key))
47-
.buildClient();
46+
ContentUnderstandingClientBuilder builder = new ContentUnderstandingClientBuilder().endpoint(endpoint);
47+
48+
// Use DefaultAzureCredential if key is not provided, otherwise use AzureKeyCredential
49+
if (key == null || key.trim().isEmpty()) {
50+
client = builder.credential(new DefaultAzureCredentialBuilder().build()).buildClient();
51+
} else {
52+
client = builder.credential(new AzureKeyCredential(key)).buildClient();
53+
}
4854

4955
// BEGIN: com.azure.ai.contentunderstanding.getResultFile
5056
// For video analysis, use a video URL to get keyframes
@@ -249,9 +255,14 @@ public void testGetResultFileAsync() throws IOException {
249255
String endpoint = System.getenv("CONTENTUNDERSTANDING_ENDPOINT");
250256
String key = System.getenv("AZURE_CONTENT_UNDERSTANDING_KEY");
251257

252-
asyncClient = new ContentUnderstandingClientBuilder().endpoint(endpoint)
253-
.credential(new AzureKeyCredential(key))
254-
.buildAsyncClient();
258+
ContentUnderstandingClientBuilder builder = new ContentUnderstandingClientBuilder().endpoint(endpoint);
259+
260+
// Use DefaultAzureCredential if key is not provided, otherwise use AzureKeyCredential
261+
if (key == null || key.trim().isEmpty()) {
262+
asyncClient = builder.credential(new DefaultAzureCredentialBuilder().build()).buildAsyncClient();
263+
} else {
264+
asyncClient = builder.credential(new AzureKeyCredential(key)).buildAsyncClient();
265+
}
255266

256267
// For video analysis
257268
String videoUrl

sdk/contentunderstanding/azure-ai-contentunderstanding/src/test/java/com/azure/ai/contentunderstanding/generated/Sample13_DeleteResult.java

Lines changed: 17 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
import com.azure.core.exception.HttpResponseException;
1616
import com.azure.core.http.rest.Response;
1717
import com.azure.core.util.polling.SyncPoller;
18+
import com.azure.identity.DefaultAzureCredentialBuilder;
1819
import org.junit.jupiter.api.Test;
1920

2021
import java.util.Collections;
@@ -38,9 +39,14 @@ public void testDeleteResult() {
3839
String endpoint = System.getenv("CONTENTUNDERSTANDING_ENDPOINT");
3940
String key = System.getenv("AZURE_CONTENT_UNDERSTANDING_KEY");
4041

41-
client = new ContentUnderstandingClientBuilder().endpoint(endpoint)
42-
.credential(new AzureKeyCredential(key))
43-
.buildClient();
42+
ContentUnderstandingClientBuilder builder = new ContentUnderstandingClientBuilder().endpoint(endpoint);
43+
44+
// Use DefaultAzureCredential if key is not provided, otherwise use AzureKeyCredential
45+
if (key == null || key.trim().isEmpty()) {
46+
client = builder.credential(new DefaultAzureCredentialBuilder().build()).buildClient();
47+
} else {
48+
client = builder.credential(new AzureKeyCredential(key)).buildClient();
49+
}
4450

4551
// BEGIN: com.azure.ai.contentunderstanding.deleteResult
4652
// Step 1: Analyze a document
@@ -127,9 +133,14 @@ public void testDeleteResultAsync() {
127133
String endpoint = System.getenv("CONTENTUNDERSTANDING_ENDPOINT");
128134
String key = System.getenv("AZURE_CONTENT_UNDERSTANDING_KEY");
129135

130-
asyncClient = new ContentUnderstandingClientBuilder().endpoint(endpoint)
131-
.credential(new AzureKeyCredential(key))
132-
.buildAsyncClient();
136+
ContentUnderstandingClientBuilder builder = new ContentUnderstandingClientBuilder().endpoint(endpoint);
137+
138+
// Use DefaultAzureCredential if key is not provided, otherwise use AzureKeyCredential
139+
if (key == null || key.trim().isEmpty()) {
140+
asyncClient = builder.credential(new DefaultAzureCredentialBuilder().build()).buildAsyncClient();
141+
} else {
142+
asyncClient = builder.credential(new AzureKeyCredential(key)).buildAsyncClient();
143+
}
133144

134145
// Analyze document
135146
String documentUrl

sdk/contentunderstanding/azure-ai-contentunderstanding/src/test/java/com/azure/ai/contentunderstanding/generated/Sample14_CopyAnalyzer.java

Lines changed: 17 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
import com.azure.core.credential.AzureKeyCredential;
1717
import com.azure.core.http.rest.Response;
1818
import com.azure.core.util.polling.SyncPoller;
19+
import com.azure.identity.DefaultAzureCredentialBuilder;
1920
import org.junit.jupiter.api.Test;
2021

2122
import java.util.HashMap;
@@ -41,9 +42,14 @@ public void testCopyAnalyzer() {
4142
String endpoint = System.getenv("CONTENTUNDERSTANDING_ENDPOINT");
4243
String key = System.getenv("AZURE_CONTENT_UNDERSTANDING_KEY");
4344

44-
client = new ContentUnderstandingClientBuilder().endpoint(endpoint)
45-
.credential(new AzureKeyCredential(key))
46-
.buildClient();
45+
ContentUnderstandingClientBuilder builder = new ContentUnderstandingClientBuilder().endpoint(endpoint);
46+
47+
// Use DefaultAzureCredential if key is not provided, otherwise use AzureKeyCredential
48+
if (key == null || key.trim().isEmpty()) {
49+
client = builder.credential(new DefaultAzureCredentialBuilder().build()).buildClient();
50+
} else {
51+
client = builder.credential(new AzureKeyCredential(key)).buildClient();
52+
}
4753

4854
// Generate unique analyzer IDs
4955
String sourceAnalyzerId = "test_analyzer_source_" + UUID.randomUUID().toString().replace("-", "");
@@ -212,9 +218,14 @@ public void testCopyAnalyzerAsync() {
212218
String endpoint = System.getenv("CONTENTUNDERSTANDING_ENDPOINT");
213219
String key = System.getenv("AZURE_CONTENT_UNDERSTANDING_KEY");
214220

215-
asyncClient = new ContentUnderstandingClientBuilder().endpoint(endpoint)
216-
.credential(new AzureKeyCredential(key))
217-
.buildAsyncClient();
221+
ContentUnderstandingClientBuilder builder = new ContentUnderstandingClientBuilder().endpoint(endpoint);
222+
223+
// Use DefaultAzureCredential if key is not provided, otherwise use AzureKeyCredential
224+
if (key == null || key.trim().isEmpty()) {
225+
asyncClient = builder.credential(new DefaultAzureCredentialBuilder().build()).buildAsyncClient();
226+
} else {
227+
asyncClient = builder.credential(new AzureKeyCredential(key)).buildAsyncClient();
228+
}
218229

219230
String sourceAnalyzerId = "test_analyzer_source_async_" + UUID.randomUUID().toString().replace("-", "");
220231
String targetAnalyzerId = "test_analyzer_target_async_" + UUID.randomUUID().toString().replace("-", "");

sdk/contentunderstanding/azure-ai-contentunderstanding/src/test/java/com/azure/ai/contentunderstanding/generated/Sample15_GrantCopyAuth.java

Lines changed: 26 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
import com.azure.core.credential.AzureKeyCredential;
1616
import com.azure.core.http.rest.RequestOptions;
1717
import com.azure.core.util.BinaryData;
18+
import com.azure.identity.DefaultAzureCredentialBuilder;
1819
import com.azure.core.util.polling.SyncPoller;
1920
import org.junit.jupiter.api.Test;
2021

@@ -53,9 +54,14 @@ public void testGrantCopyAuthAsync() {
5354
String endpoint = System.getenv("CONTENTUNDERSTANDING_ENDPOINT");
5455
String key = System.getenv("AZURE_CONTENT_UNDERSTANDING_KEY");
5556

56-
sourceClient = new ContentUnderstandingClientBuilder().endpoint(endpoint)
57-
.credential(new AzureKeyCredential(key))
58-
.buildClient();
57+
ContentUnderstandingClientBuilder builder = new ContentUnderstandingClientBuilder().endpoint(endpoint);
58+
59+
// Use DefaultAzureCredential if key is not provided, otherwise use AzureKeyCredential
60+
if (key == null || key.trim().isEmpty()) {
61+
sourceClient = builder.credential(new DefaultAzureCredentialBuilder().build()).buildClient();
62+
} else {
63+
sourceClient = builder.credential(new AzureKeyCredential(key)).buildClient();
64+
}
5965

6066
String sourceAnalyzerId = "test_grant_copy_source_" + UUID.randomUUID().toString().replace("-", "");
6167

@@ -207,18 +213,28 @@ public void testCrossResourceCopy() {
207213
|| targetRegion == null) {
208214
System.out.println("⚠️ Cross-resource copying requires environment variables:");
209215
System.out.println(" SOURCE_RESOURCE_ID, SOURCE_REGION");
210-
System.out.println(" TARGET_ENDPOINT, TARGET_RESOURCE_ID, TARGET_REGION, TARGET_KEY (optional)");
216+
System.out.println(" TARGET_ENDPOINT, TARGET_RESOURCE_ID, TARGET_REGION");
217+
System.out.println(" TARGET_KEY (optional, uses DefaultAzureCredential if not provided)");
211218
System.out.println(" Skipping cross-resource copy test.");
212219
return;
213220
}
214221

215-
sourceClient = new ContentUnderstandingClientBuilder().endpoint(endpoint)
216-
.credential(new AzureKeyCredential(key))
217-
.buildClient();
222+
// Source client: Use DefaultAzureCredential if key is not provided
223+
ContentUnderstandingClientBuilder sourceBuilder = new ContentUnderstandingClientBuilder().endpoint(endpoint);
224+
if (key == null || key.trim().isEmpty()) {
225+
sourceClient = sourceBuilder.credential(new DefaultAzureCredentialBuilder().build()).buildClient();
226+
} else {
227+
sourceClient = sourceBuilder.credential(new AzureKeyCredential(key)).buildClient();
228+
}
218229

219-
targetClient = new ContentUnderstandingClientBuilder().endpoint(targetEndpoint)
220-
.credential(new AzureKeyCredential(targetKey != null ? targetKey : key))
221-
.buildClient();
230+
// Target client: Use DefaultAzureCredential if targetKey is not provided
231+
ContentUnderstandingClientBuilder targetBuilder
232+
= new ContentUnderstandingClientBuilder().endpoint(targetEndpoint);
233+
if (targetKey == null || targetKey.trim().isEmpty()) {
234+
targetClient = targetBuilder.credential(new DefaultAzureCredentialBuilder().build()).buildClient();
235+
} else {
236+
targetClient = targetBuilder.credential(new AzureKeyCredential(targetKey)).buildClient();
237+
}
222238

223239
String sourceAnalyzerId = "test_cross_resource_source_" + UUID.randomUUID().toString().replace("-", "");
224240
String targetAnalyzerId = "test_cross_resource_target_" + UUID.randomUUID().toString().replace("-", "");

sdk/contentunderstanding/azure-ai-contentunderstanding/src/test/java/com/azure/ai/contentunderstanding/generated/Sample16_CreateAnalyzerWithLabels.java

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
import com.azure.ai.contentunderstanding.models.StringField;
2222
import com.azure.core.credential.AzureKeyCredential;
2323
import com.azure.core.util.polling.SyncPoller;
24+
import com.azure.identity.DefaultAzureCredentialBuilder;
2425
import org.junit.jupiter.api.Test;
2526

2627
import java.util.Collections;
@@ -57,9 +58,14 @@ public void testCreateAnalyzerWithLabelsAsync() {
5758
String endpoint = System.getenv("CONTENTUNDERSTANDING_ENDPOINT");
5859
String key = System.getenv("AZURE_CONTENT_UNDERSTANDING_KEY");
5960

60-
client = new ContentUnderstandingClientBuilder().endpoint(endpoint)
61-
.credential(new AzureKeyCredential(key))
62-
.buildClient();
61+
ContentUnderstandingClientBuilder builder = new ContentUnderstandingClientBuilder().endpoint(endpoint);
62+
63+
// Use DefaultAzureCredential if key is not provided, otherwise use AzureKeyCredential
64+
if (key == null || key.trim().isEmpty()) {
65+
client = builder.credential(new DefaultAzureCredentialBuilder().build()).buildClient();
66+
} else {
67+
client = builder.credential(new AzureKeyCredential(key)).buildClient();
68+
}
6369

6470
String analyzerId = "test_receipt_analyzer_" + UUID.randomUUID().toString().replace("-", "");
6571

@@ -242,7 +248,7 @@ public void testCreateAnalyzerWithActualLabels() {
242248
}
243249

244250
client = new ContentUnderstandingClientBuilder().endpoint(endpoint)
245-
.credential(new AzureKeyCredential(key))
251+
.credential(new DefaultAzureCredentialBuilder().build())
246252
.buildClient();
247253

248254
String analyzerId = "receipt_analyzer_with_training_" + UUID.randomUUID().toString().replace("-", "");

0 commit comments

Comments
 (0)