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