Skip to content

Commit

Permalink
Merge pull request #521 from commercetools/policy_middleware
Browse files Browse the repository at this point in the history
  • Loading branch information
jenschude authored Dec 4, 2023
2 parents 2f8ebe8 + 549c1fc commit 6146fe8
Show file tree
Hide file tree
Showing 27 changed files with 1,207 additions and 89 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -43,9 +43,9 @@ public void v2() {
final ProjectApiRoot projectClient = ApiRootBuilder.of()
.defaultClient(ClientCredentials.of().withClientId("clientId").withClientSecret("clientSecret").build(),
ServiceRegion.GCP_EUROPE_WEST1)
.withRetryMiddleware(5,
Arrays.asList(HttpStatusCode.BAD_GATEWAY_502, HttpStatusCode.SERVICE_UNAVAILABLE_503,
HttpStatusCode.GATEWAY_TIMEOUT_504))
.withPolicies(policyBuilder -> policyBuilder.withRetry(builder -> builder.maxRetries(5)
.statusCodes(Arrays.asList(HttpStatusCode.BAD_GATEWAY_502,
HttpStatusCode.SERVICE_UNAVAILABLE_503, HttpStatusCode.GATEWAY_TIMEOUT_504))))
.build("projectKey");

final CategoryPagedQueryResponse body = projectClient.categories().get().executeBlocking().getBody();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,8 @@ public void execute() {
count.getAndIncrement();
return next.apply(request);
})
.withRetryMiddleware(3, singletonList(HttpStatusCode.NOT_FOUND_404))
.withPolicies(policies -> policies.withRetry(
builder -> builder.maxRetries(3).statusCodes(singletonList(HttpStatusCode.NOT_FOUND_404))))
.build(projectKey);

Assertions.assertThatExceptionOfType(NotFoundException.class).isThrownBy(() -> {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,8 @@ public void timeoutWithRetry() {
}
}))
.addMiddleware((request, next) -> failsafeExecutor.getStageAsync(() -> next.apply(request)))
.withRetryMiddleware(3, singletonList(HttpStatusCode.NOT_FOUND_404))
.withPolicies(policyBuilder -> policyBuilder.withRetry(
builder -> builder.maxRetries(3).statusCodes(singletonList(HttpStatusCode.NOT_FOUND_404))))
.build(projectKey);

Assertions.assertThatExceptionOfType(TimeoutExceededException.class).isThrownBy(() -> {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,10 @@
package commercetools.utils;

import static java.util.Arrays.asList;
import static java.util.Collections.singletonList;

import java.time.Duration;
import java.util.LinkedHashSet;
import java.util.Locale;
import java.util.Set;
import java.util.UUID;
import java.util.*;

import com.commercetools.api.client.ProjectApiRoot;
import com.commercetools.api.defaultconfig.ApiRootBuilder;
Expand All @@ -23,6 +21,8 @@ public class CommercetoolsTestUtils {
static {
ApiRootBuilder builder = ApiRootBuilder.ofEnvironmentVariables()
.addConcurrentModificationMiddleware()
.withPolicies(
policyBuilder -> policyBuilder.withRetry(b -> b.maxRetries(5).statusCodes(singletonList(503))))
.withErrorMiddleware(ErrorMiddleware.ExceptionMode.UNWRAP_COMPLETION_EXCEPTION);
projectApiRoot = builder.buildProjectRoot();
}
Expand Down

Large diffs are not rendered by default.

Original file line number Diff line number Diff line change
Expand Up @@ -162,7 +162,8 @@ public void retry() {
ApiRoot apiRoot = ApiRootBuilder.of()
.defaultClient(ClientCredentials.of().withClientId("clientId").withClientSecret("clientSecret").build(),
ServiceRegion.GCP_EUROPE_WEST1)
.withRetryMiddleware(5, Arrays.asList(BAD_GATEWAY_502, SERVICE_UNAVAILABLE_503, GATEWAY_TIMEOUT_504))
.withPolicies(policies -> policies.withRetry(builder -> builder.maxRetries(5)
.statusCodes(Arrays.asList(BAD_GATEWAY_502, SERVICE_UNAVAILABLE_503, GATEWAY_TIMEOUT_504))))
.build();
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -208,37 +208,65 @@ public HistoryApiRootBuilder withErrorMiddleware(final ErrorMiddleware errorMidd
return this;
}

public HistoryApiRootBuilder withPolicies(final Function<PolicyBuilder, PolicyBuilder> fn) {
return with(clientBuilder -> clientBuilder.withPolicies(fn));
}

/**
* @deprecated use {@link #withPolicies(Function)} instead
*/
@Deprecated
public HistoryApiRootBuilder withRetryMiddleware(Supplier<RetryRequestMiddleware> retryMiddleware) {
builder.withRetryMiddleware(retryMiddleware);

return this;
}

/**
* @deprecated use {@link #withPolicies(Function)} instead
*/
@Deprecated
public HistoryApiRootBuilder withRetryMiddleware(RetryRequestMiddleware retryMiddleware) {
builder.withRetryMiddleware(retryMiddleware);

return this;
}

/**
* @deprecated use {@link #withPolicies(Function)} instead
*/
@Deprecated
public HistoryApiRootBuilder withRetryMiddleware(final int maxRetries) {
builder.withRetryMiddleware(maxRetries);

return this;
}

/**
* @deprecated use {@link #withPolicies(Function)} instead
*/
@Deprecated
public HistoryApiRootBuilder withRetryMiddleware(final int maxRetries, List<Integer> statusCodes) {
builder.withRetryMiddleware(maxRetries, statusCodes);

return this;
}

/**
* @deprecated use {@link #withPolicies(Function)} instead
*/
@Deprecated
public HistoryApiRootBuilder withRetryMiddleware(final int maxRetries, List<Integer> statusCodes,
final List<Class<? extends Throwable>> failures) {
builder.withRetryMiddleware(maxRetries, statusCodes, failures);

return this;
}

/**
* @deprecated use {@link #withPolicies(Function)} instead
*/
@Deprecated
public HistoryApiRootBuilder withRetryMiddleware(final int maxRetries, final long delay, final long maxDelay,
List<Integer> statusCodes, final List<Class<? extends Throwable>> failures,
final FailsafeRetryPolicyBuilderOptions fn) {
Expand All @@ -247,6 +275,10 @@ public HistoryApiRootBuilder withRetryMiddleware(final int maxRetries, final lon
return this;
}

/**
* @deprecated use {@link #withPolicies(Function)} instead
*/
@Deprecated
public HistoryApiRootBuilder withRetryMiddleware(final int maxRetries, final long delay, final long maxDelay,
final FailsafeRetryPolicyBuilderOptions fn) {
builder.withRetryMiddleware(maxRetries, delay, maxDelay, fn);
Expand All @@ -260,28 +292,52 @@ public HistoryApiRootBuilder withOAuthMiddleware(final Supplier<OAuthMiddleware>
return this;
}

/**
* @deprecated use {@link #withPolicies(Function)} instead
*/
@Deprecated
public HistoryApiRootBuilder withQueueMiddleware(final Supplier<QueueRequestMiddleware> queueMiddleware) {
return with(clientBuilder -> clientBuilder.withQueueMiddleware(queueMiddleware));
}

/**
* @deprecated use {@link #withPolicies(Function)} instead
*/
@Deprecated
public HistoryApiRootBuilder withQueueMiddleware(final QueueRequestMiddleware queueMiddleware) {
return with(clientBuilder -> clientBuilder.withQueueMiddleware(queueMiddleware));
}

/**
* @deprecated use {@link #withPolicies(Function)} instead
*/
@Deprecated
public HistoryApiRootBuilder withQueueMiddleware(final int maxRequests, final Duration maxWaitTime) {
return with(clientBuilder -> clientBuilder.withQueueMiddleware(maxRequests, maxWaitTime));
}

/**
* @deprecated use {@link #withPolicies(Function)} instead
*/
@Deprecated
public HistoryApiRootBuilder withQueueMiddleware(final Scheduler scheduler, final int maxRequests,
final Duration maxWaitTime) {
return with(clientBuilder -> clientBuilder.withQueueMiddleware(scheduler, maxRequests, maxWaitTime));
}

/**
* @deprecated use {@link #withPolicies(Function)} instead
*/
@Deprecated
public HistoryApiRootBuilder withQueueMiddleware(final ScheduledExecutorService executorService,
final int maxRequests, final Duration maxWaitTime) {
return with(clientBuilder -> clientBuilder.withQueueMiddleware(executorService, maxRequests, maxWaitTime));
}

/**
* @deprecated use {@link #withPolicies(Function)} instead
*/
@Deprecated
public HistoryApiRootBuilder withQueueMiddleware(final ExecutorService executorService, final int maxRequests,
final Duration maxWaitTime) {
return with(clientBuilder -> clientBuilder.withQueueMiddleware(executorService, maxRequests, maxWaitTime));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -208,37 +208,65 @@ public ImportApiRootBuilder withErrorMiddleware(final ErrorMiddleware errorMiddl
return this;
}

public ImportApiRootBuilder withPolicies(final Function<PolicyBuilder, PolicyBuilder> fn) {
return with(clientBuilder -> clientBuilder.withPolicies(fn));
}

/**
* @deprecated use {@link #withPolicies(Function)} instead
*/
@Deprecated
public ImportApiRootBuilder withRetryMiddleware(Supplier<RetryRequestMiddleware> retryMiddleware) {
builder.withRetryMiddleware(retryMiddleware);

return this;
}

/**
* @deprecated use {@link #withPolicies(Function)} instead
*/
@Deprecated
public ImportApiRootBuilder withRetryMiddleware(RetryRequestMiddleware retryMiddleware) {
builder.withRetryMiddleware(retryMiddleware);

return this;
}

/**
* @deprecated use {@link #withPolicies(Function)} instead
*/
@Deprecated
public ImportApiRootBuilder withRetryMiddleware(final int maxRetries) {
builder.withRetryMiddleware(maxRetries);

return this;
}

/**
* @deprecated use {@link #withPolicies(Function)} instead
*/
@Deprecated
public ImportApiRootBuilder withRetryMiddleware(final int maxRetries, List<Integer> statusCodes) {
builder.withRetryMiddleware(maxRetries, statusCodes);

return this;
}

/**
* @deprecated use {@link #withPolicies(Function)} instead
*/
@Deprecated
public ImportApiRootBuilder withRetryMiddleware(final int maxRetries, List<Integer> statusCodes,
final List<Class<? extends Throwable>> failures) {
builder.withRetryMiddleware(maxRetries, statusCodes, failures);

return this;
}

/**
* @deprecated use {@link #withPolicies(Function)} instead
*/
@Deprecated
public ImportApiRootBuilder withRetryMiddleware(final int maxRetries, final long delay, final long maxDelay,
List<Integer> statusCodes, final List<Class<? extends Throwable>> failures,
final FailsafeRetryPolicyBuilderOptions fn) {
Expand All @@ -247,35 +275,63 @@ public ImportApiRootBuilder withRetryMiddleware(final int maxRetries, final long
return this;
}

/**
* @deprecated use {@link #withPolicies(Function)} instead
*/
@Deprecated
public ImportApiRootBuilder withRetryMiddleware(final int maxRetries, final long delay, final long maxDelay,
final FailsafeRetryPolicyBuilderOptions fn) {
builder.withRetryMiddleware(maxRetries, delay, maxDelay, fn);

return this;
}

/**
* @deprecated use {@link #withPolicies(Function)} instead
*/
@Deprecated
public ImportApiRootBuilder withQueueMiddleware(final Supplier<QueueRequestMiddleware> queueMiddleware) {
return with(clientBuilder -> clientBuilder.withQueueMiddleware(queueMiddleware));
}

/**
* @deprecated use {@link #withPolicies(Function)} instead
*/
@Deprecated
public ImportApiRootBuilder withQueueMiddleware(final QueueRequestMiddleware queueMiddleware) {
return with(clientBuilder -> clientBuilder.withQueueMiddleware(queueMiddleware));
}

/**
* @deprecated use {@link #withPolicies(Function)} instead
*/
@Deprecated
public ImportApiRootBuilder withQueueMiddleware(final int maxRequests, final Duration maxWaitTime) {
return with(clientBuilder -> clientBuilder.withQueueMiddleware(maxRequests, maxWaitTime));
}

/**
* @deprecated use {@link #withPolicies(Function)} instead
*/
@Deprecated
public ImportApiRootBuilder withQueueMiddleware(final Scheduler scheduler, final int maxRequests,
final Duration maxWaitTime) {
return with(clientBuilder -> clientBuilder.withQueueMiddleware(scheduler, maxRequests, maxWaitTime));
}

/**
* @deprecated use {@link #withPolicies(Function)} instead
*/
@Deprecated
public ImportApiRootBuilder withQueueMiddleware(final ScheduledExecutorService executorService,
final int maxRequests, final Duration maxWaitTime) {
return with(clientBuilder -> clientBuilder.withQueueMiddleware(executorService, maxRequests, maxWaitTime));
}

/**
* @deprecated use {@link #withPolicies(Function)} instead
*/
@Deprecated
public ImportApiRootBuilder withQueueMiddleware(final ExecutorService executorService, final int maxRequests,
final Duration maxWaitTime) {
return with(clientBuilder -> clientBuilder.withQueueMiddleware(executorService, maxRequests, maxWaitTime));
Expand Down
Loading

0 comments on commit 6146fe8

Please sign in to comment.