Skip to content

Commit 49c88f5

Browse files
Review comments
1 parent a57247f commit 49c88f5

File tree

6 files changed

+25
-36
lines changed

6 files changed

+25
-36
lines changed

core/src/main/java/com/sap/ai/sdk/core/commons/ClientResponseHandler.java

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -33,12 +33,12 @@
3333
@RequiredArgsConstructor
3434
public class ClientResponseHandler<T, E extends ClientException>
3535
implements HttpClientResponseHandler<T> {
36-
@Nonnull private final Class<T> responseType;
36+
@Nonnull final Class<T> responseType;
3737
@Nonnull private final Class<? extends ClientError> errorType;
38-
@Nonnull private final BiFunction<String, Throwable, E> exceptionType;
38+
@Nonnull final BiFunction<String, Throwable, E> exceptionType;
3939

4040
/** The parses for JSON responses, will be private once we can remove mixins */
41-
@Nonnull private ObjectMapper JACKSON = getDefaultObjectMapper();
41+
@Nonnull ObjectMapper objectMapper = getDefaultObjectMapper();
4242

4343
/**
4444
* Set the {@link ObjectMapper} to use for parsing JSON responses.
@@ -48,7 +48,7 @@ public class ClientResponseHandler<T, E extends ClientException>
4848
@Beta
4949
@Nonnull
5050
public ClientResponseHandler<T, E> objectMapper(@Nonnull final ObjectMapper jackson) {
51-
JACKSON = jackson;
51+
objectMapper = jackson;
5252
return this;
5353
}
5454

@@ -79,7 +79,7 @@ private T parseResponse(@Nonnull final ClassicHttpResponse response) throws E {
7979
val content = getContent(responseEntity);
8080
log.debug("Parsing response from JSON response: {}", content);
8181
try {
82-
return JACKSON.readValue(content, responseType);
82+
return objectMapper.readValue(content, responseType);
8383
} catch (final JsonProcessingException e) {
8484
log.error("Failed to parse the following response: {}", content);
8585
throw exceptionType.apply("Failed to parse response", e);
@@ -138,7 +138,7 @@ public void buildExceptionAndThrow(@Nonnull final ClassicHttpResponse response)
138138
*/
139139
public void parseErrorAndThrow(
140140
@Nonnull final String errorResponse, @Nonnull final E baseException) throws E {
141-
val maybeError = Try.of(() -> JACKSON.readValue(errorResponse, errorType));
141+
val maybeError = Try.of(() -> objectMapper.readValue(errorResponse, errorType));
142142
if (maybeError.isFailure()) {
143143
baseException.addSuppressed(maybeError.getCause());
144144
throw baseException;

core/src/main/java/com/sap/ai/sdk/core/commons/ClientStreamingHandler.java

Lines changed: 8 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,5 @@
11
package com.sap.ai.sdk.core.commons;
22

3-
import static com.sap.ai.sdk.core.JacksonConfiguration.getDefaultObjectMapper;
4-
53
import com.fasterxml.jackson.databind.ObjectMapper;
64
import java.io.IOException;
75
import java.util.function.BiFunction;
@@ -18,14 +16,8 @@
1816
* @since 1.1.0
1917
*/
2018
@Slf4j
21-
public class ClientStreamingHandler<D extends StreamedDelta, E extends ClientException> {
22-
23-
@Nonnull private final Class<D> deltaType;
24-
@Nonnull private final ClientResponseHandler<? extends ClientError, E> errorHandler;
25-
@Nonnull private final BiFunction<String, Throwable, E> exceptionType;
26-
27-
/** The parses for JSON responses, will be private once we can remove mixins */
28-
@Nonnull private ObjectMapper JACKSON = getDefaultObjectMapper();
19+
public class ClientStreamingHandler<D extends StreamedDelta, E extends ClientException>
20+
extends ClientResponseHandler<D, E> {
2921

3022
/**
3123
* Set the {@link ObjectMapper} to use for parsing JSON responses.
@@ -34,8 +26,7 @@ public class ClientStreamingHandler<D extends StreamedDelta, E extends ClientExc
3426
*/
3527
@Nonnull
3628
public ClientStreamingHandler<D, E> objectMapper(@Nonnull final ObjectMapper jackson) {
37-
JACKSON = jackson;
38-
errorHandler.objectMapper(jackson);
29+
super.objectMapper(jackson);
3930
return this;
4031
}
4132

@@ -50,9 +41,7 @@ public ClientStreamingHandler(
5041
@Nonnull final Class<D> deltaType,
5142
@Nonnull final Class<? extends ClientError> errorType,
5243
@Nonnull final BiFunction<String, Throwable, E> exceptionType) {
53-
this.deltaType = deltaType;
54-
this.exceptionType = exceptionType;
55-
this.errorHandler = new ClientResponseHandler<>(errorType, errorType, exceptionType);
44+
super(deltaType, errorType, exceptionType);
5645
}
5746

5847
/**
@@ -65,9 +54,9 @@ public ClientStreamingHandler(
6554
*/
6655
@SuppressWarnings("PMD.CloseResource") // Stream is closed automatically when consumed
6756
@Nonnull
68-
public Stream<D> handleResponse(@Nonnull final ClassicHttpResponse response) throws E {
57+
public Stream<D> handleStreamingResponse(@Nonnull final ClassicHttpResponse response) throws E {
6958
if (response.getCode() >= 300) {
70-
errorHandler.buildExceptionAndThrow(response);
59+
super.buildExceptionAndThrow(response);
7160
}
7261
return IterableStreamConverter.lines(response.getEntity(), exceptionType)
7362
// half of the lines are empty newlines, the last line is "data: [DONE]"
@@ -76,14 +65,14 @@ public Stream<D> handleResponse(@Nonnull final ClassicHttpResponse response) thr
7665
line -> {
7766
if (!line.startsWith("data: ")) {
7867
final String msg = "Failed to parse response";
79-
errorHandler.parseErrorAndThrow(line, exceptionType.apply(msg, null));
68+
super.parseErrorAndThrow(line, exceptionType.apply(msg, null));
8069
}
8170
})
8271
.map(
8372
line -> {
8473
final String data = line.substring(5); // remove "data: "
8574
try {
86-
return JACKSON.readValue(data, deltaType);
75+
return objectMapper.readValue(data, responseType);
8776
} catch (final IOException e) { // exception message e gets lost
8877
log.error("Failed to parse the following response: {}", line);
8978
throw exceptionType.apply("Failed to parse delta message: " + line, e);

foundation-models/openai/pom.xml

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -33,12 +33,12 @@
3333
</developers>
3434
<properties>
3535
<project.rootdir>${project.basedir}/../../</project.rootdir>
36-
<coverage.complexity>70%</coverage.complexity>
37-
<coverage.line>76%</coverage.line>
38-
<coverage.instruction>75%</coverage.instruction>
39-
<coverage.branch>66%</coverage.branch>
36+
<coverage.complexity>71%</coverage.complexity>
37+
<coverage.line>80%</coverage.line>
38+
<coverage.instruction>76%</coverage.instruction>
39+
<coverage.branch>69%</coverage.branch>
4040
<coverage.method>83%</coverage.method>
41-
<coverage.class>85%</coverage.class>
41+
<coverage.class>84%</coverage.class>
4242
</properties>
4343
<dependencies>
4444
<dependency>

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -300,7 +300,7 @@ private <D extends StreamedDelta> Stream<D> streamRequest(
300300
try {
301301
final var client = ApacheHttpClient5Accessor.getHttpClient(destination);
302302
return new ClientStreamingHandler<>(deltaType, OpenAiError.class, OpenAiClientException::new)
303-
.handleResponse(client.executeOpen(null, request, null));
303+
.handleStreamingResponse(client.executeOpen(null, request, null));
304304
} catch (final IOException e) {
305305
throw new OpenAiClientException("Request to OpenAI model failed", e);
306306
}

orchestration/pom.xml

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -32,10 +32,10 @@
3232
<properties>
3333
<project.rootdir>${project.basedir}/../</project.rootdir>
3434
<coverage.complexity>70%</coverage.complexity>
35-
<coverage.line>86%</coverage.line>
36-
<coverage.instruction>86%</coverage.instruction>
37-
<coverage.branch>67%</coverage.branch>
38-
<coverage.method>85%</coverage.method>
35+
<coverage.line>87%</coverage.line>
36+
<coverage.instruction>88%</coverage.instruction>
37+
<coverage.branch>65%</coverage.branch>
38+
<coverage.method>65%</coverage.method>
3939
<coverage.class>100%</coverage.class>
4040
</properties>
4141

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -288,7 +288,7 @@ private <D extends StreamedDelta> Stream<D> streamRequest(
288288
return new ClientStreamingHandler<>(
289289
deltaType, OrchestrationError.class, OrchestrationClientException::new)
290290
.objectMapper(JACKSON)
291-
.handleResponse(client.executeOpen(null, request, null));
291+
.handleStreamingResponse(client.executeOpen(null, request, null));
292292
} catch (final IOException e) {
293293
throw new OrchestrationClientException("Request to the Orchestration service failed", e);
294294
}

0 commit comments

Comments
 (0)