Skip to content

Commit ef3a862

Browse files
committed
Minor improvements
1 parent 32788ab commit ef3a862

File tree

2 files changed

+14
-13
lines changed

2 files changed

+14
-13
lines changed

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

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,6 @@
3333
import org.apache.hc.client5.http.classic.methods.HttpPost;
3434
import org.apache.hc.core5.http.ContentType;
3535
import org.apache.hc.core5.http.io.entity.StringEntity;
36-
import org.apache.hc.core5.http.message.BasicClassicHttpRequest;
3736
import org.springframework.http.converter.json.Jackson2ObjectMapperBuilder;
3837

3938
/** Client to execute requests to the orchestration service. */
@@ -135,16 +134,15 @@ public OrchestrationChatResponse chatCompletion(
135134
@Nonnull
136135
public CompletionPostResponse executeRequest(@Nonnull final CompletionPostRequest request)
137136
throws OrchestrationClientException {
138-
final BasicClassicHttpRequest postRequest = new HttpPost("/completion");
137+
final String jsonRequest;
139138
try {
140-
val json = JACKSON.writeValueAsString(request);
141-
log.debug("Serialized request into JSON payload: {}", json);
142-
postRequest.setEntity(new StringEntity(json, ContentType.APPLICATION_JSON));
139+
jsonRequest = JACKSON.writeValueAsString(request);
140+
log.debug("Serialized request into JSON payload: {}", jsonRequest);
143141
} catch (final JsonProcessingException e) {
144142
throw new OrchestrationClientException("Failed to serialize request parameters", e);
145143
}
146144

147-
return executeRequest(postRequest);
145+
return executeRequest(jsonRequest);
148146
}
149147

150148
/**
@@ -180,29 +178,31 @@ public OrchestrationChatResponse executeRequestFromJsonModuleConfig(
180178
try {
181179
moduleConfigJson = JACKSON.readTree(moduleConfig);
182180
} catch (JsonProcessingException e) {
183-
throw new IllegalArgumentException("The provided module configuration is not valid JSON", e);
181+
throw new IllegalArgumentException(
182+
"The provided module configuration is not valid JSON: " + moduleConfig, e);
184183
}
185184
requestJson.set("orchestration_config", moduleConfigJson);
186185

187-
val postRequest = new HttpPost("/completion");
188186
final String body;
189187
try {
190188
body = JACKSON.writeValueAsString(requestJson);
191189
} catch (JsonProcessingException e) {
192190
throw new OrchestrationClientException("Failed to serialize request to JSON", e);
193191
}
194-
postRequest.setEntity(new StringEntity(body, ContentType.APPLICATION_JSON));
195-
return new OrchestrationChatResponse(executeRequest(postRequest));
192+
return new OrchestrationChatResponse(executeRequest(body));
196193
}
197194

198195
@Nonnull
199-
CompletionPostResponse executeRequest(@Nonnull final BasicClassicHttpRequest request) {
196+
CompletionPostResponse executeRequest(@Nonnull final String request) {
197+
val postRequest = new HttpPost("/completion");
198+
postRequest.setEntity(new StringEntity(request, ContentType.APPLICATION_JSON));
199+
200200
try {
201201
val destination = deployment.get().destination();
202202
log.debug("Using destination {} to connect to orchestration service", destination);
203203
val client = ApacheHttpClient5Accessor.getHttpClient(destination);
204204
return client.execute(
205-
request, new OrchestrationResponseHandler<>(CompletionPostResponse.class));
205+
postRequest, new OrchestrationResponseHandler<>(CompletionPostResponse.class));
206206
} catch (NoSuchElementException
207207
| DestinationAccessException
208208
| DestinationNotFoundException

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

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -427,7 +427,8 @@ void testExecuteRequestFromJson() {
427427
}
428428
""";
429429

430-
client.executeRequestFromJsonModuleConfig(prompt, configJson);
430+
var result = client.executeRequestFromJsonModuleConfig(prompt, configJson);
431+
assertThat(result).isNotNull();
431432

432433
verify(postRequestedFor(anyUrl()).withRequestBody(equalToJson(expectedJson)));
433434
}

0 commit comments

Comments
 (0)