Skip to content

Commit 9a8799b

Browse files
committed
Fix formatting
Signed-off-by: Filip Hrisafov <[email protected]>
1 parent f0858ad commit 9a8799b

File tree

2 files changed

+40
-46
lines changed

2 files changed

+40
-46
lines changed

models/spring-ai-anthropic/src/main/java/org/springframework/ai/anthropic/api/AnthropicApi.java

Lines changed: 15 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -164,20 +164,17 @@ public ResponseEntity<ChatCompletionResponse> chatCompletionEntity(ChatCompletio
164164
Assert.isTrue(!chatRequest.stream(), "Request must set the stream property to false.");
165165
Assert.notNull(additionalHttpHeader, "The additional HTTP headers can not be null.");
166166

167+
// @formatter:off
167168
return this.restClient.post()
168169
.uri(this.completionsPath)
169170
.headers(headers -> {
170171
headers.addAll(additionalHttpHeader);
171-
if (!headers.containsKey(HEADER_X_API_KEY)) {
172-
String apiKeyValue = this.apiKey.getValue();
173-
if (StringUtils.hasText(apiKeyValue)) {
174-
headers.add(HEADER_X_API_KEY, apiKeyValue);
175-
}
176-
}
172+
addDefaultHeadersIfMissing(headers);
177173
})
178174
.body(chatRequest)
179175
.retrieve()
180176
.toEntity(ChatCompletionResponse.class);
177+
// @formatter:on
181178
}
182179

183180
/**
@@ -208,17 +205,13 @@ public Flux<ChatCompletionResponse> chatCompletionStream(ChatCompletionRequest c
208205

209206
AtomicReference<ChatCompletionResponseBuilder> chatCompletionReference = new AtomicReference<>();
210207

208+
// @formatter:off
211209
return this.webClient.post()
212210
.uri(this.completionsPath)
213211
.headers(headers -> {
214212
headers.addAll(additionalHttpHeader);
215-
if (!headers.containsKey(HEADER_X_API_KEY)) {
216-
String apiKeyValue = this.apiKey.getValue();
217-
if (StringUtils.hasText(apiKeyValue)) {
218-
headers.add(HEADER_X_API_KEY, apiKeyValue);
219-
}
220-
}
221-
})
213+
addDefaultHeadersIfMissing(headers);
214+
}) // @formatter:off
222215
.body(Mono.just(chatRequest), ChatCompletionRequest.class)
223216
.retrieve()
224217
.bodyToFlux(String.class)
@@ -252,6 +245,15 @@ public Flux<ChatCompletionResponse> chatCompletionStream(ChatCompletionRequest c
252245
.filter(chatCompletionResponse -> chatCompletionResponse.type() != null);
253246
}
254247

248+
private void addDefaultHeadersIfMissing(HttpHeaders headers) {
249+
if (!headers.containsKey(HEADER_X_API_KEY)) {
250+
String apiKeyValue = this.apiKey.getValue();
251+
if (StringUtils.hasText(apiKeyValue)) {
252+
headers.add(HEADER_X_API_KEY, apiKeyValue);
253+
}
254+
}
255+
}
256+
255257
/**
256258
* Check the <a href="https://docs.anthropic.com/claude/docs/models-overview">Models
257259
* overview</a> and <a href=

models/spring-ai-anthropic/src/test/java/org/springframework/ai/anthropic/api/AnthropicApiBuilderTests.java

Lines changed: 25 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -196,12 +196,9 @@ void dynamicApiKeyRestClient() throws InterruptedException {
196196

197197
@Test
198198
void dynamicApiKeyRestClientWithAdditionalApiKeyHeader() throws InterruptedException {
199-
AnthropicApi api = AnthropicApi.builder()
200-
.apiKey(() -> {
201-
throw new AssertionFailedError("Should not be called, API key is provided in headers");
202-
})
203-
.baseUrl(mockWebServer.url("/").toString())
204-
.build();
199+
AnthropicApi api = AnthropicApi.builder().apiKey(() -> {
200+
throw new AssertionFailedError("Should not be called, API key is provided in headers");
201+
}).baseUrl(mockWebServer.url("/").toString()).build();
205202

206203
MockResponse mockResponse = new MockResponse().setResponseCode(200)
207204
.addHeader(HttpHeaders.CONTENT_TYPE, MediaType.APPLICATION_JSON_VALUE)
@@ -231,7 +228,8 @@ void dynamicApiKeyRestClientWithAdditionalApiKeyHeader() throws InterruptedExcep
231228
.build();
232229
MultiValueMap<String, String> additionalHeaders = new LinkedMultiValueMap<>();
233230
additionalHeaders.add("x-api-key", "additional-key");
234-
ResponseEntity<AnthropicApi.ChatCompletionResponse> response = api.chatCompletionEntity(request, additionalHeaders);
231+
ResponseEntity<AnthropicApi.ChatCompletionResponse> response = api.chatCompletionEntity(request,
232+
additionalHeaders);
235233
assertThat(response.getStatusCode()).isEqualTo(HttpStatus.OK);
236234
RecordedRequest recordedRequest = mockWebServer.takeRequest();
237235
assertThat(recordedRequest.getHeader(HttpHeaders.AUTHORIZATION)).isNull();
@@ -248,8 +246,7 @@ void dynamicApiKeyWebClient() throws InterruptedException {
248246

249247
MockResponse mockResponse = new MockResponse().setResponseCode(200)
250248
.addHeader(HttpHeaders.CONTENT_TYPE, MediaType.TEXT_EVENT_STREAM_VALUE)
251-
.setBody(
252-
"""
249+
.setBody("""
253250
{
254251
"type": "message_start",
255252
"message": {
@@ -278,9 +275,7 @@ void dynamicApiKeyWebClient() throws InterruptedException {
278275
.messages(List.of(chatCompletionMessage))
279276
.stream(true)
280277
.build();
281-
api.chatCompletionStream(request)
282-
.collectList()
283-
.block();
278+
api.chatCompletionStream(request).collectList().block();
284279
RecordedRequest recordedRequest = mockWebServer.takeRequest();
285280
assertThat(recordedRequest.getHeader(HttpHeaders.AUTHORIZATION)).isNull();
286281
assertThat(recordedRequest.getHeader("x-api-key")).isEqualTo("key1");
@@ -301,26 +296,25 @@ void dynamicApiKeyWebClientWithAdditionalApiKey() throws InterruptedException {
301296
.build();
302297

303298
MockResponse mockResponse = new MockResponse().setResponseCode(200)
304-
.addHeader(HttpHeaders.CONTENT_TYPE, MediaType.TEXT_EVENT_STREAM_VALUE)
305-
.setBody(
306-
"""
307-
{
308-
"type": "message_start",
309-
"message": {
310-
"id": "msg_1nZdL29xx5MUA1yADyHTEsnR8uuvGzszyY",
311-
"type": "message",
312-
"role": "assistant",
313-
"content": [],
314-
"model": "claude-opus-4-20250514",
315-
"stop_reason": null,
316-
"stop_sequence": null,
317-
"usage": {
318-
"input_tokens": 25,
319-
"output_tokens": 1
320-
}
299+
.addHeader(HttpHeaders.CONTENT_TYPE, MediaType.TEXT_EVENT_STREAM_VALUE)
300+
.setBody("""
301+
{
302+
"type": "message_start",
303+
"message": {
304+
"id": "msg_1nZdL29xx5MUA1yADyHTEsnR8uuvGzszyY",
305+
"type": "message",
306+
"role": "assistant",
307+
"content": [],
308+
"model": "claude-opus-4-20250514",
309+
"stop_reason": null,
310+
"stop_sequence": null,
311+
"usage": {
312+
"input_tokens": 25,
313+
"output_tokens": 1
321314
}
322315
}
323-
""".replace("\n", ""));
316+
}
317+
""".replace("\n", ""));
324318
mockWebServer.enqueue(mockResponse);
325319

326320
AnthropicApi.AnthropicMessage chatCompletionMessage = new AnthropicApi.AnthropicMessage(
@@ -334,9 +328,7 @@ void dynamicApiKeyWebClientWithAdditionalApiKey() throws InterruptedException {
334328
MultiValueMap<String, String> additionalHeaders = new LinkedMultiValueMap<>();
335329
additionalHeaders.add("x-api-key", "additional-key");
336330

337-
api.chatCompletionStream(request, additionalHeaders)
338-
.collectList()
339-
.block();
331+
api.chatCompletionStream(request, additionalHeaders).collectList().block();
340332
RecordedRequest recordedRequest = mockWebServer.takeRequest();
341333
assertThat(recordedRequest.getHeader(HttpHeaders.AUTHORIZATION)).isNull();
342334
assertThat(recordedRequest.getHeader("x-api-key")).isEqualTo("additional-key");

0 commit comments

Comments
 (0)