Skip to content

Commit d718b2b

Browse files
committed
Add API key authentication convenience method to WatsonxService builder
Introduced a new `apiKey(String apiKey)` method in the `WatsonxService.Builder` class to simplify IAM authentication setup. This method internally creates an `IAMAuthenticator` instance using the provided IBM Cloud API key Signed-off-by: andreadimaio <[email protected]>
1 parent 3eb791d commit d718b2b

File tree

3 files changed

+52
-3
lines changed

3 files changed

+52
-3
lines changed

modules/watsonx-ai/src/main/java/com/ibm/watsonx/ai/WatsonxService.java

Lines changed: 20 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
import java.util.concurrent.Executor;
1414
import com.ibm.watsonx.ai.chat.ChatService;
1515
import com.ibm.watsonx.ai.core.auth.AuthenticationProvider;
16+
import com.ibm.watsonx.ai.core.auth.iam.IAMAuthenticator;
1617
import com.ibm.watsonx.ai.core.http.AsyncHttpClient;
1718
import com.ibm.watsonx.ai.core.http.SyncHttpClient;
1819
import com.ibm.watsonx.ai.core.http.interceptors.BearerInterceptor;
@@ -172,9 +173,26 @@ public T timeout(Duration timeout) {
172173
}
173174

174175
/**
175-
* Sets the {@link AuthenticationProvider} used for authenticating requests.
176+
* Sets an {@link IAMAuthenticator}-based {@link AuthenticationProvider}, initialized from the provided IBM Cloud API key.
177+
* <p>
178+
* For alternative authentication mechanisms, use {@link #authenticationProvider(AuthenticationProvider)}.
176179
*
177-
* @param authenticationProvider {@link AuthenticationProvider} instance
180+
* @param apiKey IBM Cloud API key
181+
*/
182+
public T apiKey(String apiKey) {
183+
requireNonNull(apiKey, "The apiKey must be provided");
184+
authenticationProvider = IAMAuthenticator.builder().apiKey(apiKey).build();
185+
return (T) this;
186+
}
187+
188+
/**
189+
* Sets the {@link AuthenticationProvider} used to authenticate requests.
190+
* <p>
191+
* Use this method to specify a custom or non-IAM implementation.
192+
* <p>
193+
* For IBM Cloud IAM authentication, {@link #apiKey(String)} provides a simpler alternative.
194+
*
195+
* @param authenticationProvider non-null {@link AuthenticationProvider} instance
178196
*/
179197
public T authenticationProvider(AuthenticationProvider authenticationProvider) {
180198
this.authenticationProvider = authenticationProvider;

modules/watsonx-ai/src/test/java/com/ibm/watsonx/ai/chat/ChatServiceTest.java

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@
3030
import static org.mockito.Mockito.inOrder;
3131
import static org.mockito.Mockito.mock;
3232
import static org.mockito.Mockito.mockStatic;
33+
import static org.mockito.Mockito.spy;
3334
import static org.mockito.Mockito.when;
3435
import java.io.File;
3536
import java.io.IOException;
@@ -3287,4 +3288,34 @@ void test_thinking_without_extraction_tags() {
32873288
"Extraction tags are required when using control messages");
32883289
});
32893290
}
3291+
3292+
@Test
3293+
void test_builder_with_api_key() {
3294+
3295+
withWatsonxServiceMock(() -> {
3296+
3297+
var chatServiceBuilder = ChatService.builder()
3298+
.apiKey("my-api-key");
3299+
3300+
assertNotNull(chatServiceBuilder.getAuthenticationProvider());
3301+
var spyAuthenticator = spy(chatServiceBuilder.getAuthenticationProvider());
3302+
3303+
when(mockHttpResponse.statusCode()).thenReturn(200);
3304+
when(mockHttpResponse.body()).thenReturn(
3305+
"""
3306+
{
3307+
"access_token": "my-super-token",
3308+
"refresh_token": "not_supported",
3309+
"ims_user_id": 14364907,
3310+
"token_type": "Bearer",
3311+
"expires_in": 3600,
3312+
"expiration": 1757106813,
3313+
"scope": "ibm openid"
3314+
}""");
3315+
3316+
mockHttpClientSend(mockHttpRequest.capture(), any(BodyHandler.class));
3317+
spyAuthenticator.token();
3318+
assertEquals("grant_type=urn%3Aibm%3Aparams%3Aoauth%3Agrant-type%3Aapikey&apikey=my-api-key", bodyPublisherToString(mockHttpRequest));
3319+
});
3320+
}
32903321
}

modules/watsonx-ai/src/test/java/com/ibm/watsonx/ai/it/ChatServiceIT.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -72,9 +72,9 @@ void test_chat() {
7272

7373
var chatService = ChatService.builder()
7474
.url(URL)
75+
.apiKey(API_KEY)
7576
.projectId(PROJECT_ID)
7677
.modelId("ibm/granite-3-3-8b-instruct")
77-
.authenticationProvider(authentication)
7878
.logRequests(true)
7979
.logResponses(true)
8080
.build();

0 commit comments

Comments
 (0)