Skip to content

Commit f2b664a

Browse files
fix(client): allow updating header/query affecting fields in toBuilder()
1 parent bab2317 commit f2b664a

File tree

2 files changed

+27
-3
lines changed

2 files changed

+27
-3
lines changed

brand-dev-java-core/src/main/kotlin/com/branddev/api/core/ClientOptions.kt

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -403,13 +403,14 @@ private constructor(
403403
headers.put("X-Stainless-Runtime", "JRE")
404404
headers.put("X-Stainless-Runtime-Version", getJavaVersion())
405405
headers.put("X-Stainless-Kotlin-Version", KotlinVersion.CURRENT.toString())
406+
// We replace after all the default headers to allow end-users to overwrite them.
407+
headers.replaceAll(this.headers.build())
408+
queryParams.replaceAll(this.queryParams.build())
406409
apiKey.let {
407410
if (!it.isEmpty()) {
408-
headers.put("Authorization", "Bearer $it")
411+
headers.replace("Authorization", "Bearer $it")
409412
}
410413
}
411-
headers.replaceAll(this.headers.build())
412-
queryParams.replaceAll(this.queryParams.build())
413414

414415
return ClientOptions(
415416
httpClient,

brand-dev-java-core/src/test/kotlin/com/branddev/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 API Key")
26+
.build()
27+
28+
assertThat(clientOptions.headers.values("User-Agent")).containsExactly("My User Agent")
29+
}
30+
31+
@Test
32+
fun toBuilder_bearerAuthCanBeUpdated() {
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("Authorization"))
39+
.containsExactly("Bearer another My API Key")
40+
}
41+
1942
@Test
2043
fun toBuilder_whenOriginalClientOptionsGarbageCollected_doesNotCloseOriginalClient() {
2144
var clientOptions =

0 commit comments

Comments
 (0)