Skip to content

Commit a183da9

Browse files
committed
apply suggestions
1 parent 6eed629 commit a183da9

File tree

4 files changed

+18
-80
lines changed

4 files changed

+18
-80
lines changed

foundation-models/openai/src/main/java/com/sap/ai/sdk/foundationmodels/openai/OpenAiClient.java

Lines changed: 5 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -129,22 +129,6 @@ public OpenAiClient withSystemPrompt(@Nonnull final String systemPrompt) {
129129
return this;
130130
}
131131

132-
/**
133-
* Create a new OpenAI client with a custom HTTP request header added to every call made with this
134-
* client
135-
*
136-
* @param customHeader the custom header to add
137-
* @return a new client.
138-
* @since 1.11.0
139-
*/
140-
@Beta
141-
@Nonnull
142-
public OpenAiClient withHeader(@Nonnull final Header customHeader) {
143-
final var newDestination =
144-
DefaultHttpDestination.fromDestination(this.destination).header(customHeader).build();
145-
return new OpenAiClient(newDestination);
146-
}
147-
148132
/**
149133
* Create a new OpenAI client with a custom header added to every call made with this client
150134
*
@@ -156,8 +140,11 @@ public OpenAiClient withHeader(@Nonnull final Header customHeader) {
156140
@Beta
157141
@Nonnull
158142
public OpenAiClient withHeader(@Nonnull final String key, @Nonnull final String value) {
159-
final var customHeader = new Header(key, value);
160-
return this.withHeader(customHeader);
143+
final var newDestination =
144+
DefaultHttpDestination.fromDestination(this.destination)
145+
.header(new Header(key, value))
146+
.build();
147+
return new OpenAiClient(newDestination);
161148
}
162149

163150
/**

foundation-models/openai/src/test/java/com/sap/ai/sdk/foundationmodels/openai/OpenAiClientTest.java

Lines changed: 4 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,6 @@
1616
import com.sap.ai.sdk.foundationmodels.openai.model.OpenAiChatCompletionTool;
1717
import com.sap.ai.sdk.foundationmodels.openai.model.OpenAiContentFilterPromptResults;
1818
import com.sap.ai.sdk.foundationmodels.openai.model.OpenAiEmbeddingParameters;
19-
import com.sap.cloud.sdk.cloudplatform.connectivity.Header;
2019
import java.io.IOException;
2120
import java.util.List;
2221
import java.util.Map;
@@ -489,26 +488,16 @@ void testCustomHeaders() {
489488
final var request =
490489
new OpenAiChatCompletionRequest("Hello World! Why is this phrase so famous?");
491490

492-
var customHeader = new Header("foo", "bar");
493-
final var result =
494-
client.withHeader("footoo", "barzar").withHeader(customHeader).chatCompletion(request);
491+
final var result = client.withHeader("foo", "bar").chatCompletion(request);
495492
assertThat(result).isNotNull();
496493

497-
var newCustomHeader = new Header("foo", "baz");
498494
var streamResult =
499495
client
500-
.withHeader("footoo", "barz")
501-
.withHeader(newCustomHeader)
496+
.withHeader("foot", "baz")
502497
.streamChatCompletion("Hello World! Why is this phrase so famous?");
503498
assertThat(streamResult).isNotNull();
504499

505-
verify(
506-
postRequestedFor(anyUrl())
507-
.withHeader("foo", equalTo("bar"))
508-
.withHeader("footoo", equalTo("barzar")));
509-
verify(
510-
postRequestedFor(anyUrl())
511-
.withHeader("foo", equalTo("baz"))
512-
.withHeader("footoo", equalTo("barz")));
500+
verify(postRequestedFor(anyUrl()).withHeader("foo", equalTo("bar")));
501+
verify(postRequestedFor(anyUrl()).withHeader("foot", equalTo("baz")));
513502
}
514503
}

orchestration/src/main/java/com/sap/ai/sdk/orchestration/OrchestrationClient.java

Lines changed: 5 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -24,12 +24,13 @@
2424
import java.util.function.Supplier;
2525
import java.util.stream.Stream;
2626
import javax.annotation.Nonnull;
27-
import javax.annotation.Nullable;
27+
import lombok.RequiredArgsConstructor;
2828
import lombok.extern.slf4j.Slf4j;
2929
import lombok.val;
3030

3131
/** Client to execute requests to the orchestration service. */
3232
@Slf4j
33+
@RequiredArgsConstructor(access = lombok.AccessLevel.PRIVATE)
3334
public class OrchestrationClient {
3435
private static final String DEFAULT_SCENARIO = "orchestration";
3536
private static final String COMPLETION_ENDPOINT = "/v2/completion";
@@ -66,15 +67,6 @@ public OrchestrationClient(@Nonnull final HttpDestination destination) {
6667
this.executor = new OrchestrationHttpExecutor(() -> destination);
6768
}
6869

69-
private OrchestrationClient(
70-
@Nonnull final OrchestrationHttpExecutor executor,
71-
@Nullable final List<Header> customHeaders) {
72-
this.executor = executor;
73-
if (customHeaders != null) {
74-
this.customHeaders.addAll(customHeaders);
75-
}
76-
}
77-
7870
/**
7971
* Convert the given prompt and config into a low-level request data object. The data object
8072
* allows for further customization before sending the request.
@@ -258,22 +250,9 @@ EmbeddingsPostResponse embed(@Nonnull final EmbeddingsPostRequest request)
258250
@Beta
259251
@Nonnull
260252
public OrchestrationClient withHeader(@Nonnull final String key, @Nonnull final String value) {
261-
return this.withHeader(new Header(key, value));
262-
}
263-
264-
/**
265-
* Create a new orchestration client with a custom header added to every call made with this
266-
* client
267-
*
268-
* @param customHeader the custom header to add
269-
* @return a new client.
270-
* @since 1.11.0
271-
*/
272-
@Beta
273-
@Nonnull
274-
public OrchestrationClient withHeader(@Nonnull final Header customHeader) {
275-
final var newClient = new OrchestrationClient(this.executor, this.customHeaders);
276-
newClient.customHeaders.add(customHeader);
253+
final var newClient = new OrchestrationClient(this.executor);
254+
newClient.customHeaders.addAll(this.customHeaders);
255+
newClient.customHeaders.add(new Header(key, value));
277256
return newClient;
278257
}
279258
}

orchestration/src/test/java/com/sap/ai/sdk/orchestration/OrchestrationUnitTest.java

Lines changed: 4 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,6 @@
7878
import com.sap.cloud.sdk.cloudplatform.connectivity.ApacheHttpClient5Accessor;
7979
import com.sap.cloud.sdk.cloudplatform.connectivity.ApacheHttpClient5Cache;
8080
import com.sap.cloud.sdk.cloudplatform.connectivity.DefaultHttpDestination;
81-
import com.sap.cloud.sdk.cloudplatform.connectivity.Header;
8281
import java.io.IOException;
8382
import java.io.InputStream;
8483
import java.math.BigDecimal;
@@ -179,30 +178,14 @@ void testCustomHeaders() {
179178
.withBodyFile("templatingResponse.json")
180179
.withHeader("Content-Type", "application/json")));
181180

182-
var customHeader = new Header("foo", "bar");
183-
final var result =
184-
client
185-
.withHeader("footoo", "barzar")
186-
.withHeader(customHeader)
187-
.chatCompletion(prompt, config);
181+
final var result = client.withHeader("foo", "bar").chatCompletion(prompt, config);
188182
assertThat(result).isNotNull();
189183

190-
var newCustomHeader = new Header("foo", "baz");
191-
var streamResult =
192-
client
193-
.withHeader("footoo", "barz")
194-
.withHeader(newCustomHeader)
195-
.streamChatCompletion(prompt, config);
184+
var streamResult = client.withHeader("foot", "baz").streamChatCompletion(prompt, config);
196185
assertThat(streamResult).isNotNull();
197186

198-
verify(
199-
postRequestedFor(urlPathEqualTo("/v2/completion"))
200-
.withHeader("foo", equalTo("bar"))
201-
.withHeader("footoo", equalTo("barzar")));
202-
verify(
203-
postRequestedFor(urlPathEqualTo("/v2/completion"))
204-
.withHeader("foo", equalTo("baz"))
205-
.withHeader("footoo", equalTo("barz")));
187+
verify(postRequestedFor(urlPathEqualTo("/v2/completion")).withHeader("foo", equalTo("bar")));
188+
verify(postRequestedFor(urlPathEqualTo("/v2/completion")).withHeader("foot", equalTo("baz")));
206189
}
207190

208191
@Test

0 commit comments

Comments
 (0)