From 4c387e5fccf3555c280bd463955aaeef538ca7e3 Mon Sep 17 00:00:00 2001 From: Jens Schulze Date: Fri, 21 Feb 2025 22:46:49 +0100 Subject: [PATCH] make serviceregion configurable in examples --- .../commercetools/sdk/examples/GettingStarted.java | 4 +++- .../commercetools/sdk/examples/GettingStarted.java | 4 +++- .../springmvc/service/CtpClientBeanService.java | 13 ++++++++++++- .../springmvc/config/CtpSecurityConfig.java | 11 +++++++---- .../examples/springmvc/config/MeClientFilter.java | 9 ++++++--- .../springmvc/service/CtpClientBeanService.java | 13 ++++++++++++- .../service/CtpReactiveAuthenticationManager.java | 6 ++++-- .../springmvc/service/CtpClientBeanService.java | 13 ++++++++++++- .../springmvc/service/CtpClientBeanService.java | 13 ++++++++++++- .../springmvc/service/CtpClientBeanService.java | 13 ++++++++++++- examples/spring/README.md | 6 +++++- .../examples/spring/config/CtpSecurityConfig.java | 11 +++++++---- .../sdk/examples/spring/config/MeClientFilter.java | 11 +++++++---- .../spring/service/CtpClientBeanService.java | 13 ++++++++++++- .../service/CtpReactiveAuthenticationManager.java | 6 ++++-- 15 files changed, 118 insertions(+), 28 deletions(-) diff --git a/examples/maven-okhttp3/src/test/java/com/commercetools/sdk/examples/GettingStarted.java b/examples/maven-okhttp3/src/test/java/com/commercetools/sdk/examples/GettingStarted.java index 34b1a582d4c..72130149edc 100644 --- a/examples/maven-okhttp3/src/test/java/com/commercetools/sdk/examples/GettingStarted.java +++ b/examples/maven-okhttp3/src/test/java/com/commercetools/sdk/examples/GettingStarted.java @@ -8,6 +8,8 @@ import org.junit.jupiter.api.Assertions; import org.junit.jupiter.api.Test; +import java.util.Optional; + public class GettingStarted { @Test @@ -16,7 +18,7 @@ public void project() { ClientCredentials.of().withClientId(System.getenv("CTP_CLIENT_ID")) .withClientSecret(System.getenv("CTP_CLIENT_SECRET")) .build(), - ServiceRegion.GCP_EUROPE_WEST1).build(System.getenv("CTP_PROJECT_KEY")); + ServiceRegion.valueOf(Optional.ofNullable(System.getenv("CTP_SERVICE_REGION")).orElse("GCP_EUROPE_WEST1"))).build(System.getenv("CTP_PROJECT_KEY")); Project response = apiRoot .get() diff --git a/examples/maven-okhttp4/src/test/java/com/commercetools/sdk/examples/GettingStarted.java b/examples/maven-okhttp4/src/test/java/com/commercetools/sdk/examples/GettingStarted.java index 34b1a582d4c..72130149edc 100644 --- a/examples/maven-okhttp4/src/test/java/com/commercetools/sdk/examples/GettingStarted.java +++ b/examples/maven-okhttp4/src/test/java/com/commercetools/sdk/examples/GettingStarted.java @@ -8,6 +8,8 @@ import org.junit.jupiter.api.Assertions; import org.junit.jupiter.api.Test; +import java.util.Optional; + public class GettingStarted { @Test @@ -16,7 +18,7 @@ public void project() { ClientCredentials.of().withClientId(System.getenv("CTP_CLIENT_ID")) .withClientSecret(System.getenv("CTP_CLIENT_SECRET")) .build(), - ServiceRegion.GCP_EUROPE_WEST1).build(System.getenv("CTP_PROJECT_KEY")); + ServiceRegion.valueOf(Optional.ofNullable(System.getenv("CTP_SERVICE_REGION")).orElse("GCP_EUROPE_WEST1"))).build(System.getenv("CTP_PROJECT_KEY")); Project response = apiRoot .get() diff --git a/examples/spring-datadog-statsd/src/main/java/com/commercetools/sdk/examples/springmvc/service/CtpClientBeanService.java b/examples/spring-datadog-statsd/src/main/java/com/commercetools/sdk/examples/springmvc/service/CtpClientBeanService.java index 87f9f31882f..8d84c7090dc 100644 --- a/examples/spring-datadog-statsd/src/main/java/com/commercetools/sdk/examples/springmvc/service/CtpClientBeanService.java +++ b/examples/spring-datadog-statsd/src/main/java/com/commercetools/sdk/examples/springmvc/service/CtpClientBeanService.java @@ -3,6 +3,7 @@ import com.commercetools.api.client.ProjectScopedApiRoot; import com.commercetools.api.defaultconfig.ApiRootBuilder; +import com.commercetools.api.defaultconfig.ServiceRegion; import com.commercetools.monitoring.datadog.statsd.DatadogMiddleware; import com.commercetools.monitoring.datadog.statsd.DatadogResponseSerializer; import com.timgroup.statsd.NonBlockingStatsDClientBuilder; @@ -15,6 +16,8 @@ import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; +import java.util.Optional; + @Configuration @EnableAutoConfiguration public class CtpClientBeanService { @@ -28,10 +31,18 @@ public class CtpClientBeanService { @Value(value = "${ctp.project.key}") private String projectKey; + @Value(value = "${ctp.service.region:#{null}}") + private String serviceRegion; + private ClientCredentials credentials() { return ClientCredentials.of().withClientId(clientId).withClientSecret(clientSecret).build(); } + @Bean + public ServiceRegion serviceRegion() { + return ServiceRegion.valueOf(Optional.ofNullable(this.serviceRegion).orElse("GCP_EUROPE_WEST1")); + } + @Bean public ProjectScopedApiRoot apiRoot() { StatsDClient statsd = new NonBlockingStatsDClientBuilder() @@ -39,7 +50,7 @@ public ProjectScopedApiRoot apiRoot() { .build(); ApiRootBuilder builder = ApiRootBuilder.of() - .defaultClient(credentials()) + .defaultClient(credentials(), serviceRegion()) .withSerializer(new DatadogResponseSerializer(ResponseSerializer.of(), statsd)) .withTelemetryMiddleware(new DatadogMiddleware(statsd)); return builder.build(projectKey); diff --git a/examples/spring-datadog/src/main/java/com/commercetools/sdk/examples/springmvc/config/CtpSecurityConfig.java b/examples/spring-datadog/src/main/java/com/commercetools/sdk/examples/springmvc/config/CtpSecurityConfig.java index 63e46f37d0d..d7913b9967a 100644 --- a/examples/spring-datadog/src/main/java/com/commercetools/sdk/examples/springmvc/config/CtpSecurityConfig.java +++ b/examples/spring-datadog/src/main/java/com/commercetools/sdk/examples/springmvc/config/CtpSecurityConfig.java @@ -94,6 +94,8 @@ public static class CtpReactiveAuthenticationManagerResolver implements ReactiveAuthenticationManagerResolver { private final ApiHttpClient apiHttpClient; + private final ServiceRegion serviceRegion; + @Value(value = "${ctp.client.id}") private String clientId; @@ -108,23 +110,24 @@ private ClientCredentials credentials() { } @Autowired - public CtpReactiveAuthenticationManagerResolver(final ApiHttpClient apiHttpClient) { + public CtpReactiveAuthenticationManagerResolver(final ApiHttpClient apiHttpClient, final ServiceRegion serviceRegion) { this.apiHttpClient = apiHttpClient; + this.serviceRegion = serviceRegion; } @Override public Mono resolve(final ServerWebExchange context) { return Mono.just(new CtpReactiveAuthenticationManager(meClient(apiHttpClient, context.getSession()), - credentials(), projectKey)); + credentials(), projectKey, serviceRegion)); } private ProjectApiRoot meClient(final ApiHttpClient client, final Mono session) { TokenStorage storage = new SessionTokenStorage(session); ApiRootBuilder builder = ApiRootBuilder.of(client) - .withApiBaseUrl(ServiceRegion.GCP_EUROPE_WEST1.getApiUrl()) + .withApiBaseUrl(serviceRegion.getApiUrl()) .withProjectKey(projectKey) - .withAnonymousRefreshFlow(credentials(), ServiceRegion.GCP_EUROPE_WEST1, storage); + .withAnonymousRefreshFlow(credentials(), serviceRegion, storage); return builder.build(projectKey); } diff --git a/examples/spring-datadog/src/main/java/com/commercetools/sdk/examples/springmvc/config/MeClientFilter.java b/examples/spring-datadog/src/main/java/com/commercetools/sdk/examples/springmvc/config/MeClientFilter.java index 562f8c383ad..6da0a09b815 100644 --- a/examples/spring-datadog/src/main/java/com/commercetools/sdk/examples/springmvc/config/MeClientFilter.java +++ b/examples/spring-datadog/src/main/java/com/commercetools/sdk/examples/springmvc/config/MeClientFilter.java @@ -35,13 +35,16 @@ public class MeClientFilter implements WebFilter { @Value(value = "${ctp.project.key}") private String projectKey; + private final ServiceRegion serviceRegion; + private ClientCredentials credentials() { return ClientCredentials.of().withClientId(clientId).withClientSecret(clientSecret).build(); } @Autowired - public MeClientFilter(ApiHttpClient client) { + public MeClientFilter(final ApiHttpClient client, final ServiceRegion serviceRegion) { this.client = client; + this.serviceRegion = serviceRegion; } @Override @@ -59,9 +62,9 @@ private ProjectApiRoot meClient(ApiHttpClient client, Mono session) TokenStorage storage = new SessionTokenStorage(session); ApiRootBuilder builder = ApiRootBuilder.of(client) - .withApiBaseUrl(ServiceRegion.GCP_EUROPE_WEST1.getApiUrl()) + .withApiBaseUrl(serviceRegion.getApiUrl()) .withProjectKey(projectKey) - .withAnonymousRefreshFlow(credentials(), ServiceRegion.GCP_EUROPE_WEST1, storage) + .withAnonymousRefreshFlow(credentials(), serviceRegion, storage) .withTelemetryMiddleware(new DatadogMiddleware(ApiClient.getDefaultApiClient())) .withSerializer(new DatadogResponseSerializer(ResponseSerializer.of(), ApiClient.getDefaultApiClient())); diff --git a/examples/spring-datadog/src/main/java/com/commercetools/sdk/examples/springmvc/service/CtpClientBeanService.java b/examples/spring-datadog/src/main/java/com/commercetools/sdk/examples/springmvc/service/CtpClientBeanService.java index 9681df0d5a0..3a0514fd018 100644 --- a/examples/spring-datadog/src/main/java/com/commercetools/sdk/examples/springmvc/service/CtpClientBeanService.java +++ b/examples/spring-datadog/src/main/java/com/commercetools/sdk/examples/springmvc/service/CtpClientBeanService.java @@ -2,6 +2,7 @@ import com.commercetools.api.client.ProjectApiRoot; import com.commercetools.api.defaultconfig.ApiRootBuilder; +import com.commercetools.api.defaultconfig.ServiceRegion; import com.commercetools.monitoring.datadog.DatadogMiddleware; import com.commercetools.monitoring.datadog.DatadogResponseSerializer; import com.datadog.api.client.ApiClient; @@ -15,6 +16,8 @@ import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; +import java.util.Optional; + @Configuration @EnableAutoConfiguration public class CtpClientBeanService { @@ -28,6 +31,14 @@ public class CtpClientBeanService { @Value(value = "${ctp.project.key}") private String projectKey; + @Value(value = "${ctp.service.region:#{null}}") + private String serviceRegion; + + @Bean + public ServiceRegion serviceRegion() { + return ServiceRegion.valueOf(Optional.ofNullable(this.serviceRegion).orElse("GCP_EUROPE_WEST1")); + } + private ClientCredentials credentials() { return ClientCredentials.of().withClientId(clientId).withClientSecret(clientSecret).build(); } @@ -35,7 +46,7 @@ private ClientCredentials credentials() { @Bean public ApiHttpClient client() { ApiRootBuilder builder = ApiRootBuilder.of() - .defaultClient(credentials()) + .defaultClient(credentials(), serviceRegion()) .withTelemetryMiddleware(new DatadogMiddleware(ApiClient.getDefaultApiClient())) .withSerializer(new DatadogResponseSerializer(ResponseSerializer.of(), ApiClient.getDefaultApiClient())); return builder.buildClient(); diff --git a/examples/spring-datadog/src/main/java/com/commercetools/sdk/examples/springmvc/service/CtpReactiveAuthenticationManager.java b/examples/spring-datadog/src/main/java/com/commercetools/sdk/examples/springmvc/service/CtpReactiveAuthenticationManager.java index e9965cb35f4..471bb09a86d 100644 --- a/examples/spring-datadog/src/main/java/com/commercetools/sdk/examples/springmvc/service/CtpReactiveAuthenticationManager.java +++ b/examples/spring-datadog/src/main/java/com/commercetools/sdk/examples/springmvc/service/CtpReactiveAuthenticationManager.java @@ -24,6 +24,7 @@ import reactor.core.publisher.Mono; public class CtpReactiveAuthenticationManager implements ReactiveAuthenticationManager { + private final ServiceRegion serviceRegion; VrapHttpClient client; ProjectApiRoot apiRoot; @@ -32,11 +33,12 @@ public class CtpReactiveAuthenticationManager implements ReactiveAuthenticationM private final String projectKey; public CtpReactiveAuthenticationManager(final ProjectApiRoot apiRoot, final ClientCredentials credentials, - final String projectKey) { + final String projectKey, final ServiceRegion serviceRegion) { this.apiRoot = apiRoot; this.client = HttpClientSupplier.of().get(); this.credentials = credentials; this.projectKey = projectKey; + this.serviceRegion = serviceRegion; } @Override @@ -56,7 +58,7 @@ public Mono authenticate(Authentication authentication) { GlobalCustomerPasswordTokenSupplier supplier = new GlobalCustomerPasswordTokenSupplier( credentials.getClientId(), credentials.getClientSecret(), authentication.getName(), authentication.getCredentials().toString(), null, - ServiceRegion.GCP_EUROPE_WEST1.getPasswordFlowTokenURL(projectKey), client); + serviceRegion.getPasswordFlowTokenURL(projectKey), client); return Mono.zip(Mono.fromFuture(supplier.getToken().exceptionally(throwable -> null)), Mono.just(customerSignInResultApiHttpResponse.getBody())); diff --git a/examples/spring-dynatrace-oneagent/src/main/java/com/commercetools/sdk/examples/springmvc/service/CtpClientBeanService.java b/examples/spring-dynatrace-oneagent/src/main/java/com/commercetools/sdk/examples/springmvc/service/CtpClientBeanService.java index 5ddd7cd1e29..321576dd3a7 100644 --- a/examples/spring-dynatrace-oneagent/src/main/java/com/commercetools/sdk/examples/springmvc/service/CtpClientBeanService.java +++ b/examples/spring-dynatrace-oneagent/src/main/java/com/commercetools/sdk/examples/springmvc/service/CtpClientBeanService.java @@ -3,6 +3,7 @@ import com.commercetools.api.client.ProjectScopedApiRoot; import com.commercetools.api.defaultconfig.ApiRootBuilder; +import com.commercetools.api.defaultconfig.ServiceRegion; import io.vrap.rmf.base.client.oauth2.ClientCredentials; import org.springframework.beans.factory.annotation.Value; @@ -10,6 +11,8 @@ import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; +import java.util.Optional; + @Configuration @EnableAutoConfiguration public class CtpClientBeanService { @@ -23,14 +26,22 @@ public class CtpClientBeanService { @Value(value = "${ctp.project.key}") private String projectKey; + @Value(value = "${ctp.service.region:#{null}}") + private String serviceRegion; + private ClientCredentials credentials() { return ClientCredentials.of().withClientId(clientId).withClientSecret(clientSecret).build(); } + @Bean + public ServiceRegion serviceRegion() { + return ServiceRegion.valueOf(Optional.ofNullable(this.serviceRegion).orElse("GCP_EUROPE_WEST1")); + } + @Bean public ProjectScopedApiRoot apiRoot() { ApiRootBuilder builder = ApiRootBuilder.of() - .defaultClient(credentials()); + .defaultClient(credentials(), serviceRegion()); return builder.build(projectKey); } } diff --git a/examples/spring-newrelic/src/main/java/com/commercetools/sdk/examples/springmvc/service/CtpClientBeanService.java b/examples/spring-newrelic/src/main/java/com/commercetools/sdk/examples/springmvc/service/CtpClientBeanService.java index f73cd58db5e..7e6dec8eee4 100644 --- a/examples/spring-newrelic/src/main/java/com/commercetools/sdk/examples/springmvc/service/CtpClientBeanService.java +++ b/examples/spring-newrelic/src/main/java/com/commercetools/sdk/examples/springmvc/service/CtpClientBeanService.java @@ -5,6 +5,7 @@ import com.commercetools.api.client.ProjectScopedApiRoot; import com.commercetools.api.defaultconfig.ApiRootBuilder; +import com.commercetools.api.defaultconfig.ServiceRegion; import com.commercetools.monitoring.newrelic.NewRelicContext; import com.commercetools.monitoring.newrelic.NewRelicTelemetryMiddleware; import com.commercetools.monitoring.newrelic.NewrelicResponseSerializer; @@ -19,6 +20,8 @@ import org.springframework.context.annotation.Configuration; import org.springframework.web.context.annotation.RequestScope; +import java.util.Optional; + @Configuration @EnableAutoConfiguration public class CtpClientBeanService { @@ -32,14 +35,22 @@ public class CtpClientBeanService { @Value(value = "${ctp.project.key}") private String projectKey; + @Value(value = "${ctp.service.region:#{null}}") + private String serviceRegion; + private ClientCredentials credentials() { return ClientCredentials.of().withClientId(clientId).withClientSecret(clientSecret).build(); } + @Bean + public ServiceRegion serviceRegion() { + return ServiceRegion.valueOf(Optional.ofNullable(this.serviceRegion).orElse("GCP_EUROPE_WEST1")); + } + @Bean public ApiHttpClient client() { return ApiRootBuilder.of() - .defaultClient(credentials()) + .defaultClient(credentials(), serviceRegion()) .withTelemetryMiddleware(new NewRelicTelemetryMiddleware()) .withSerializer(new NewrelicResponseSerializer(ResponseSerializer.of())) .buildClient(); diff --git a/examples/spring-otel/src/main/java/com/commercetools/sdk/examples/springmvc/service/CtpClientBeanService.java b/examples/spring-otel/src/main/java/com/commercetools/sdk/examples/springmvc/service/CtpClientBeanService.java index e20a1e63f69..a485fdc782e 100644 --- a/examples/spring-otel/src/main/java/com/commercetools/sdk/examples/springmvc/service/CtpClientBeanService.java +++ b/examples/spring-otel/src/main/java/com/commercetools/sdk/examples/springmvc/service/CtpClientBeanService.java @@ -5,6 +5,7 @@ import com.commercetools.api.client.ProjectScopedApiRoot; import com.commercetools.api.defaultconfig.ApiRootBuilder; +import com.commercetools.api.defaultconfig.ServiceRegion; import com.commercetools.monitoring.opentelemetry.OpenTelemetryMiddleware; import com.commercetools.monitoring.opentelemetry.OpenTelemetryResponseSerializer; import io.opentelemetry.api.GlobalOpenTelemetry; @@ -17,6 +18,8 @@ import org.springframework.context.annotation.Configuration; import org.springframework.web.context.annotation.RequestScope; +import java.util.Optional; + @Configuration @EnableAutoConfiguration public class CtpClientBeanService { @@ -33,14 +36,22 @@ public class CtpClientBeanService { @Value(value = "${otel.provider:local}") private OtelProvider otelProvider; + @Value(value = "${ctp.service.region:#{null}}") + private String serviceRegion; + private ClientCredentials credentials() { return ClientCredentials.of().withClientId(clientId).withClientSecret(clientSecret).build(); } + @Bean + public ServiceRegion serviceRegion() { + return ServiceRegion.valueOf(Optional.ofNullable(this.serviceRegion).orElse("GCP_EUROPE_WEST1")); + } + @Bean public ProjectScopedApiRoot apiRoot() { ApiRootBuilder builder = ApiRootBuilder.of() - .defaultClient(credentials()) + .defaultClient(credentials(), serviceRegion()) .withTelemetryMiddleware(new OpenTelemetryMiddleware(GlobalOpenTelemetry.get(), otelProvider.supportsHistogram())); if (otelProvider.useOtelSerializer()) { diff --git a/examples/spring/README.md b/examples/spring/README.md index 6a4146a5f06..7320fae3f46 100644 --- a/examples/spring/README.md +++ b/examples/spring/README.md @@ -15,7 +15,11 @@ Example to show how the ME endpoints can be used with the Java SDK in a Spring B 2. Navigate to the path `spring/`. 3. Register the client credentials in environment variables: `CTP_CLIENT_ID`, `CTP_CLIENT_SECRET` and `CTP_PROJECT_KEY` -4. If your [project region](https://docs.commercetools.com/api/general-concepts#regions) is not GCP Europe, then register [API URL](https://docs.commercetools.com/api/general-concepts#hosts) and [Auth URL](https://docs.commercetools.com/api/authorization#requesting-an-access-token-using-the-composable-commerce-oauth-20-service) in environment variables: `CTP_PROJECT_API_BASE_URL`, `CTP_PROJECT_AUTH_URL` +4. If your [project region](https://docs.commercetools.com/api/general-concepts#regions) is not GCP Europe, then register + [API URL](https://docs.commercetools.com/api/general-concepts#hosts) and [Auth URL](https://docs.commercetools.com/api/authorization#requesting-an-access-token-using-the-composable-commerce-oauth-20-service) + in environment variables: `CTP_PROJECT_API_BASE_URL`, `CTP_PROJECT_AUTH_URL` + **or** + register the service region in the environment variable `CTP_SERVICE_REGION` ## Using the ME Endpoint Checkout App diff --git a/examples/spring/src/main/java/com/commercetools/sdk/examples/spring/config/CtpSecurityConfig.java b/examples/spring/src/main/java/com/commercetools/sdk/examples/spring/config/CtpSecurityConfig.java index d95a668f4ed..d08eb33e5d6 100644 --- a/examples/spring/src/main/java/com/commercetools/sdk/examples/spring/config/CtpSecurityConfig.java +++ b/examples/spring/src/main/java/com/commercetools/sdk/examples/spring/config/CtpSecurityConfig.java @@ -110,26 +110,29 @@ public static class CtpReactiveAuthenticationManagerResolver @Value(value = "${ctp.project.auth.url:#{null}}") private String authUrl; + private final ServiceRegion serviceRegion; + private ClientCredentials credentials() { return ClientCredentials.of().withClientId(clientId).withClientSecret(clientSecret).build(); } @Autowired - public CtpReactiveAuthenticationManagerResolver(final ApiHttpClient apiHttpClient) { + public CtpReactiveAuthenticationManagerResolver(final ApiHttpClient apiHttpClient, final ServiceRegion serviceRegion) { this.apiHttpClient = apiHttpClient; + this.serviceRegion = serviceRegion; } @Override public Mono resolve(final ServerWebExchange context) { return Mono.just(new CtpReactiveAuthenticationManager(meClient(apiHttpClient, context.getSession()), - credentials(), projectKey, authUrl)); + credentials(), projectKey, authUrl, serviceRegion)); } private ProjectApiRoot meClient(final ApiHttpClient client, final Mono session) { TokenStorage storage = new SessionTokenStorage(session); ApiRootBuilder builder = ApiRootBuilder.of(client) - .withApiBaseUrl(apiBaseUrl != null ? apiBaseUrl : ServiceRegion.GCP_EUROPE_WEST1.getApiUrl()) + .withApiBaseUrl(apiBaseUrl != null ? apiBaseUrl : serviceRegion.getApiUrl()) .withProjectKey(projectKey); if (authUrl != null) { @@ -137,7 +140,7 @@ private ProjectApiRoot meClient(final ApiHttpClient client, final Mono filter(ServerWebExchange exchange, WebFilterChain chain) { private ProjectApiRoot meClient(ApiHttpClient client, Mono session) { final TokenStorage storage = new SessionTokenStorage(session); - ApiRootBuilder builder = ApiRootBuilder.of(client) - .withApiBaseUrl(apiBaseUrl != null ? apiBaseUrl : ServiceRegion.GCP_EUROPE_WEST1.getApiUrl()) + .withApiBaseUrl(apiBaseUrl != null ? apiBaseUrl : serviceRegion.getApiUrl()) .withProjectKey(projectKey); if (authUrl != null) { @@ -72,7 +75,7 @@ private ProjectApiRoot meClient(ApiHttpClient client, Mono session) authUrl + "/oauth/" + projectKey + "/anonymous/token", authUrl + "/oauth/token", storage); } else { - builder = builder.withAnonymousRefreshFlow(credentials(), ServiceRegion.GCP_EUROPE_WEST1, storage); + builder = builder.withAnonymousRefreshFlow(credentials(), serviceRegion, storage); } return builder.build(projectKey); diff --git a/examples/spring/src/main/java/com/commercetools/sdk/examples/spring/service/CtpClientBeanService.java b/examples/spring/src/main/java/com/commercetools/sdk/examples/spring/service/CtpClientBeanService.java index f575efb81f8..01e6f5ddc76 100644 --- a/examples/spring/src/main/java/com/commercetools/sdk/examples/spring/service/CtpClientBeanService.java +++ b/examples/spring/src/main/java/com/commercetools/sdk/examples/spring/service/CtpClientBeanService.java @@ -4,6 +4,7 @@ import com.commercetools.api.client.ProjectApiRoot; import com.commercetools.api.defaultconfig.ApiRootBuilder; +import com.commercetools.api.defaultconfig.ServiceRegion; import io.vrap.rmf.base.client.*; import io.vrap.rmf.base.client.oauth2.ClientCredentials; @@ -12,6 +13,8 @@ import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; +import java.util.Optional; + @Configuration @EnableAutoConfiguration public class CtpClientBeanService { @@ -31,10 +34,18 @@ public class CtpClientBeanService { @Value(value = "${ctp.project.auth.url:#{null}}") private String authUrl; + @Value(value = "${ctp.service.region:#{null}}") + private String serviceRegion; + private ClientCredentials credentials() { return ClientCredentials.of().withClientId(clientId).withClientSecret(clientSecret).build(); } + @Bean + public ServiceRegion serviceRegion() { + return ServiceRegion.valueOf(Optional.ofNullable(this.serviceRegion).orElse("GCP_EUROPE_WEST1")); + } + @Bean public ApiHttpClient client() { ApiRootBuilder builder; @@ -42,7 +53,7 @@ public ApiHttpClient client() { builder = ApiRootBuilder.of().defaultClient(credentials(), authUrl + "/oauth/token", apiBaseUrl); } else { - builder = ApiRootBuilder.of().defaultClient(credentials()); + builder = ApiRootBuilder.of().defaultClient(credentials(), serviceRegion()); } return builder.buildClient(); diff --git a/examples/spring/src/main/java/com/commercetools/sdk/examples/spring/service/CtpReactiveAuthenticationManager.java b/examples/spring/src/main/java/com/commercetools/sdk/examples/spring/service/CtpReactiveAuthenticationManager.java index d1f6c7fae25..4d663234b30 100644 --- a/examples/spring/src/main/java/com/commercetools/sdk/examples/spring/service/CtpReactiveAuthenticationManager.java +++ b/examples/spring/src/main/java/com/commercetools/sdk/examples/spring/service/CtpReactiveAuthenticationManager.java @@ -26,6 +26,7 @@ public class CtpReactiveAuthenticationManager implements ReactiveAuthenticationManager { private final String authUrl; + private final ServiceRegion serviceRegion; VrapHttpClient client; ProjectApiRoot apiRoot; @@ -34,12 +35,13 @@ public class CtpReactiveAuthenticationManager implements ReactiveAuthenticationM private final String projectKey; public CtpReactiveAuthenticationManager(final ProjectApiRoot apiRoot, final ClientCredentials credentials, - final String projectKey, final String authUrl) { + final String projectKey, final String authUrl, final ServiceRegion serviceRegion) { this.apiRoot = apiRoot; this.client = HttpClientSupplier.of().get(); this.credentials = credentials; this.projectKey = projectKey; this.authUrl = authUrl; + this.serviceRegion = serviceRegion; } @Override @@ -58,7 +60,7 @@ public Mono authenticate(Authentication authentication) { .flatMap(customerSignInResultApiHttpResponse -> { String passwordFlowTokenURL = this.authUrl != null ? this.authUrl + "/oauth/" + projectKey + "/customers/token" - : ServiceRegion.GCP_EUROPE_WEST1.getPasswordFlowTokenURL(projectKey); + : serviceRegion.getPasswordFlowTokenURL(projectKey); GlobalCustomerPasswordTokenSupplier supplier = new GlobalCustomerPasswordTokenSupplier( credentials.getClientId(), credentials.getClientSecret(), authentication.getName(), authentication.getCredentials().toString(), null, passwordFlowTokenURL, client);