Skip to content

Commit 17c55c1

Browse files
fix(client): allow updating header/query affecting fields in toBuilder()
1 parent 50f96cd commit 17c55c1

2 files changed

Lines changed: 27 additions & 3 deletions

File tree

lithic-java-core/src/main/kotlin/com/lithic/api/core/ClientOptions.kt

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -476,13 +476,14 @@ private constructor(
476476
headers.put("X-Stainless-Runtime", "JRE")
477477
headers.put("X-Stainless-Runtime-Version", getJavaVersion())
478478
headers.put("X-Stainless-Kotlin-Version", KotlinVersion.CURRENT.toString())
479+
// We replace after all the default headers to allow end-users to overwrite them.
480+
headers.replaceAll(this.headers.build())
481+
queryParams.replaceAll(this.queryParams.build())
479482
apiKey.let {
480483
if (!it.isEmpty()) {
481-
headers.put("Authorization", it)
484+
headers.replace("Authorization", it)
482485
}
483486
}
484-
headers.replaceAll(this.headers.build())
485-
queryParams.replaceAll(this.queryParams.build())
486487

487488
return ClientOptions(
488489
httpClient,

lithic-java-core/src/test/kotlin/com/lithic/api/core/ClientOptionsTest.kt

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,29 @@ 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 Lithic API Key")
26+
.build()
27+
28+
assertThat(clientOptions.headers.values("User-Agent")).containsExactly("My User Agent")
29+
}
30+
31+
@Test
32+
fun toBuilder_apiKeyAuthCanBeUpdated() {
33+
var clientOptions =
34+
ClientOptions.builder().httpClient(httpClient).apiKey("My Lithic API Key").build()
35+
36+
clientOptions = clientOptions.toBuilder().apiKey("another My Lithic API Key").build()
37+
38+
assertThat(clientOptions.headers.values("Authorization"))
39+
.containsExactly("another My Lithic API Key")
40+
}
41+
1942
@Test
2043
fun toBuilder_whenOriginalClientOptionsGarbageCollected_doesNotCloseOriginalClient() {
2144
var clientOptions =

0 commit comments

Comments
 (0)