From 1ffe68728d039ebde65c882c69abe112722baf25 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jose=CC=81=20Carlos=20Montan=CC=83ez?= Date: Mon, 4 Sep 2023 16:38:58 +0200 Subject: [PATCH] some refactor --- .../xef/server/http/routes/Routes.kt | 29 ++++++++++--------- 1 file changed, 15 insertions(+), 14 deletions(-) diff --git a/server/src/main/kotlin/com/xebia/functional/xef/server/http/routes/Routes.kt b/server/src/main/kotlin/com/xebia/functional/xef/server/http/routes/Routes.kt index 5bbaa9f71..672bdf692 100644 --- a/server/src/main/kotlin/com/xebia/functional/xef/server/http/routes/Routes.kt +++ b/server/src/main/kotlin/com/xebia/functional/xef/server/http/routes/Routes.kt @@ -78,15 +78,10 @@ private suspend fun HttpClient.makeRequest( call: ApplicationCall, url: String, body: String, - token: String) -{ + token: String +) { val response = this.request(url) { - headers { - bearerAuth(token) - } - contentType(ContentType.Application.Json) - method = HttpMethod.Post - setBody(body) + setHttpRequestBuilder(token, body) } call.respond(response.status, response.body()) } @@ -98,12 +93,7 @@ private suspend fun HttpClient.makeStreaming( token: String ) { this.preparePost(url) { - headers { - bearerAuth(token) - } - contentType(ContentType.Application.Json) - method = HttpMethod.Post - setBody(body) + setHttpRequestBuilder(token, body) }.execute { httpResponse -> val channel: ByteReadChannel = httpResponse.body() call.respondBytesWriter( @@ -121,6 +111,17 @@ private suspend fun HttpClient.makeStreaming( } } +private fun setHttpRequestBuilder(token: String, body: String): HttpRequestBuilder = + HttpRequestBuilder().apply { + headers { + bearerAuth(token) + } + contentType(ContentType.Application.Json) + method = HttpMethod.Post + setBody(body) + } + + private fun ApplicationCall.getProvider(): Provider = request.headers["xef-provider"]?.toProvider() ?: Provider.OPENAI