Skip to content

Commit 5069881

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

2 files changed

Lines changed: 36 additions & 6 deletions

File tree

langsmith-java-core/src/main/kotlin/com/langchain/smith/core/ClientOptions.kt

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -503,28 +503,29 @@ private constructor(
503503
headers.put("X-Stainless-Runtime", "JRE")
504504
headers.put("X-Stainless-Runtime-Version", getJavaVersion())
505505
headers.put("X-Stainless-Kotlin-Version", KotlinVersion.CURRENT.toString())
506+
// We replace after all the default headers to allow end-users to overwrite them.
507+
headers.replaceAll(this.headers.build())
508+
queryParams.replaceAll(this.queryParams.build())
506509
apiKey?.let {
507510
if (!it.isEmpty()) {
508-
headers.put("X-API-Key", it)
511+
headers.replace("X-API-Key", it)
509512
}
510513
}
511514
organizationId?.let {
512515
if (!it.isEmpty()) {
513-
headers.put("X-Organization-Id", it)
516+
headers.replace("X-Organization-Id", it)
514517
}
515518
}
516519
bearerToken?.let {
517520
if (!it.isEmpty()) {
518-
headers.put("Authorization", "Bearer $it")
521+
headers.replace("Authorization", "Bearer $it")
519522
}
520523
}
521524
tenantId?.let {
522525
if (!it.isEmpty()) {
523-
headers.put("X-Tenant-Id", it)
526+
headers.replace("X-Tenant-Id", it)
524527
}
525528
}
526-
headers.replaceAll(this.headers.build())
527-
queryParams.replaceAll(this.queryParams.build())
528529

529530
return ClientOptions(
530531
httpClient,

langsmith-java-core/src/test/kotlin/com/langchain/smith/core/ClientOptionsTest.kt

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,35 @@ 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+
.tenantId("My Tenant ID")
27+
.organizationId("My Organization ID")
28+
.build()
29+
30+
assertThat(clientOptions.headers.values("User-Agent")).containsExactly("My User Agent")
31+
}
32+
33+
@Test
34+
fun toBuilder_apiKeyCanBeUpdated() {
35+
var clientOptions =
36+
ClientOptions.builder()
37+
.httpClient(httpClient)
38+
.apiKey("My API Key")
39+
.tenantId("My Tenant ID")
40+
.organizationId("My Organization ID")
41+
.build()
42+
43+
clientOptions = clientOptions.toBuilder().apiKey("another My API Key").build()
44+
45+
assertThat(clientOptions.headers.values("X-API-Key")).containsExactly("another My API Key")
46+
}
47+
1948
@Test
2049
fun toBuilder_whenOriginalClientOptionsGarbageCollected_doesNotCloseOriginalClient() {
2150
var clientOptions =

0 commit comments

Comments
 (0)