Skip to content

Commit

Permalink
feat(clients): helper to switch API key in use (generated)
Browse files Browse the repository at this point in the history
algolia/api-clients-automation#3616

Co-authored-by: algolia-bot <accounts+algolia-api-client-bot@algolia.com>
Co-authored-by: Thomas Raffray <Fluf22@users.noreply.github.com>
Co-authored-by: Pierre Millot <pierre.millot@algolia.com>
  • Loading branch information
3 people committed Sep 6, 2024
1 parent 205d78f commit 5ba70b1
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 2 deletions.
14 changes: 13 additions & 1 deletion algoliasearch/src/main/java/com/algolia/ApiClient.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;

/**
Expand All @@ -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<Host> defaultHosts) {
Expand All @@ -52,8 +54,9 @@ private Requester defaultRequester(String appId, String apiKey, String clientNam
List<StatefulHost> 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) {
Expand All @@ -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.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down

0 comments on commit 5ba70b1

Please sign in to comment.