Skip to content

Commit 33e6e3d

Browse files
authored
fix(requestOptions): request options headers override previous values (#353)
1 parent 8aab15e commit 33e6e3d

File tree

3 files changed

+32
-1
lines changed

3 files changed

+32
-1
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66

77
### Fixed
88
- **chat**: enhance flow cancel capability (#333)
9+
- **config**: request options headers override previous values (#353)
910

1011
## 3.7.2
1112
> Published 28 Apr 2024

openai-client/src/commonMain/kotlin/com.aallam.openai.client/internal/extension/HttpRequestBuilder.kt

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,10 @@ import io.ktor.client.request.*
99
*/
1010
internal fun HttpRequestBuilder.requestOptions(requestOptions: RequestOptions? = null) {
1111
if (requestOptions == null) return
12-
requestOptions.headers.forEach { (key, value) -> headers.append(key, value) }
12+
requestOptions.headers.forEach { (key, value) ->
13+
if (headers.contains(key)) headers.remove(key)
14+
headers[key] = value
15+
}
1316
requestOptions.urlParameters.forEach { (key, value) -> url.parameters.append(key, value) }
1417
requestOptions.timeout?.let { timeout ->
1518
timeout {

openai-client/src/commonTest/kotlin/com/aallam/openai/client/misc/TestRequestOptions.kt

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,4 +32,31 @@ class TestRequestOptions {
3232
client.models(requestOptions = requestOptions)
3333
assertEquals(requestHeaders?.get(key), requestOptions.headers[key])
3434
}
35+
36+
@Test
37+
fun testOverride() = runTest {
38+
val key = "OpenAI-Beta"
39+
val requestOptions = RequestOptions(
40+
headers = mapOf(key to "assistants=v0"),
41+
)
42+
var requestHeaders: Map<String, String>? = null
43+
val client = OpenAI(
44+
token = token,
45+
headers = mapOf(key to "assistants=v3"),
46+
httpClientConfig = {
47+
install("RequestInterceptor") {
48+
requestPipeline.intercept(HttpRequestPipeline.Before) {
49+
requestHeaders = context.headers.entries().associate { it.key to it.value.first() }
50+
}
51+
}
52+
}
53+
)
54+
55+
try {
56+
client.assistants(requestOptions = requestOptions)
57+
} catch (e: Exception) {
58+
// skip
59+
}
60+
assertEquals(requestHeaders?.get(key), requestOptions.headers[key])
61+
}
3562
}

0 commit comments

Comments
 (0)