001 002package com.commercetools.importapi.defaultconfig; 003 004import java.net.URI; 005import java.time.Duration; 006import java.util.List; 007import java.util.Map; 008import java.util.concurrent.ExecutorService; 009import java.util.concurrent.ScheduledExecutorService; 010import java.util.function.Function; 011import java.util.function.Supplier; 012import java.util.function.UnaryOperator; 013 014import javax.annotation.Nullable; 015 016import com.commercetools.importapi.client.ApiRoot; 017import com.commercetools.importapi.client.ByProjectKeyRequestBuilder; 018import com.commercetools.importapi.client.ImportCorrelationIdProvider; 019import com.commercetools.importapi.client.ProjectApiRoot; 020 021import io.vrap.rmf.base.client.*; 022import io.vrap.rmf.base.client.error.HttpExceptionFactory; 023import io.vrap.rmf.base.client.http.*; 024import io.vrap.rmf.base.client.oauth2.ClientCredentials; 025import io.vrap.rmf.base.client.oauth2.TokenSupplier; 026 027import org.slf4j.event.Level; 028 029import dev.failsafe.spi.Scheduler; 030 031public class ImportApiRootBuilder { 032 private final ClientBuilder builder; 033 034 private ImportApiRootBuilder(ClientBuilder builder) { 035 this.builder = builder; 036 } 037 038 public static ImportApiRootBuilder of() { 039 return new ImportApiRootBuilder(ClientBuilder.of()); 040 } 041 042 public static ImportApiRootBuilder of(final VrapHttpClient httpClient) { 043 return new ImportApiRootBuilder(ClientBuilder.of(httpClient)); 044 } 045 046 public static ImportApiRootBuilder of(final HandlerStack stack) { 047 return new ImportApiRootBuilder(ClientBuilder.of(stack)); 048 } 049 050 public ImportApiRootBuilder withAuthCircuitBreaker() { 051 builder.withAuthCircuitBreaker(); 052 return this; 053 } 054 055 public ImportApiRootBuilder withoutAuthCircuitBreaker() { 056 builder.withoutAuthCircuitBreaker(); 057 return this; 058 } 059 060 public ImportApiRootBuilder withAuthRetries(final int authRetries) { 061 builder.withAuthRetries(authRetries); 062 return this; 063 } 064 065 public ImportApiRootBuilder withHandlerStack(final HandlerStack stack) { 066 builder.withHandlerStack(stack); 067 return this; 068 } 069 070 public ImportApiRootBuilder withHttpClient(final VrapHttpClient httpClient) { 071 builder.withHttpClient(httpClient); 072 return this; 073 } 074 075 public ImportApiRootBuilder withSerializer(final ResponseSerializer serializer) { 076 builder.withSerializer(serializer); 077 return this; 078 } 079 080 public ImportApiRootBuilder withSerializer(final Supplier<ResponseSerializer> serializer) { 081 builder.withSerializer(serializer); 082 return this; 083 } 084 085 public ImportApiRootBuilder withHttpExceptionFactory(final HttpExceptionFactory factory) { 086 builder.withHttpExceptionFactory(factory); 087 return this; 088 } 089 090 public ImportApiRootBuilder withHttpExceptionFactory( 091 final Function<ResponseSerializer, HttpExceptionFactory> factory) { 092 builder.withHttpExceptionFactory(factory); 093 return this; 094 } 095 096 public ImportApiRootBuilder withHttpExceptionFactory(final Supplier<HttpExceptionFactory> factory) { 097 builder.withHttpExceptionFactory(factory); 098 return this; 099 } 100 101 public ImportApiRootBuilder defaultClient(final ClientCredentials credentials) { 102 return defaultClient(credentials, ServiceRegion.GCP_EUROPE_WEST1); 103 } 104 105 public ImportApiRootBuilder defaultClient(final ClientCredentials credentials, ServiceRegionConfig serviceRegion) { 106 builder.defaultClient(credentials, serviceRegion); 107 108 return this; 109 } 110 111 public ImportApiRootBuilder defaultClient(final ClientCredentials credentials, final String tokenEndpoint, 112 final String apiEndpoint) { 113 return this.defaultClient(URI.create(apiEndpoint)).withClientCredentialsFlow(credentials, tokenEndpoint); 114 } 115 116 public ImportApiRootBuilder defaultClient(final String apiEndpoint, final ClientCredentials credentials, 117 final String tokenEndpoint) { 118 return this.defaultClient(URI.create(apiEndpoint)).withClientCredentialsFlow(credentials, tokenEndpoint); 119 } 120 121 public ImportApiRootBuilder defaultClient(final String apiEndpoint) { 122 return this.defaultClient(URI.create(apiEndpoint)); 123 } 124 125 public ImportApiRootBuilder defaultClient(final URI apiEndpoint) { 126 builder.defaultClient(apiEndpoint); 127 128 return this; 129 } 130 131 public ImportApiRootBuilder withClientCredentialsFlow(final ClientCredentials credentials, 132 final String tokenEndpoint) { 133 builder.withClientCredentialsFlow(credentials, tokenEndpoint); 134 135 return this; 136 } 137 138 public ImportApiRootBuilder withClientCredentialsFlow(final ClientCredentials credentials, 139 final String tokenEndpoint, final VrapHttpClient httpClient) { 140 builder.withClientCredentialsFlow(credentials, tokenEndpoint, httpClient); 141 142 return this; 143 } 144 145 public ImportApiRootBuilder withClientCredentialsFlow(final ClientCredentials credentials, 146 final URI tokenEndpoint) { 147 builder.withClientCredentialsFlow(credentials, tokenEndpoint); 148 149 return this; 150 } 151 152 public ImportApiRootBuilder withClientCredentialsFlow(final ClientCredentials credentials, final URI tokenEndpoint, 153 final VrapHttpClient httpClient) { 154 builder.withClientCredentialsFlow(credentials, tokenEndpoint, httpClient); 155 156 return this; 157 } 158 159 public ImportApiRootBuilder withStaticTokenFlow(final AuthenticationToken token) { 160 builder.withStaticTokenFlow(token); 161 162 return this; 163 } 164 165 public ImportApiRootBuilder withAnonymousSessionFlow(final ClientCredentials credentials, 166 final String tokenEndpoint) { 167 builder.withAnonymousSessionFlow(credentials, tokenEndpoint); 168 169 return this; 170 } 171 172 public ImportApiRootBuilder withAnonymousSessionFlow(final ClientCredentials credentials, 173 final String tokenEndpoint, final VrapHttpClient httpClient) { 174 builder.withAnonymousSessionFlow(credentials, tokenEndpoint, httpClient); 175 176 return this; 177 } 178 179 public ImportApiRootBuilder withGlobalCustomerPasswordFlow(final ClientCredentials credentials, final String email, 180 final String password, final String tokenEndpoint) { 181 builder.withGlobalCustomerPasswordFlow(credentials, email, password, tokenEndpoint); 182 183 return this; 184 } 185 186 public ImportApiRootBuilder withGlobalCustomerPasswordFlow(final ClientCredentials credentials, final String email, 187 final String password, final String tokenEndpoint, final VrapHttpClient httpClient) { 188 builder.withGlobalCustomerPasswordFlow(credentials, email, password, tokenEndpoint, httpClient); 189 190 return this; 191 } 192 193 public ImportApiRootBuilder addAcceptGZipMiddleware() { 194 builder.addAcceptGZipMiddleware(); 195 196 return this; 197 } 198 199 public ImportApiRootBuilder withErrorMiddleware() { 200 builder.withErrorMiddleware(); 201 202 return this; 203 } 204 205 public ImportApiRootBuilder withErrorMiddleware(final ErrorMiddleware errorMiddleware) { 206 builder.withErrorMiddleware(errorMiddleware); 207 208 return this; 209 } 210 211 public ImportApiRootBuilder withRetryMiddleware(Supplier<RetryRequestMiddleware> retryMiddleware) { 212 builder.withRetryMiddleware(retryMiddleware); 213 214 return this; 215 } 216 217 public ImportApiRootBuilder withRetryMiddleware(RetryRequestMiddleware retryMiddleware) { 218 builder.withRetryMiddleware(retryMiddleware); 219 220 return this; 221 } 222 223 public ImportApiRootBuilder withRetryMiddleware(final int maxRetries) { 224 builder.withRetryMiddleware(maxRetries); 225 226 return this; 227 } 228 229 public ImportApiRootBuilder withRetryMiddleware(final int maxRetries, List<Integer> statusCodes) { 230 builder.withRetryMiddleware(maxRetries, statusCodes); 231 232 return this; 233 } 234 235 public ImportApiRootBuilder withRetryMiddleware(final int maxRetries, List<Integer> statusCodes, 236 final List<Class<? extends Throwable>> failures) { 237 builder.withRetryMiddleware(maxRetries, statusCodes, failures); 238 239 return this; 240 } 241 242 public ImportApiRootBuilder withRetryMiddleware(final int maxRetries, final long delay, final long maxDelay, 243 List<Integer> statusCodes, final List<Class<? extends Throwable>> failures, 244 final FailsafeRetryPolicyBuilderOptions fn) { 245 builder.withRetryMiddleware(maxRetries, delay, maxDelay, statusCodes, failures, fn); 246 247 return this; 248 } 249 250 public ImportApiRootBuilder withRetryMiddleware(final int maxRetries, final long delay, final long maxDelay, 251 final FailsafeRetryPolicyBuilderOptions fn) { 252 builder.withRetryMiddleware(maxRetries, delay, maxDelay, fn); 253 254 return this; 255 } 256 257 public ImportApiRootBuilder withQueueMiddleware(final Supplier<QueueRequestMiddleware> queueMiddleware) { 258 return with(clientBuilder -> clientBuilder.withQueueMiddleware(queueMiddleware)); 259 } 260 261 public ImportApiRootBuilder withQueueMiddleware(final QueueRequestMiddleware queueMiddleware) { 262 return with(clientBuilder -> clientBuilder.withQueueMiddleware(queueMiddleware)); 263 } 264 265 public ImportApiRootBuilder withQueueMiddleware(final int maxRequests, final Duration maxWaitTime) { 266 return with(clientBuilder -> clientBuilder.withQueueMiddleware(maxRequests, maxWaitTime)); 267 } 268 269 public ImportApiRootBuilder withQueueMiddleware(final Scheduler scheduler, final int maxRequests, 270 final Duration maxWaitTime) { 271 return with(clientBuilder -> clientBuilder.withQueueMiddleware(scheduler, maxRequests, maxWaitTime)); 272 } 273 274 public ImportApiRootBuilder withQueueMiddleware(final ScheduledExecutorService executorService, 275 final int maxRequests, final Duration maxWaitTime) { 276 return with(clientBuilder -> clientBuilder.withQueueMiddleware(executorService, maxRequests, maxWaitTime)); 277 } 278 279 public ImportApiRootBuilder withQueueMiddleware(final ExecutorService executorService, final int maxRequests, 280 final Duration maxWaitTime) { 281 return with(clientBuilder -> clientBuilder.withQueueMiddleware(executorService, maxRequests, maxWaitTime)); 282 } 283 284 public ImportApiRootBuilder withOAuthMiddleware(final Supplier<OAuthMiddleware> oAuthMiddleware) { 285 builder.withOAuthMiddleware(oAuthMiddleware); 286 287 return this; 288 } 289 290 public ImportApiRootBuilder withOAuthMiddleware(final OAuthMiddleware oAuthMiddleware) { 291 builder.withOAuthMiddleware(oAuthMiddleware); 292 293 return this; 294 } 295 296 public ImportApiRootBuilder withTokenSupplier(final TokenSupplier tokenSupplier) { 297 builder.withTokenSupplier(tokenSupplier); 298 299 return this; 300 } 301 302 public ImportApiRootBuilder withTokenSupplier(final Supplier<TokenSupplier> tokenSupplier) { 303 builder.withTokenSupplier(tokenSupplier); 304 305 return this; 306 } 307 308 public ImportApiRootBuilder withInternalLoggerMiddleware(final InternalLoggerMiddleware internalLoggerMiddleware) { 309 builder.withInternalLoggerMiddleware(internalLoggerMiddleware); 310 311 return this; 312 } 313 314 public ImportApiRootBuilder withInternalLoggerFactory(final InternalLoggerFactory internalLoggerFactory) { 315 builder.withInternalLoggerFactory(internalLoggerFactory); 316 317 return this; 318 } 319 320 public ImportApiRootBuilder withInternalLoggerFactory(final InternalLoggerFactory internalLoggerFactory, 321 final Level responseLogEvent, final Level deprecationLogEvent) { 322 builder.withInternalLoggerFactory(internalLoggerFactory, responseLogEvent, deprecationLogEvent); 323 324 return this; 325 } 326 327 public ImportApiRootBuilder withInternalLoggerFactory(final InternalLoggerFactory internalLoggerFactory, 328 final Level responseLogEvent, final Level deprecationLogEvent, final Level defaultExceptionLogEvent, 329 final Map<Class<? extends Throwable>, Level> exceptionLogEvents) { 330 builder.withInternalLoggerFactory(internalLoggerFactory, responseLogEvent, deprecationLogEvent, 331 defaultExceptionLogEvent, exceptionLogEvents); 332 333 return this; 334 } 335 336 public ImportApiRootBuilder withApiBaseUrl(String apiBaseUrl) { 337 builder.withApiBaseUrl(apiBaseUrl); 338 339 return this; 340 } 341 342 public ImportApiRootBuilder withApiBaseUrl(final URI apiBaseUrl) { 343 builder.withApiBaseUrl(apiBaseUrl); 344 345 return this; 346 } 347 348 public ImportApiRootBuilder withUserAgentSupplier(final Supplier<String> userAgentSupplier) { 349 builder.withUserAgentSupplier(userAgentSupplier); 350 351 return this; 352 } 353 354 public ImportApiRootBuilder addCorrelationIdProvider(final @Nullable CorrelationIdProvider correlationIdProvider) { 355 return addCorrelationIdProvider(correlationIdProvider, true); 356 } 357 358 private ImportApiRootBuilder addCorrelationIdProvider(final @Nullable CorrelationIdProvider correlationIdProvider, 359 final boolean replace) { 360 builder.addCorrelationIdProvider(correlationIdProvider, replace); 361 362 return this; 363 } 364 365 public ImportApiRootBuilder withMiddleware(final Middleware middleware, final Middleware... middlewares) { 366 builder.withMiddleware(middleware, middlewares); 367 368 return this; 369 } 370 371 public ImportApiRootBuilder addMiddleware(final Middleware middleware, final Middleware... middlewares) { 372 builder.addMiddleware(middleware, middlewares); 373 374 return this; 375 } 376 377 public ImportApiRootBuilder withMiddlewares(final List<Middleware> middlewares) { 378 builder.withMiddlewares(middlewares); 379 380 return this; 381 } 382 383 public ImportApiRootBuilder addMiddlewares(final List<Middleware> middlewares) { 384 builder.addMiddlewares(middlewares); 385 386 return this; 387 } 388 389 public ImportApiRootBuilder with(UnaryOperator<ClientBuilder> builderUnaryOperator) { 390 builderUnaryOperator.apply(builder); 391 392 return this; 393 } 394 395 public ApiRoot build() { 396 return ApiRoot.fromClient(clientSupplier().get()); 397 } 398 399 public ApiHttpClient buildClient() { 400 return clientSupplier().get(); 401 } 402 403 public Supplier<ApiHttpClient> clientSupplier() { 404 return builder::build; 405 } 406 407 /** 408 * @deprecated use {@link #build(String)} instead 409 */ 410 @Deprecated 411 public ByProjectKeyRequestBuilder buildForProject(final String projectKey) { 412 addCorrelationIdProvider(new ImportCorrelationIdProvider(projectKey), false); 413 return ApiRoot.fromClient(builder.build()).withProjectKeyValue(projectKey); 414 } 415 416 /** 417 * @deprecated use {@link #build(String)} instead 418 */ 419 @Deprecated 420 public ProjectApiRoot buildProjectRoot(final String projectKey) { 421 addCorrelationIdProvider(new ImportCorrelationIdProvider(projectKey), false); 422 return ProjectApiRoot.fromClient(projectKey, builder.build()); 423 } 424 425 public ProjectApiRoot build(final String projectKey) { 426 addCorrelationIdProvider(new ImportCorrelationIdProvider(projectKey), false); 427 return ProjectApiRoot.fromClient(projectKey, builder.build()); 428 } 429}