Skip to content

Commit 906a2a5

Browse files
fix(client): allow updating header/query affecting fields in toBuilder()
1 parent 79fa3e3 commit 906a2a5

2 files changed

Lines changed: 26 additions & 3 deletions

File tree

sent-dm-java-core/src/main/kotlin/dm/sent/core/ClientOptions.kt

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -411,13 +411,14 @@ private constructor(
411411
headers.put("X-Stainless-Runtime", "JRE")
412412
headers.put("X-Stainless-Runtime-Version", getJavaVersion())
413413
headers.put("X-Stainless-Kotlin-Version", KotlinVersion.CURRENT.toString())
414+
// We replace after all the default headers to allow end-users to overwrite them.
415+
headers.replaceAll(this.headers.build())
416+
queryParams.replaceAll(this.queryParams.build())
414417
apiKey.let {
415418
if (!it.isEmpty()) {
416-
headers.put("x-api-key", it)
419+
headers.replace("x-api-key", it)
417420
}
418421
}
419-
headers.replaceAll(this.headers.build())
420-
queryParams.replaceAll(this.queryParams.build())
421422

422423
return ClientOptions(
423424
httpClient,

sent-dm-java-core/src/test/kotlin/dm/sent/core/ClientOptionsTest.kt

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,28 @@ internal class ClientOptionsTest {
1616

1717
private val httpClient = mock<HttpClient>()
1818

19+
@Test
20+
fun putHeader_canOverwriteDefaultHeader() {
21+
val clientOptions =
22+
ClientOptions.builder()
23+
.httpClient(httpClient)
24+
.putHeader("User-Agent", "My User Agent")
25+
.apiKey("My API Key")
26+
.build()
27+
28+
assertThat(clientOptions.headers.values("User-Agent")).containsExactly("My User Agent")
29+
}
30+
31+
@Test
32+
fun toBuilder_customerApiKeyCanBeUpdated() {
33+
var clientOptions =
34+
ClientOptions.builder().httpClient(httpClient).apiKey("My API Key").build()
35+
36+
clientOptions = clientOptions.toBuilder().apiKey("another My API Key").build()
37+
38+
assertThat(clientOptions.headers.values("x-api-key")).containsExactly("another My API Key")
39+
}
40+
1941
@Test
2042
fun toBuilder_whenOriginalClientOptionsGarbageCollected_doesNotCloseOriginalClient() {
2143
var clientOptions =

0 commit comments

Comments
 (0)