Skip to content

Commit d33ae3b

Browse files
committed
apply suggestions
1 parent 329a7d3 commit d33ae3b

File tree

3 files changed

+36
-16
lines changed

3 files changed

+36
-16
lines changed

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

Lines changed: 13 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,8 @@
2828
import com.sap.cloud.sdk.cloudplatform.connectivity.Header;
2929
import com.sap.cloud.sdk.cloudplatform.connectivity.HttpDestination;
3030
import java.io.IOException;
31+
import java.util.ArrayList;
32+
import java.util.List;
3133
import java.util.stream.Stream;
3234
import javax.annotation.Nonnull;
3335
import javax.annotation.Nullable;
@@ -50,6 +52,7 @@ public final class OpenAiClient {
5052
@Nullable private String systemPrompt = null;
5153

5254
@Nonnull private final Destination destination;
55+
@Nonnull private final List<Header> customHeaders = new ArrayList<>();
5356

5457
/**
5558
* Create a new OpenAI client for the given foundation model, using the default resource group.
@@ -139,11 +142,10 @@ public OpenAiClient withSystemPrompt(@Nonnull final String systemPrompt) {
139142
@Beta
140143
@Nonnull
141144
public OpenAiClient withHeader(@Nonnull final String key, @Nonnull final String value) {
142-
final var newDestination =
143-
DefaultHttpDestination.fromDestination(this.destination)
144-
.header(new Header(key, value))
145-
.build();
146-
return new OpenAiClient(newDestination);
145+
final var newClient = new OpenAiClient(this.destination);
146+
newClient.customHeaders.addAll(this.customHeaders);
147+
newClient.customHeaders.add(new Header(key, value));
148+
return newClient;
147149
}
148150

149151
/**
@@ -414,7 +416,7 @@ private <T> T execute(
414416
@Nonnull final Object payload,
415417
@Nonnull final Class<T> responseType) {
416418
final var request = new HttpPost(path);
417-
serializeAndSetHttpEntity(request, payload);
419+
serializeAndSetHttpEntity(request, payload, this.customHeaders);
418420
return executeRequest(request, responseType);
419421
}
420422

@@ -424,15 +426,18 @@ private <D extends StreamedDelta> Stream<D> executeStream(
424426
@Nonnull final Object payload,
425427
@Nonnull final Class<D> deltaType) {
426428
final var request = new HttpPost(path);
427-
serializeAndSetHttpEntity(request, payload);
429+
serializeAndSetHttpEntity(request, payload, this.customHeaders);
428430
return streamRequest(request, deltaType);
429431
}
430432

431433
private static void serializeAndSetHttpEntity(
432-
@Nonnull final BasicClassicHttpRequest request, @Nonnull final Object payload) {
434+
@Nonnull final BasicClassicHttpRequest request,
435+
@Nonnull final Object payload,
436+
@Nonnull final List<Header> customHeaders) {
433437
try {
434438
final var json = JACKSON.writeValueAsString(payload);
435439
request.setEntity(new StringEntity(json, ContentType.APPLICATION_JSON));
440+
customHeaders.forEach(h -> request.addHeader(h.getName(), h.getValue()));
436441
} catch (final JsonProcessingException e) {
437442
throw new OpenAiClientException("Failed to serialize request parameters", e)
438443
.setHttpRequest(request);

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

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -487,17 +487,24 @@ void testCustomHeaders() {
487487
stubForChatCompletion();
488488
final var request =
489489
new OpenAiChatCompletionRequest("Hello World! Why is this phrase so famous?");
490+
final var clientWithHeader = client.withHeader("Header-For-Both", "value");
490491

491-
final var result = client.withHeader("foo", "bar").chatCompletion(request);
492+
final var result = clientWithHeader.withHeader("foo", "bar").chatCompletion(request);
492493
assertThat(result).isNotNull();
493494

494495
var streamResult =
495-
client
496+
clientWithHeader
496497
.withHeader("foot", "baz")
497498
.streamChatCompletion("Hello World! Why is this phrase so famous?");
498499
assertThat(streamResult).isNotNull();
499500

500-
verify(postRequestedFor(anyUrl()).withHeader("foo", equalTo("bar")));
501-
verify(postRequestedFor(anyUrl()).withHeader("foot", equalTo("baz")));
501+
verify(
502+
postRequestedFor(anyUrl())
503+
.withHeader("Header-For-Both", equalTo("value"))
504+
.withHeader("foo", equalTo("bar")));
505+
verify(
506+
postRequestedFor(anyUrl())
507+
.withHeader("Header-For-Both", equalTo("value"))
508+
.withHeader("foot", equalTo("baz")));
502509
}
503510
}

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

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -178,14 +178,22 @@ void testCustomHeaders() {
178178
.withBodyFile("templatingResponse.json")
179179
.withHeader("Content-Type", "application/json")));
180180

181-
final var result = client.withHeader("foo", "bar").chatCompletion(prompt, config);
181+
final var clientWithHeader = client.withHeader("Header-For-Both", "value");
182+
final var result = clientWithHeader.withHeader("foo", "bar").chatCompletion(prompt, config);
182183
assertThat(result).isNotNull();
183184

184-
var streamResult = client.withHeader("foot", "baz").streamChatCompletion(prompt, config);
185+
var streamResult =
186+
clientWithHeader.withHeader("foot", "baz").streamChatCompletion(prompt, config);
185187
assertThat(streamResult).isNotNull();
186188

187-
verify(postRequestedFor(urlPathEqualTo("/v2/completion")).withHeader("foo", equalTo("bar")));
188-
verify(postRequestedFor(urlPathEqualTo("/v2/completion")).withHeader("foot", equalTo("baz")));
189+
verify(
190+
postRequestedFor(urlPathEqualTo("/v2/completion"))
191+
.withHeader("Header-For-Both", equalTo("value"))
192+
.withHeader("foo", equalTo("bar")));
193+
verify(
194+
postRequestedFor(urlPathEqualTo("/v2/completion"))
195+
.withHeader("Header-For-Both", equalTo("value"))
196+
.withHeader("foot", equalTo("baz")));
189197
}
190198

191199
@Test

0 commit comments

Comments
 (0)