openai/openai-java
Publicmirrored from https://github.com/openai/openai-javaAvailable
openai-java-client-okhttp/src/main/kotlin/com/openai/client/okhttp/OpenAIOkHttpClient.kt
160lines · modecode
| 1 | // File generated from our OpenAPI spec by Stainless. |
| 2 | |
| 3 | package com.openai.client.okhttp |
| 4 | |
| 5 | import com.fasterxml.jackson.databind.json.JsonMapper |
| 6 | import com.openai.azure.AzureOpenAIServiceVersion |
| 7 | import com.openai.client.OpenAIClient |
| 8 | import com.openai.client.OpenAIClientImpl |
| 9 | import com.openai.core.ClientOptions |
| 10 | import com.openai.core.http.Headers |
| 11 | import com.openai.core.http.QueryParams |
| 12 | import com.openai.credential.Credential |
| 13 | import java.net.Proxy |
| 14 | import java.time.Clock |
| 15 | import java.time.Duration |
| 16 | |
| 17 | class OpenAIOkHttpClient private constructor() { |
| 18 | |
| 19 | companion object { |
| 20 | |
| 21 | @JvmStatic fun builder() = Builder() |
| 22 | |
| 23 | @JvmStatic fun fromEnv(): OpenAIClient = builder().fromEnv().build() |
| 24 | } |
| 25 | |
| 26 | class Builder { |
| 27 | |
| 28 | private var clientOptions: ClientOptions.Builder = ClientOptions.builder() |
| 29 | private var baseUrl: String = ClientOptions.PRODUCTION_URL |
| 30 | // The default timeout for the client is 10 minutes. |
| 31 | private var timeout: Duration = Duration.ofSeconds(600) |
| 32 | private var proxy: Proxy? = null |
| 33 | |
| 34 | fun baseUrl(baseUrl: String) = apply { |
| 35 | clientOptions.baseUrl(baseUrl) |
| 36 | this.baseUrl = baseUrl |
| 37 | } |
| 38 | |
| 39 | fun jsonMapper(jsonMapper: JsonMapper) = apply { clientOptions.jsonMapper(jsonMapper) } |
| 40 | |
| 41 | fun clock(clock: Clock) = apply { clientOptions.clock(clock) } |
| 42 | |
| 43 | fun headers(headers: Headers) = apply { clientOptions.headers(headers) } |
| 44 | |
| 45 | fun headers(headers: Map<String, Iterable<String>>) = apply { |
| 46 | clientOptions.headers(headers) |
| 47 | } |
| 48 | |
| 49 | fun putHeader(name: String, value: String) = apply { clientOptions.putHeader(name, value) } |
| 50 | |
| 51 | fun putHeaders(name: String, values: Iterable<String>) = apply { |
| 52 | clientOptions.putHeaders(name, values) |
| 53 | } |
| 54 | |
| 55 | fun putAllHeaders(headers: Headers) = apply { clientOptions.putAllHeaders(headers) } |
| 56 | |
| 57 | fun putAllHeaders(headers: Map<String, Iterable<String>>) = apply { |
| 58 | clientOptions.putAllHeaders(headers) |
| 59 | } |
| 60 | |
| 61 | fun replaceHeaders(name: String, value: String) = apply { |
| 62 | clientOptions.replaceHeaders(name, value) |
| 63 | } |
| 64 | |
| 65 | fun replaceHeaders(name: String, values: Iterable<String>) = apply { |
| 66 | clientOptions.replaceHeaders(name, values) |
| 67 | } |
| 68 | |
| 69 | fun replaceAllHeaders(headers: Headers) = apply { clientOptions.replaceAllHeaders(headers) } |
| 70 | |
| 71 | fun replaceAllHeaders(headers: Map<String, Iterable<String>>) = apply { |
| 72 | clientOptions.replaceAllHeaders(headers) |
| 73 | } |
| 74 | |
| 75 | fun removeHeaders(name: String) = apply { clientOptions.removeHeaders(name) } |
| 76 | |
| 77 | fun removeAllHeaders(names: Set<String>) = apply { clientOptions.removeAllHeaders(names) } |
| 78 | |
| 79 | fun queryParams(queryParams: QueryParams) = apply { clientOptions.queryParams(queryParams) } |
| 80 | |
| 81 | fun queryParams(queryParams: Map<String, Iterable<String>>) = apply { |
| 82 | clientOptions.queryParams(queryParams) |
| 83 | } |
| 84 | |
| 85 | fun putQueryParam(key: String, value: String) = apply { |
| 86 | clientOptions.putQueryParam(key, value) |
| 87 | } |
| 88 | |
| 89 | fun putQueryParams(key: String, values: Iterable<String>) = apply { |
| 90 | clientOptions.putQueryParams(key, values) |
| 91 | } |
| 92 | |
| 93 | fun putAllQueryParams(queryParams: QueryParams) = apply { |
| 94 | clientOptions.putAllQueryParams(queryParams) |
| 95 | } |
| 96 | |
| 97 | fun putAllQueryParams(queryParams: Map<String, Iterable<String>>) = apply { |
| 98 | clientOptions.putAllQueryParams(queryParams) |
| 99 | } |
| 100 | |
| 101 | fun replaceQueryParams(key: String, value: String) = apply { |
| 102 | clientOptions.replaceQueryParams(key, value) |
| 103 | } |
| 104 | |
| 105 | fun replaceQueryParams(key: String, values: Iterable<String>) = apply { |
| 106 | clientOptions.replaceQueryParams(key, values) |
| 107 | } |
| 108 | |
| 109 | fun replaceAllQueryParams(queryParams: QueryParams) = apply { |
| 110 | clientOptions.replaceAllQueryParams(queryParams) |
| 111 | } |
| 112 | |
| 113 | fun replaceAllQueryParams(queryParams: Map<String, Iterable<String>>) = apply { |
| 114 | clientOptions.replaceAllQueryParams(queryParams) |
| 115 | } |
| 116 | |
| 117 | fun removeQueryParams(key: String) = apply { clientOptions.removeQueryParams(key) } |
| 118 | |
| 119 | fun removeAllQueryParams(keys: Set<String>) = apply { |
| 120 | clientOptions.removeAllQueryParams(keys) |
| 121 | } |
| 122 | |
| 123 | fun timeout(timeout: Duration) = apply { this.timeout = timeout } |
| 124 | |
| 125 | fun maxRetries(maxRetries: Int) = apply { clientOptions.maxRetries(maxRetries) } |
| 126 | |
| 127 | fun proxy(proxy: Proxy) = apply { this.proxy = proxy } |
| 128 | |
| 129 | fun responseValidation(responseValidation: Boolean) = apply { |
| 130 | clientOptions.responseValidation(responseValidation) |
| 131 | } |
| 132 | |
| 133 | fun apiKey(apiKey: String) = apply { clientOptions.apiKey(apiKey) } |
| 134 | |
| 135 | fun credential(credential: Credential) = apply { clientOptions.credential(credential) } |
| 136 | |
| 137 | fun azureServiceVersion(azureServiceVersion: AzureOpenAIServiceVersion) = apply { |
| 138 | clientOptions.azureServiceVersion(azureServiceVersion) |
| 139 | } |
| 140 | |
| 141 | fun organization(organization: String?) = apply { clientOptions.organization(organization) } |
| 142 | |
| 143 | fun project(project: String?) = apply { clientOptions.project(project) } |
| 144 | |
| 145 | fun fromEnv() = apply { clientOptions.fromEnv() } |
| 146 | |
| 147 | fun build(): OpenAIClient = |
| 148 | OpenAIClientImpl( |
| 149 | clientOptions |
| 150 | .httpClient( |
| 151 | OkHttpClient.builder() |
| 152 | .baseUrl(baseUrl) |
| 153 | .timeout(timeout) |
| 154 | .proxy(proxy) |
| 155 | .build() |
| 156 | ) |
| 157 | .build() |
| 158 | ) |
| 159 | } |
| 160 | } |
| 161 | |