Skip to content

Commit

Permalink
feat(azure): Add HttpRequest.Builder extension methods (#9)
Browse files Browse the repository at this point in the history
* Added HttpRequest.Builder extension methods

* remove unused imports

---------

Co-authored-by: Robert Craigie <robert@craigie.dev>
  • Loading branch information
2 people authored and stainless-app[bot] committed Nov 21, 2024
1 parent d9f6033 commit 5b774cd
Show file tree
Hide file tree
Showing 9 changed files with 59 additions and 30 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
package com.openai.azure

import com.openai.core.ClientOptions
import com.openai.core.http.HttpRequest
import com.openai.core.isAzureEndpoint
import com.openai.credential.BearerTokenCredential

@JvmSynthetic
internal fun HttpRequest.Builder.addPathSegmentsForAzure(
clientOptions: ClientOptions,
deploymentModel: String
): HttpRequest.Builder = apply {
if (isAzureEndpoint(clientOptions.baseUrl)) {
addPathSegments("openai", "deployments", deploymentModel)
}
}

@JvmSynthetic
internal fun HttpRequest.Builder.replaceBearerTokenForAzure(
clientOptions: ClientOptions
): HttpRequest.Builder = apply {
if (
isAzureEndpoint(clientOptions.baseUrl) && clientOptions.credential is BearerTokenCredential
) {
replaceHeaders("Authorization", "Bearer ${clientOptions.credential.token()}")
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

package com.openai.services.async

import com.openai.azure.addPathSegmentsForAzure
import com.openai.azure.replaceBearerTokenForAzure
import com.openai.core.ClientOptions
import com.openai.core.JsonValue
import com.openai.core.RequestOptions
Expand Down Expand Up @@ -41,10 +43,12 @@ constructor(
val request =
HttpRequest.builder()
.method(HttpMethod.POST)
.addPathSegmentsForAzure(clientOptions, params.model().toString())
.addPathSegments("completions")
.putAllQueryParams(clientOptions.queryParams)
.replaceAllQueryParams(params.getQueryParams())
.putAllHeaders(clientOptions.headers)
.replaceBearerTokenForAzure(clientOptions)
.replaceAllHeaders(params.getHeaders())
.body(json(clientOptions.jsonMapper, params.getBody()))
.build()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

package com.openai.services.async

import com.openai.azure.addPathSegmentsForAzure
import com.openai.azure.replaceBearerTokenForAzure
import com.openai.core.ClientOptions
import com.openai.core.RequestOptions
import com.openai.core.handlers.errorHandler
Expand Down Expand Up @@ -35,10 +37,12 @@ constructor(
val request =
HttpRequest.builder()
.method(HttpMethod.POST)
.addPathSegmentsForAzure(clientOptions, params.model().toString())
.addPathSegments("embeddings")
.putAllQueryParams(clientOptions.queryParams)
.replaceAllQueryParams(params.getQueryParams())
.putAllHeaders(clientOptions.headers)
.replaceBearerTokenForAzure(clientOptions)
.replaceAllHeaders(params.getHeaders())
.body(json(clientOptions.jsonMapper, params.getBody()))
.build()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

package com.openai.services.async

import com.openai.azure.addPathSegmentsForAzure
import com.openai.azure.replaceBearerTokenForAzure
import com.openai.core.ClientOptions
import com.openai.core.RequestOptions
import com.openai.core.handlers.errorHandler
Expand Down Expand Up @@ -34,10 +36,12 @@ constructor(
val request =
HttpRequest.builder()
.method(HttpMethod.POST)
.addPathSegmentsForAzure(clientOptions, params.model().get().toString())
.addPathSegments("images", "generations")
.putAllQueryParams(clientOptions.queryParams)
.replaceAllQueryParams(params.getQueryParams())
.putAllHeaders(clientOptions.headers)
.replaceBearerTokenForAzure(clientOptions)
.replaceAllHeaders(params.getHeaders())
.body(json(clientOptions.jsonMapper, params.getBody()))
.build()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

package com.openai.services.async.chat

import com.openai.azure.addPathSegmentsForAzure
import com.openai.azure.replaceBearerTokenForAzure
import com.openai.core.ClientOptions
import com.openai.core.JsonValue
import com.openai.core.RequestOptions
Expand All @@ -17,9 +19,7 @@ import com.openai.core.http.HttpRequest
import com.openai.core.http.HttpResponse.Handler
import com.openai.core.http.StreamResponse
import com.openai.core.http.toAsync
import com.openai.core.isAzureEndpoint
import com.openai.core.json
import com.openai.credential.BearerTokenCredential
import com.openai.errors.OpenAIError
import com.openai.models.ChatCompletion
import com.openai.models.ChatCompletionChunk
Expand Down Expand Up @@ -49,23 +49,12 @@ constructor(
val request =
HttpRequest.builder()
.method(HttpMethod.POST)
.apply {
if (isAzureEndpoint(clientOptions.baseUrl)) {
addPathSegments("openai", "deployments", params.model().toString())
}
}
.addPathSegmentsForAzure(clientOptions, params.model().toString())
.addPathSegments("chat", "completions")
.putAllQueryParams(clientOptions.queryParams)
.replaceAllQueryParams(params.getQueryParams())
.putAllHeaders(clientOptions.headers)
.apply {
if (
isAzureEndpoint(clientOptions.baseUrl) &&
clientOptions.credential is BearerTokenCredential
) {
putHeader("Authorization", "Bearer ${clientOptions.credential.token()}")
}
}
.replaceBearerTokenForAzure(clientOptions)
.replaceAllHeaders(params.getHeaders())
.body(json(clientOptions.jsonMapper, params.getBody()))
.build()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

package com.openai.services.blocking

import com.openai.azure.addPathSegmentsForAzure
import com.openai.azure.replaceBearerTokenForAzure
import com.openai.core.ClientOptions
import com.openai.core.JsonValue
import com.openai.core.RequestOptions
Expand Down Expand Up @@ -38,10 +40,12 @@ constructor(
val request =
HttpRequest.builder()
.method(HttpMethod.POST)
.addPathSegmentsForAzure(clientOptions, params.model().toString())
.addPathSegments("completions")
.putAllQueryParams(clientOptions.queryParams)
.replaceAllQueryParams(params.getQueryParams())
.putAllHeaders(clientOptions.headers)
.replaceBearerTokenForAzure(clientOptions)
.replaceAllHeaders(params.getHeaders())
.body(json(clientOptions.jsonMapper, params.getBody()))
.build()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

package com.openai.services.blocking

import com.openai.azure.addPathSegmentsForAzure
import com.openai.azure.replaceBearerTokenForAzure
import com.openai.core.ClientOptions
import com.openai.core.RequestOptions
import com.openai.core.handlers.errorHandler
Expand Down Expand Up @@ -34,10 +36,12 @@ constructor(
val request =
HttpRequest.builder()
.method(HttpMethod.POST)
.addPathSegmentsForAzure(clientOptions, params.model().toString())
.addPathSegments("embeddings")
.putAllQueryParams(clientOptions.queryParams)
.replaceAllQueryParams(params.getQueryParams())
.putAllHeaders(clientOptions.headers)
.replaceBearerTokenForAzure(clientOptions)
.replaceAllHeaders(params.getHeaders())
.body(json(clientOptions.jsonMapper, params.getBody()))
.build()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

package com.openai.services.blocking

import com.openai.azure.addPathSegmentsForAzure
import com.openai.azure.replaceBearerTokenForAzure
import com.openai.core.ClientOptions
import com.openai.core.RequestOptions
import com.openai.core.handlers.errorHandler
Expand Down Expand Up @@ -33,10 +35,12 @@ constructor(
val request =
HttpRequest.builder()
.method(HttpMethod.POST)
.addPathSegmentsForAzure(clientOptions, params.model().get().toString())
.addPathSegments("images", "generations")
.putAllQueryParams(clientOptions.queryParams)
.replaceAllQueryParams(params.getQueryParams())
.putAllHeaders(clientOptions.headers)
.replaceBearerTokenForAzure(clientOptions)
.replaceAllHeaders(params.getHeaders())
.body(json(clientOptions.jsonMapper, params.getBody()))
.build()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

package com.openai.services.blocking.chat

import com.openai.azure.addPathSegmentsForAzure
import com.openai.azure.replaceBearerTokenForAzure
import com.openai.core.ClientOptions
import com.openai.core.JsonValue
import com.openai.core.RequestOptions
Expand All @@ -15,9 +17,7 @@ import com.openai.core.http.HttpMethod
import com.openai.core.http.HttpRequest
import com.openai.core.http.HttpResponse.Handler
import com.openai.core.http.StreamResponse
import com.openai.core.isAzureEndpoint
import com.openai.core.json
import com.openai.credential.BearerTokenCredential
import com.openai.errors.OpenAIError
import com.openai.models.ChatCompletion
import com.openai.models.ChatCompletionChunk
Expand Down Expand Up @@ -46,23 +46,12 @@ constructor(
val request =
HttpRequest.builder()
.method(HttpMethod.POST)
.apply {
if (isAzureEndpoint(clientOptions.baseUrl)) {
addPathSegments("openai", "deployments", params.model().toString())
}
}
.addPathSegmentsForAzure(clientOptions, params.model().toString())
.addPathSegments("chat", "completions")
.putAllQueryParams(clientOptions.queryParams)
.replaceAllQueryParams(params.getQueryParams())
.putAllHeaders(clientOptions.headers)
.apply {
if (
isAzureEndpoint(clientOptions.baseUrl) &&
clientOptions.credential is BearerTokenCredential
) {
putHeader("Authorization", "Bearer ${clientOptions.credential.token()}")
}
}
.replaceBearerTokenForAzure(clientOptions)
.replaceAllHeaders(params.getHeaders())
.body(json(clientOptions.jsonMapper, params.getBody()))
.build()
Expand Down

0 comments on commit 5b774cd

Please sign in to comment.