Skip to content

Commit

Permalink
fix(requestOptions): headers override previous values
Browse files Browse the repository at this point in the history
  • Loading branch information
aallam committed Jun 15, 2024
1 parent 8aab15e commit 3bbf499
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,10 @@ import io.ktor.client.request.*
*/
internal fun HttpRequestBuilder.requestOptions(requestOptions: RequestOptions? = null) {
if (requestOptions == null) return
requestOptions.headers.forEach { (key, value) -> headers.append(key, value) }
requestOptions.headers.forEach { (key, value) ->
if (headers.contains(key)) headers.remove(key)
headers[key] = value
}
requestOptions.urlParameters.forEach { (key, value) -> url.parameters.append(key, value) }
requestOptions.timeout?.let { timeout ->
timeout {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,4 +32,31 @@ class TestRequestOptions {
client.models(requestOptions = requestOptions)
assertEquals(requestHeaders?.get(key), requestOptions.headers[key])
}

@Test
fun testOverride() = runTest {
val key = "OpenAI-Beta"
val requestOptions = RequestOptions(
headers = mapOf(key to "assistants=v0"),
)
var requestHeaders: Map<String, String>? = null
val client = OpenAI(
token = token,
headers = mapOf(key to "assistants=v3"),
httpClientConfig = {
install("RequestInterceptor") {
requestPipeline.intercept(HttpRequestPipeline.Before) {
requestHeaders = context.headers.entries().associate { it.key to it.value.first() }
}
}
}
)

try {
client.assistants(requestOptions = requestOptions)
} catch (e: Exception) {
// skip
}
assertEquals(requestHeaders?.get(key), requestOptions.headers[key])
}
}

0 comments on commit 3bbf499

Please sign in to comment.