|
| 1 | +package so.trophy; |
| 2 | + |
| 3 | +/** |
| 4 | + * This file was auto-generated by Fern from our API Definition. |
| 5 | + */ |
| 6 | + |
| 7 | +import so.trophy.core.ClientOptions; |
| 8 | +import so.trophy.core.Environment; |
| 9 | +import java.lang.Integer; |
| 10 | +import java.lang.String; |
| 11 | +import java.util.HashMap; |
| 12 | +import java.util.Map; |
| 13 | +import java.util.Optional; |
| 14 | +import okhttp3.OkHttpClient; |
| 15 | + |
| 16 | +public class AsyncTrophyApiClientBuilder { |
| 17 | + private Optional<Integer> timeout = Optional.empty(); |
| 18 | + |
| 19 | + private Optional<Integer> maxRetries = Optional.empty(); |
| 20 | + |
| 21 | + private final Map<String, String> customHeaders = new HashMap<>(); |
| 22 | + |
| 23 | + private String apiKey = null; |
| 24 | + |
| 25 | + private Environment environment = Environment.PRODUCTION; |
| 26 | + |
| 27 | + private OkHttpClient httpClient; |
| 28 | + |
| 29 | + /** |
| 30 | + * Sets apiKey |
| 31 | + */ |
| 32 | + public AsyncTrophyApiClientBuilder apiKey(String apiKey) { |
| 33 | + this.apiKey = apiKey; |
| 34 | + return this; |
| 35 | + } |
| 36 | + |
| 37 | + public AsyncTrophyApiClientBuilder environment(Environment environment) { |
| 38 | + this.environment = environment; |
| 39 | + return this; |
| 40 | + } |
| 41 | + |
| 42 | + /** |
| 43 | + * Sets the timeout (in seconds) for the client. Defaults to 60 seconds. |
| 44 | + */ |
| 45 | + public AsyncTrophyApiClientBuilder timeout(int timeout) { |
| 46 | + this.timeout = Optional.of(timeout); |
| 47 | + return this; |
| 48 | + } |
| 49 | + |
| 50 | + /** |
| 51 | + * Sets the maximum number of retries for the client. Defaults to 2 retries. |
| 52 | + */ |
| 53 | + public AsyncTrophyApiClientBuilder maxRetries(int maxRetries) { |
| 54 | + this.maxRetries = Optional.of(maxRetries); |
| 55 | + return this; |
| 56 | + } |
| 57 | + |
| 58 | + /** |
| 59 | + * Sets the underlying OkHttp client |
| 60 | + */ |
| 61 | + public AsyncTrophyApiClientBuilder httpClient(OkHttpClient httpClient) { |
| 62 | + this.httpClient = httpClient; |
| 63 | + return this; |
| 64 | + } |
| 65 | + |
| 66 | + /** |
| 67 | + * Add a custom header to be sent with all requests. |
| 68 | + * For headers that need to be computed dynamically or conditionally, use the setAdditional() method override instead. |
| 69 | + * |
| 70 | + * @param name The header name |
| 71 | + * @param value The header value |
| 72 | + * @return This builder for method chaining |
| 73 | + */ |
| 74 | + public AsyncTrophyApiClientBuilder addHeader(String name, String value) { |
| 75 | + this.customHeaders.put(name, value); |
| 76 | + return this; |
| 77 | + } |
| 78 | + |
| 79 | + protected ClientOptions buildClientOptions() { |
| 80 | + ClientOptions.Builder builder = ClientOptions.builder(); |
| 81 | + setEnvironment(builder); |
| 82 | + setAuthentication(builder); |
| 83 | + setHttpClient(builder); |
| 84 | + setTimeouts(builder); |
| 85 | + setRetries(builder); |
| 86 | + for (Map.Entry<String, String> header : this.customHeaders.entrySet()) { |
| 87 | + builder.addHeader(header.getKey(), header.getValue()); |
| 88 | + } |
| 89 | + setAdditional(builder); |
| 90 | + return builder.build(); |
| 91 | + } |
| 92 | + |
| 93 | + /** |
| 94 | + * Sets the environment configuration for the client. |
| 95 | + * Override this method to modify URLs or add environment-specific logic. |
| 96 | + * |
| 97 | + * @param builder The ClientOptions.Builder to configure |
| 98 | + */ |
| 99 | + protected void setEnvironment(ClientOptions.Builder builder) { |
| 100 | + builder.environment(this.environment); |
| 101 | + } |
| 102 | + |
| 103 | + /** |
| 104 | + * Override this method to customize authentication. |
| 105 | + * This method is called during client options construction to set up authentication headers. |
| 106 | + * |
| 107 | + * @param builder The ClientOptions.Builder to configure |
| 108 | + * |
| 109 | + * Example: |
| 110 | + * <pre>{@code |
| 111 | + * @Override |
| 112 | + * protected void setAuthentication(ClientOptions.Builder builder) { |
| 113 | + * super.setAuthentication(builder); // Keep existing auth |
| 114 | + * builder.addHeader("X-API-Key", this.apiKey); |
| 115 | + * } |
| 116 | + * }</pre> |
| 117 | + */ |
| 118 | + protected void setAuthentication(ClientOptions.Builder builder) { |
| 119 | + builder.addHeader("X-API-KEY", this.apiKey); |
| 120 | + } |
| 121 | + |
| 122 | + /** |
| 123 | + * Sets the request timeout configuration. |
| 124 | + * Override this method to customize timeout behavior. |
| 125 | + * |
| 126 | + * @param builder The ClientOptions.Builder to configure |
| 127 | + */ |
| 128 | + protected void setTimeouts(ClientOptions.Builder builder) { |
| 129 | + if (this.timeout.isPresent()) { |
| 130 | + builder.timeout(this.timeout.get()); |
| 131 | + } |
| 132 | + } |
| 133 | + |
| 134 | + /** |
| 135 | + * Sets the retry configuration for failed requests. |
| 136 | + * Override this method to implement custom retry strategies. |
| 137 | + * |
| 138 | + * @param builder The ClientOptions.Builder to configure |
| 139 | + */ |
| 140 | + protected void setRetries(ClientOptions.Builder builder) { |
| 141 | + if (this.maxRetries.isPresent()) { |
| 142 | + builder.maxRetries(this.maxRetries.get()); |
| 143 | + } |
| 144 | + } |
| 145 | + |
| 146 | + /** |
| 147 | + * Sets the OkHttp client configuration. |
| 148 | + * Override this method to customize HTTP client behavior (interceptors, connection pools, etc). |
| 149 | + * |
| 150 | + * @param builder The ClientOptions.Builder to configure |
| 151 | + */ |
| 152 | + protected void setHttpClient(ClientOptions.Builder builder) { |
| 153 | + if (this.httpClient != null) { |
| 154 | + builder.httpClient(this.httpClient); |
| 155 | + } |
| 156 | + } |
| 157 | + |
| 158 | + /** |
| 159 | + * Override this method to add any additional configuration to the client. |
| 160 | + * This method is called at the end of the configuration chain, allowing you to add |
| 161 | + * custom headers, modify settings, or perform any other client customization. |
| 162 | + * |
| 163 | + * @param builder The ClientOptions.Builder to configure |
| 164 | + * |
| 165 | + * Example: |
| 166 | + * <pre>{@code |
| 167 | + * @Override |
| 168 | + * protected void setAdditional(ClientOptions.Builder builder) { |
| 169 | + * builder.addHeader("X-Request-ID", () -> UUID.randomUUID().toString()); |
| 170 | + * builder.addHeader("X-Client-Version", "1.0.0"); |
| 171 | + * } |
| 172 | + * }</pre> |
| 173 | + */ |
| 174 | + protected void setAdditional(ClientOptions.Builder builder) { |
| 175 | + } |
| 176 | + |
| 177 | + /** |
| 178 | + * Override this method to add custom validation logic before the client is built. |
| 179 | + * This method is called at the beginning of the build() method to ensure the configuration is valid. |
| 180 | + * Throw an exception to prevent client creation if validation fails. |
| 181 | + * |
| 182 | + * Example: |
| 183 | + * <pre>{@code |
| 184 | + * @Override |
| 185 | + * protected void validateConfiguration() { |
| 186 | + * super.validateConfiguration(); // Run parent validations |
| 187 | + * if (tenantId == null || tenantId.isEmpty()) { |
| 188 | + * throw new IllegalStateException("tenantId is required"); |
| 189 | + * } |
| 190 | + * } |
| 191 | + * }</pre> |
| 192 | + */ |
| 193 | + protected void validateConfiguration() { |
| 194 | + } |
| 195 | + |
| 196 | + public AsyncTrophyApiClient build() { |
| 197 | + if (apiKey == null) { |
| 198 | + throw new RuntimeException("Please provide apiKey"); |
| 199 | + } |
| 200 | + validateConfiguration(); |
| 201 | + return new AsyncTrophyApiClient(buildClientOptions()); |
| 202 | + } |
| 203 | +} |
0 commit comments