Skip to content

Commit 7e08c19

Browse files
authored
Merge pull request #80 from IBM/api-key-auth-convenience
Add API key authentication convenience method to WatsonxService builder
2 parents 812af57 + b8ba402 commit 7e08c19

File tree

13 files changed

+105
-70
lines changed

13 files changed

+105
-70
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/main/java/com/ibm/watsonx/ai/chat/ChatService.java

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -37,14 +37,14 @@
3737
import com.ibm.watsonx.ai.core.auth.AuthenticationProvider;
3838

3939
/**
40-
* Service class to interact with IBM watsonx.ai Text Chat APIs.
40+
* Service for interacting with IBM watsonx.ai Text Chat APIs.
4141
* <p>
4242
* <b>Example usage:</b>
4343
*
4444
* <pre>{@code
4545
* ChatService chatService = ChatService.builder()
46-
* .url("https://...") // or use CloudRegion
47-
* .authenticationProvider(authProvider)
46+
* .url("https://...") // or use CloudRegion
47+
* .apiKey("my-api-key") // creates an IAM-based AuthenticationProvider
4848
* .projectId("my-project-id")
4949
* .modelId("ibm/granite-3-8b-instruct")
5050
* .build();
@@ -55,7 +55,7 @@
5555
* );
5656
* }</pre>
5757
*
58-
* For more information, see the <a href="https://cloud.ibm.com/apidocs/watsonx-ai#text-chat" target="_blank"> official documentation</a>.
58+
* To use a custom authentication mechanism, configure it explicitly with {@link #authenticationProvider(AuthenticationProvider)}.
5959
*
6060
* @see AuthenticationProvider
6161
*/
@@ -369,8 +369,8 @@ public CompletableFuture<Void> chatStreaming(List<ChatMessage> messages, ChatPar
369369
*
370370
* <pre>{@code
371371
* ChatService chatService = ChatService.builder()
372-
* .url("https://...") // or use CloudRegion
373-
* .authenticationProvider(authProvider)
372+
* .url("https://...") // or use CloudRegion
373+
* .apiKey("my-api-key") // creates an IAM-based AuthenticationProvider
374374
* .projectId("my-project-id")
375375
* .modelId("ibm/granite-3-8b-instruct")
376376
* .build();
@@ -381,7 +381,6 @@ public CompletableFuture<Void> chatStreaming(List<ChatMessage> messages, ChatPar
381381
* );
382382
* }</pre>
383383
*
384-
* @see AuthenticationProvider
385384
* @return {@link Builder} instance.
386385
*/
387386
public static Builder builder() {

modules/watsonx-ai/src/main/java/com/ibm/watsonx/ai/deployment/DeploymentService.java

Lines changed: 5 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -53,13 +53,13 @@
5353
*
5454
* <pre>{@code
5555
* DeploymentService deploymentService = DeploymentService.builder()
56-
* .url("https://...") // or use CloudRegion
57-
* .authenticationProvider(authProvider)
56+
* .url("https://...") // or use CloudRegion
57+
* .apiKey("my-api-key") // creates an IAM-based AuthenticationProvider
5858
* .build();
5959
*
6060
* }</pre>
6161
*
62-
* For more information, see the <a href="https://cloud.ibm.com/apidocs/watsonx-ai#deployments-text-chat" target="_blank"> official documentation</a>.
62+
* To use a custom authentication mechanism, configure it explicitly with {@link #authenticationProvider(AuthenticationProvider)}. *
6363
*
6464
* @see AuthenticationProvider
6565
*/
@@ -332,17 +332,11 @@ public ForecastResponse forecast(TimeSeriesRequest timeSeriesRequest) {
332332
*
333333
* <pre>{@code
334334
* DeploymentService deploymentService = DeploymentService.builder()
335-
* .url("https://...") // or use CloudRegion
336-
* .deployment("...")
337-
* .authenticationProvider(authProvider)
335+
* .url("https://...") // or use CloudRegion
336+
* .apiKey("my-api-key") // creates an IAM-based AuthenticationProvider
338337
* .build();
339-
*
340-
* ChatResponse response = deploymentService.chat(
341-
* UserMessage.text("Tell me a joke")
342-
* );
343338
* }</pre>
344339
*
345-
* @see AuthenticationProvider
346340
* @return {@link Builder} instance.
347341
*/
348342
public static Builder builder() {

modules/watsonx-ai/src/main/java/com/ibm/watsonx/ai/embedding/EmbeddingService.java

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -29,8 +29,8 @@
2929
*
3030
* <pre>{@code
3131
* EmbeddingService embeddingService = EmbeddingService.builder()
32-
* .url("https://...") // or use CloudRegion
33-
* .authenticationProvider(authProvider)
32+
* .url("https://...") // or use CloudRegion
33+
* .apiKey("my-api-key") // creates an IAM-based AuthenticationProvider
3434
* .projectId("my-project-id")
3535
* .modelId("ibm/granite-embedding-278m-multilingual")
3636
* .build();
@@ -41,7 +41,7 @@
4141
* );
4242
* }</pre>
4343
*
44-
* For more information, see the <a href="https://cloud.ibm.com/apidocs/watsonx-ai#text-embeddings" target="_blank"> official documentation</a>.
44+
* To use a custom authentication mechanism, configure it explicitly with {@link #authenticationProvider(AuthenticationProvider)}.
4545
*
4646
* @see AuthenticationProvider
4747
*/
@@ -144,8 +144,8 @@ public EmbeddingResponse embedding(List<String> inputs, EmbeddingParameters para
144144
*
145145
* <pre>{@code
146146
* EmbeddingService embeddingService = EmbeddingService.builder()
147-
* .url("https://...") // or use CloudRegion
148-
* .authenticationProvider(authProvider)
147+
* .url("https://...") // or use CloudRegion
148+
* .apiKey("my-api-key") // creates an IAM-based AuthenticationProvider
149149
* .projectId("my-project-id")
150150
* .modelId("ibm/granite-embedding-278m-multilingual")
151151
* .build();
@@ -156,7 +156,6 @@ public EmbeddingResponse embedding(List<String> inputs, EmbeddingParameters para
156156
* );
157157
* }</pre>
158158
*
159-
* @see AuthenticationProvider
160159
* @return {@link Builder} instance.
161160
*/
162161
public static Builder builder() {

modules/watsonx-ai/src/main/java/com/ibm/watsonx/ai/foundationmodel/FoundationModelService.java

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -36,9 +36,6 @@
3636
* var maxSequenceLength = result.maxSequenceLength();
3737
* }</pre>
3838
*
39-
* For more information, see the <a href="https://cloud.ibm.com/apidocs/watsonx-ai#list-foundation-model-specs" target="_blank"> official
40-
* documentation</a>.
41-
*
4239
* @see AuthenticationProvider
4340
*/
4441
public final class FoundationModelService extends WatsonxService {
@@ -222,7 +219,6 @@ public FoundationModelResponse<FoundationModelTask> getTasks(FoundationModelPara
222219
* var maxSequenceLength = result.maxSequenceLength();
223220
* }</pre>
224221
*
225-
* @see AuthenticationProvider
226222
* @return {@link Builder} instance.
227223
*/
228224
public static Builder builder() {

modules/watsonx-ai/src/main/java/com/ibm/watsonx/ai/rerank/RerankService.java

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -29,8 +29,8 @@
2929
*
3030
* <pre>{@code
3131
* RerankService rerankService = RerankService.builder()
32-
* .url("https://...") // or use CloudRegion
33-
* .authenticationProvider(authProvider)
32+
* .url("https://...") // or use CloudRegion
33+
* .apiKey("my-api-key") // creates an IAM-based AuthenticationProvider
3434
* .projectId("my-project-id")
3535
* .modelId("cross-encoder/ms-marco-minilm-l-12-v2")
3636
* .build();
@@ -44,7 +44,7 @@
4444
* );
4545
* }</pre>
4646
*
47-
* For more information, see the <a href="https://cloud.ibm.com/apidocs/watsonx-ai#text-rerank" target="_blank"> official documentation</a>.
47+
* To use a custom authentication mechanism, configure it explicitly with {@link #authenticationProvider(AuthenticationProvider)}.
4848
*
4949
* @see AuthenticationProvider
5050
*/
@@ -128,8 +128,8 @@ public RerankResponse rerank(String query, List<String> inputs, RerankParameters
128128
*
129129
* <pre>{@code
130130
* RerankService rerankService = RerankService.builder()
131-
* .url("https://...") // or use CloudRegion
132-
* .authenticationProvider(authProvider)
131+
* .url("https://...") // or use CloudRegion
132+
* .apiKey("my-api-key") // creates an IAM-based AuthenticationProvider
133133
* .projectId("my-project-id")
134134
* .modelId("cross-encoder/ms-marco-minilm-l-12-v2")
135135
* .build();
@@ -143,7 +143,6 @@ public RerankResponse rerank(String query, List<String> inputs, RerankParameters
143143
* );
144144
* }</pre>
145145
*
146-
* @see AuthenticationProvider
147146
* @return {@link Builder} instance.
148147
*/
149148
public static Builder builder() {

modules/watsonx-ai/src/main/java/com/ibm/watsonx/ai/textextraction/TextExtractionService.java

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -51,9 +51,9 @@
5151
*
5252
* <pre>{@code
5353
* TextExtractionService textExtractionService = TextExtractionService.builder()
54-
* .url("https://...") // or use CloudRegion
55-
* .cosUrl("https://...") // or use CosUrl
56-
* .authenticationProvider(authProvider)
54+
* .url("https://...") // or use CloudRegion
55+
* .cosUrl("https://...") // or use CosUrl
56+
* .apiKey("my-api-key") // creates an IAM-based AuthenticationProvider
5757
* .projectId("my-project-id")
5858
* .documentReference("<connection_id>", "<bucket-name")
5959
* .resultReference("<connection_id>", "<bucket-name")
@@ -62,7 +62,7 @@
6262
* TextExtractionResponse response = textExtractionService.startExtraction("myfile.pdf")
6363
* }</pre>
6464
*
65-
* For more information, see the <a href="https://cloud.ibm.com/apidocs/watsonx-ai#text-extraction" target="_blank"> official documentation</a>.
65+
* To use a custom authentication mechanism, configure it explicitly with {@link #authenticationProvider(AuthenticationProvider)}.
6666
*
6767
* @see AuthenticationProvider
6868
*/
@@ -824,9 +824,9 @@ private InputStream getExtractedText(String requestId, TextExtractionResponse te
824824
*
825825
* <pre>{@code
826826
* TextExtractionService textExtractionService = TextExtractionService.builder()
827-
* .url("https://...") // or use CloudRegion
828-
* .cosUrl("https://...") // or use CosUrl
829-
* .authenticationProvider(authProvider)
827+
* .url("https://...") // or use CloudRegion
828+
* .cosUrl("https://...") // or use CosUrl
829+
* .apiKey("my-api-key") // creates an IAM-based AuthenticationProvider
830830
* .projectId("my-project-id")
831831
* .documentReference("<connection_id>", "<bucket-name")
832832
* .resultReference("<connection_id>", "<bucket-name")
@@ -835,7 +835,6 @@ private InputStream getExtractedText(String requestId, TextExtractionResponse te
835835
* TextExtractionResponse response = textExtractionService.startExtraction("myfile.pdf")
836836
* }</pre>
837837
*
838-
* @see AuthenticationProvider
839838
* @return {@link Builder} instance.
840839
*/
841840
public static Builder builder() {

modules/watsonx-ai/src/main/java/com/ibm/watsonx/ai/textgeneration/TextGenerationService.java

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -32,16 +32,16 @@
3232
*
3333
* <pre>{@code
3434
* TextGenerationService textGenerationService = TextGenerationService.builder()
35-
* .url("https://...") // or use CloudRegion
36-
* .authenticationProvider(authProvider)
35+
* .url("https://...") // or use CloudRegion
36+
* .apiKey("my-api-key") // creates an IAM-based AuthenticationProvider
3737
* .projectId("my-project-id")
3838
* .modelId("ibm/granite-13b-instruct-v2")
3939
* .build();
4040
*
4141
* TextGenerationResponse response = textGenerationService.generate("Hello!");
4242
* }</pre>
4343
*
44-
* For more information, see the <a href="https://cloud.ibm.com/apidocs/watsonx-ai#text-generation" target="_blank"> official documentation</a>.
44+
* To use a custom authentication mechanism, configure it explicitly with {@link #authenticationProvider(AuthenticationProvider)}.
4545
*
4646
* @see AuthenticationProvider
4747
*/
@@ -223,16 +223,15 @@ public CompletableFuture<Void> generateStreaming(String input, TextGenerationPar
223223
*
224224
* <pre>{@code
225225
* TextGenerationService textGenerationService = TextGenerationService.builder()
226-
* .url("https://...") // or use CloudRegion
227-
* .authenticationProvider(authProvider)
226+
* .url("https://...") // or use CloudRegion
227+
* .apiKey("my-api-key") // creates an IAM-based AuthenticationProvider
228228
* .projectId("my-project-id")
229229
* .modelId("ibm/granite-13b-instruct-v2")
230230
* .build();
231231
*
232232
* TextGenerationResponse response = textGenerationService.generate("Hello!");
233233
* }</pre>
234234
*
235-
* @see AuthenticationProvider
236235
* @return {@link Builder} instance.
237236
*/
238237
public static Builder builder() {

modules/watsonx-ai/src/main/java/com/ibm/watsonx/ai/timeseries/TimeSeriesService.java

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -28,8 +28,8 @@
2828
*
2929
* <pre>{@code
3030
* TimeSeriesService tsService = TimeSeriesService.builder()
31-
* .url("https://...") // or use CloudRegion
32-
* .authenticationProvider(authProvider)
31+
* .url("https://...") // or use CloudRegion
32+
* .apiKey("my-api-key") // creates an IAM-based AuthenticationProvider
3333
* .projectId("my-project-id")
3434
* .modelId("ibm/granite-ttm-1536-96-r2")
3535
* .build();
@@ -54,7 +54,7 @@
5454
* ForecastResponse response = tsService.forecast(request);
5555
* }</pre>
5656
*
57-
* For more information, see the <a href="https://cloud.ibm.com/apidocs/watsonx-ai#time-series-forecast" target="_blank"> official documentation</a>.
57+
* To use a custom authentication mechanism, configure it explicitly with {@link #authenticationProvider(AuthenticationProvider)}.
5858
*
5959
* @see AuthenticationProvider
6060
*/
@@ -149,8 +149,8 @@ public ForecastResponse forecast(InputSchema inputSchema, ForecastData data, Tim
149149
*
150150
* <pre>{@code
151151
* TimeSeriesService tsService = TimeSeriesService.builder()
152-
* .url("https://...") // or use CloudRegion
153-
* .authenticationProvider(authProvider)
152+
* .url("https://...") // or use CloudRegion
153+
* .apiKey("my-api-key") // creates an IAM-based AuthenticationProvider
154154
* .projectId("my-project-id")
155155
* .modelId("ibm/granite-ttm-1536-96-r2")
156156
* .build();
@@ -175,7 +175,6 @@ public ForecastResponse forecast(InputSchema inputSchema, ForecastData data, Tim
175175
* ForecastResponse response = tsService.forecast(request);
176176
* }</pre>
177177
*
178-
* @see AuthenticationProvider
179178
* @return {@link Builder} instance.
180179
*/
181180
public static Builder builder() {

modules/watsonx-ai/src/main/java/com/ibm/watsonx/ai/tokenization/TokenizationService.java

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -28,16 +28,16 @@
2828
*
2929
* <pre>{@code
3030
* TokenizationService tokenizationService = TokenizationService.builder()
31-
* .url("https://...") // or use CloudRegion
32-
* .authenticationProvider(authProvider)
31+
* .url("https://...") // or use CloudRegion
32+
* .apiKey("my-api-key") // creates an IAM-based AuthenticationProvider
3333
* .projectId("my-project-id")
3434
* .modelId("ibm/granite-3-8b-instruct")
3535
* .build();
3636
*
3737
* TokenizationResponse response = tokenizationService.tokenize("Tell me a joke");
3838
* }</pre>
3939
*
40-
* For more information, see the <a href="https://cloud.ibm.com/apidocs/watsonx-ai#text-tokenization" target="_blank"> official documentation</a>.
40+
* To use a custom authentication mechanism, configure it explicitly with {@link #authenticationProvider(AuthenticationProvider)}.
4141
*
4242
* @see AuthenticationProvider
4343
*/
@@ -148,16 +148,15 @@ private HttpRequest buildHttpRequest(String input, TokenizationParameters parame
148148
*
149149
* <pre>{@code
150150
* TokenizationService tokenizationService = TokenizationService.builder()
151-
* .url("https://...") // or use CloudRegion
152-
* .authenticationProvider(authProvider)
151+
* .url("https://...") // or use CloudRegion
152+
* .apiKey("my-api-key") // creates an IAM-based AuthenticationProvider
153153
* .projectId("my-project-id")
154154
* .modelId("ibm/granite-3-8b-instruct")
155155
* .build();
156156
*
157157
* TokenizationResponse response = tokenizationService.tokenize("Tell me a joke");
158158
* }</pre>
159159
*
160-
* @see AuthenticationProvider
161160
* @return {@link Builder} instance.
162161
*/
163162
public static Builder builder() {

0 commit comments

Comments
 (0)