From 5ba70b19cf56001dc2b33f5e154d1e703745423a Mon Sep 17 00:00:00 2001 From: algolia-bot Date: Fri, 6 Sep 2024 08:58:23 +0000 Subject: [PATCH] feat(clients): helper to switch API key in use (generated) https://github.com/algolia/api-clients-automation/pull/3616 Co-authored-by: algolia-bot Co-authored-by: Thomas Raffray Co-authored-by: Pierre Millot --- .../src/main/java/com/algolia/ApiClient.java | 14 +++++++++++++- .../internal/interceptors/AuthInterceptor.java | 6 +++++- 2 files changed, 18 insertions(+), 2 deletions(-) diff --git a/algoliasearch/src/main/java/com/algolia/ApiClient.java b/algoliasearch/src/main/java/com/algolia/ApiClient.java index 2052c3cae..8d6433072 100644 --- a/algoliasearch/src/main/java/com/algolia/ApiClient.java +++ b/algoliasearch/src/main/java/com/algolia/ApiClient.java @@ -15,6 +15,7 @@ import java.util.concurrent.CompletableFuture; import java.util.concurrent.ExecutorService; import java.util.stream.Collectors; +import javax.annotation.Nonnull; import org.jetbrains.annotations.Nullable; /** @@ -26,6 +27,7 @@ public abstract class ApiClient implements Closeable { private final Requester requester; private final ExecutorService executor; + private AuthInterceptor authInterceptor; /** Constructs a new instance of the {@link ApiClient}. */ protected ApiClient(String appId, String apiKey, String clientName, @Nullable ClientOptions options, List defaultHosts) { @@ -52,8 +54,9 @@ private Requester defaultRequester(String appId, String apiKey, String clientNam List statefulHosts = hosts.stream().map(StatefulHost::new).collect(Collectors.toList()); JsonSerializer serializer = JsonSerializer.builder().setCustomConfig(options.getMapperConfig()).build(); + this.authInterceptor = new AuthInterceptor(appId, apiKey); HttpRequester.Builder builder = new HttpRequester.Builder(serializer) - .addInterceptor(new AuthInterceptor(appId, apiKey)) + .addInterceptor(authInterceptor) .addInterceptor(new UserAgentInterceptor(algoliaAgent)) .addInterceptor(new RetryStrategy(statefulHosts)); if (options.getRequesterConfig() != null) { @@ -62,6 +65,15 @@ private Requester defaultRequester(String appId, String apiKey, String clientNam return builder.build(options); } + /** + * Helper method to switch the API key used to authenticate the requests. + * + * @param apiKey The new API key to be used from now on. + */ + public void setClientApiKey(@Nonnull String apiKey) { + this.authInterceptor.setApiKey(apiKey); + } + /** * Executes an HTTP request asynchronously and returns a {@link CompletableFuture} of the response * deserialized into a specified type. diff --git a/algoliasearch/src/main/java/com/algolia/internal/interceptors/AuthInterceptor.java b/algoliasearch/src/main/java/com/algolia/internal/interceptors/AuthInterceptor.java index 14c25d6a0..f7940c91d 100644 --- a/algoliasearch/src/main/java/com/algolia/internal/interceptors/AuthInterceptor.java +++ b/algoliasearch/src/main/java/com/algolia/internal/interceptors/AuthInterceptor.java @@ -12,13 +12,17 @@ public final class AuthInterceptor implements Interceptor { private static final String HEADER_API_KEY = "x-algolia-api-key"; private final String applicationId; - private final String apiKey; + private String apiKey; public AuthInterceptor(String applicationId, String apiKey) { this.applicationId = applicationId; this.apiKey = apiKey; } + public void setApiKey(String apiKey) { + this.apiKey = apiKey; + } + @Nonnull @Override public Response intercept(Chain chain) throws IOException {