|
| 1 | +/** |
| 2 | + * This file was auto-generated by Fern from our API Definition. |
| 3 | + */ |
| 4 | +package com.deepgram.core; |
| 5 | + |
| 6 | +import java.util.HashMap; |
| 7 | +import java.util.Map; |
| 8 | +import java.util.Optional; |
| 9 | +import java.util.concurrent.TimeUnit; |
| 10 | +import java.util.function.Supplier; |
| 11 | +import okhttp3.OkHttpClient; |
| 12 | + |
| 13 | +public final class ClientOptions { |
| 14 | + private final Environment environment; |
| 15 | + |
| 16 | + private final Map<String, String> headers; |
| 17 | + |
| 18 | + private final Map<String, Supplier<String>> headerSuppliers; |
| 19 | + |
| 20 | + private final OkHttpClient httpClient; |
| 21 | + |
| 22 | + private final int timeout; |
| 23 | + |
| 24 | + private final int maxRetries; |
| 25 | + |
| 26 | + private final Optional<WebSocketFactory> webSocketFactory; |
| 27 | + |
| 28 | + private final Optional<LogConfig> logging; |
| 29 | + |
| 30 | + private ClientOptions( |
| 31 | + Environment environment, |
| 32 | + Map<String, String> headers, |
| 33 | + Map<String, Supplier<String>> headerSuppliers, |
| 34 | + OkHttpClient httpClient, |
| 35 | + int timeout, |
| 36 | + int maxRetries, |
| 37 | + Optional<WebSocketFactory> webSocketFactory, |
| 38 | + Optional<LogConfig> logging) { |
| 39 | + this.environment = environment; |
| 40 | + this.headers = new HashMap<>(); |
| 41 | + this.headers.putAll(headers); |
| 42 | + this.headers.putAll(new HashMap<String, String>() { |
| 43 | + { |
| 44 | + put("User-Agent", "com.deepgram:deepgram-java-sdk/0.4.0"); // x-release-please-version |
| 45 | + put("X-Fern-Language", "JAVA"); |
| 46 | + put("X-Fern-SDK-Name", "com.deepgram:deepgram-java-sdk"); |
| 47 | + put("X-Fern-SDK-Version", "0.4.0"); // x-release-please-version |
| 48 | + } |
| 49 | + }); |
| 50 | + this.headerSuppliers = headerSuppliers; |
| 51 | + this.httpClient = httpClient; |
| 52 | + this.timeout = timeout; |
| 53 | + this.maxRetries = maxRetries; |
| 54 | + this.webSocketFactory = webSocketFactory; |
| 55 | + this.logging = logging; |
| 56 | + } |
| 57 | + |
| 58 | + public Environment environment() { |
| 59 | + return this.environment; |
| 60 | + } |
| 61 | + |
| 62 | + public Map<String, String> headers(RequestOptions requestOptions) { |
| 63 | + Map<String, String> values = new HashMap<>(this.headers); |
| 64 | + headerSuppliers.forEach((key, supplier) -> { |
| 65 | + values.put(key, supplier.get()); |
| 66 | + }); |
| 67 | + if (requestOptions != null) { |
| 68 | + values.putAll(requestOptions.getHeaders()); |
| 69 | + } |
| 70 | + return values; |
| 71 | + } |
| 72 | + |
| 73 | + public int timeout(RequestOptions requestOptions) { |
| 74 | + if (requestOptions == null) { |
| 75 | + return this.timeout; |
| 76 | + } |
| 77 | + return requestOptions.getTimeout().orElse(this.timeout); |
| 78 | + } |
| 79 | + |
| 80 | + public OkHttpClient httpClient() { |
| 81 | + return this.httpClient; |
| 82 | + } |
| 83 | + |
| 84 | + public OkHttpClient httpClientWithTimeout(RequestOptions requestOptions) { |
| 85 | + if (requestOptions == null) { |
| 86 | + return this.httpClient; |
| 87 | + } |
| 88 | + return this.httpClient |
| 89 | + .newBuilder() |
| 90 | + .callTimeout(requestOptions.getTimeout().get(), requestOptions.getTimeoutTimeUnit()) |
| 91 | + .connectTimeout(0, TimeUnit.SECONDS) |
| 92 | + .writeTimeout(0, TimeUnit.SECONDS) |
| 93 | + .readTimeout(0, TimeUnit.SECONDS) |
| 94 | + .build(); |
| 95 | + } |
| 96 | + |
| 97 | + public int maxRetries() { |
| 98 | + return this.maxRetries; |
| 99 | + } |
| 100 | + |
| 101 | + public Optional<WebSocketFactory> webSocketFactory() { |
| 102 | + return this.webSocketFactory; |
| 103 | + } |
| 104 | + |
| 105 | + public Optional<LogConfig> logging() { |
| 106 | + return this.logging; |
| 107 | + } |
| 108 | + |
| 109 | + public static Builder builder() { |
| 110 | + return new Builder(); |
| 111 | + } |
| 112 | + |
| 113 | + public static class Builder { |
| 114 | + private Environment environment; |
| 115 | + |
| 116 | + private final Map<String, String> headers = new HashMap<>(); |
| 117 | + |
| 118 | + private final Map<String, Supplier<String>> headerSuppliers = new HashMap<>(); |
| 119 | + |
| 120 | + private int maxRetries = 2; |
| 121 | + |
| 122 | + private Optional<Integer> timeout = Optional.empty(); |
| 123 | + |
| 124 | + private OkHttpClient httpClient = null; |
| 125 | + |
| 126 | + private Optional<LogConfig> logging = Optional.empty(); |
| 127 | + |
| 128 | + private Optional<WebSocketFactory> webSocketFactory = Optional.empty(); |
| 129 | + |
| 130 | + public Builder environment(Environment environment) { |
| 131 | + this.environment = environment; |
| 132 | + return this; |
| 133 | + } |
| 134 | + |
| 135 | + public Builder addHeader(String key, String value) { |
| 136 | + this.headers.put(key, value); |
| 137 | + return this; |
| 138 | + } |
| 139 | + |
| 140 | + public Builder addHeader(String key, Supplier<String> value) { |
| 141 | + this.headerSuppliers.put(key, value); |
| 142 | + return this; |
| 143 | + } |
| 144 | + |
| 145 | + /** |
| 146 | + * Override the timeout in seconds. Defaults to 60 seconds. |
| 147 | + */ |
| 148 | + public Builder timeout(int timeout) { |
| 149 | + this.timeout = Optional.of(timeout); |
| 150 | + return this; |
| 151 | + } |
| 152 | + |
| 153 | + /** |
| 154 | + * Override the timeout in seconds. Defaults to 60 seconds. |
| 155 | + */ |
| 156 | + public Builder timeout(Optional<Integer> timeout) { |
| 157 | + this.timeout = timeout; |
| 158 | + return this; |
| 159 | + } |
| 160 | + |
| 161 | + /** |
| 162 | + * Override the maximum number of retries. Defaults to 2 retries. |
| 163 | + */ |
| 164 | + public Builder maxRetries(int maxRetries) { |
| 165 | + this.maxRetries = maxRetries; |
| 166 | + return this; |
| 167 | + } |
| 168 | + |
| 169 | + public Builder httpClient(OkHttpClient httpClient) { |
| 170 | + this.httpClient = httpClient; |
| 171 | + return this; |
| 172 | + } |
| 173 | + |
| 174 | + /** |
| 175 | + * Set a custom WebSocketFactory for creating WebSocket connections. |
| 176 | + */ |
| 177 | + public Builder webSocketFactory(WebSocketFactory webSocketFactory) { |
| 178 | + this.webSocketFactory = Optional.of(webSocketFactory); |
| 179 | + return this; |
| 180 | + } |
| 181 | + |
| 182 | + /** |
| 183 | + * Configure logging for the SDK. Silent by default — no log output unless explicitly configured. |
| 184 | + */ |
| 185 | + public Builder logging(LogConfig logging) { |
| 186 | + this.logging = Optional.of(logging); |
| 187 | + return this; |
| 188 | + } |
| 189 | + |
| 190 | + public ClientOptions build() { |
| 191 | + OkHttpClient.Builder httpClientBuilder = |
| 192 | + this.httpClient != null ? this.httpClient.newBuilder() : new OkHttpClient.Builder(); |
| 193 | + |
| 194 | + if (this.httpClient != null) { |
| 195 | + timeout.ifPresent(timeout -> httpClientBuilder |
| 196 | + .callTimeout(timeout, TimeUnit.SECONDS) |
| 197 | + .connectTimeout(0, TimeUnit.SECONDS) |
| 198 | + .writeTimeout(0, TimeUnit.SECONDS) |
| 199 | + .readTimeout(0, TimeUnit.SECONDS)); |
| 200 | + } else { |
| 201 | + httpClientBuilder |
| 202 | + .callTimeout(this.timeout.orElse(60), TimeUnit.SECONDS) |
| 203 | + .connectTimeout(0, TimeUnit.SECONDS) |
| 204 | + .writeTimeout(0, TimeUnit.SECONDS) |
| 205 | + .readTimeout(0, TimeUnit.SECONDS) |
| 206 | + .addInterceptor(new RetryInterceptor(this.maxRetries)); |
| 207 | + } |
| 208 | + |
| 209 | + Logger logger = Logger.from(this.logging); |
| 210 | + httpClientBuilder.addInterceptor(new LoggingInterceptor(logger)); |
| 211 | + |
| 212 | + this.httpClient = httpClientBuilder.build(); |
| 213 | + this.timeout = Optional.of(httpClient.callTimeoutMillis() / 1000); |
| 214 | + |
| 215 | + return new ClientOptions( |
| 216 | + environment, |
| 217 | + headers, |
| 218 | + headerSuppliers, |
| 219 | + httpClient, |
| 220 | + this.timeout.get(), |
| 221 | + this.maxRetries, |
| 222 | + this.webSocketFactory, |
| 223 | + this.logging); |
| 224 | + } |
| 225 | + |
| 226 | + /** |
| 227 | + * Create a new Builder initialized with values from an existing ClientOptions |
| 228 | + */ |
| 229 | + public static Builder from(ClientOptions clientOptions) { |
| 230 | + Builder builder = new Builder(); |
| 231 | + builder.environment = clientOptions.environment(); |
| 232 | + builder.timeout = Optional.of(clientOptions.timeout(null)); |
| 233 | + builder.httpClient = clientOptions.httpClient(); |
| 234 | + builder.headers.putAll(clientOptions.headers); |
| 235 | + builder.headerSuppliers.putAll(clientOptions.headerSuppliers); |
| 236 | + builder.maxRetries = clientOptions.maxRetries(); |
| 237 | + builder.logging = clientOptions.logging(); |
| 238 | + return builder; |
| 239 | + } |
| 240 | + } |
| 241 | +} |
0 commit comments