openai/openai-java

Public

mirrored from https://github.com/openai/openai-javaAvailable

CodeCommitsIssuesPull requestsActionsInsightsSecurity
c077dca57103290656bb5aa23ef1e81dc0cd5756

Branches

Tags

  • No tags available.
0Branches0Tags
Go to file
Add file
Code

Clone

HTTPS

Download ZIP

openai-java-client-okhttp/src/main/kotlin/com/openai/client/okhttp/OpenAIOkHttpClient.kt

152lines · modecode

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