Skip to content

Commit 5ba70b1

Browse files
algolia-botFluf22millotp
committed
feat(clients): helper to switch API key in use (generated)
algolia/api-clients-automation#3616 Co-authored-by: algolia-bot <[email protected]> Co-authored-by: Thomas Raffray <[email protected]> Co-authored-by: Pierre Millot <[email protected]>
1 parent 205d78f commit 5ba70b1

File tree

2 files changed

+18
-2
lines changed

2 files changed

+18
-2
lines changed

algoliasearch/src/main/java/com/algolia/ApiClient.java

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
import java.util.concurrent.CompletableFuture;
1616
import java.util.concurrent.ExecutorService;
1717
import java.util.stream.Collectors;
18+
import javax.annotation.Nonnull;
1819
import org.jetbrains.annotations.Nullable;
1920

2021
/**
@@ -26,6 +27,7 @@ public abstract class ApiClient implements Closeable {
2627

2728
private final Requester requester;
2829
private final ExecutorService executor;
30+
private AuthInterceptor authInterceptor;
2931

3032
/** Constructs a new instance of the {@link ApiClient}. */
3133
protected ApiClient(String appId, String apiKey, String clientName, @Nullable ClientOptions options, List<Host> defaultHosts) {
@@ -52,8 +54,9 @@ private Requester defaultRequester(String appId, String apiKey, String clientNam
5254
List<StatefulHost> statefulHosts = hosts.stream().map(StatefulHost::new).collect(Collectors.toList());
5355

5456
JsonSerializer serializer = JsonSerializer.builder().setCustomConfig(options.getMapperConfig()).build();
57+
this.authInterceptor = new AuthInterceptor(appId, apiKey);
5558
HttpRequester.Builder builder = new HttpRequester.Builder(serializer)
56-
.addInterceptor(new AuthInterceptor(appId, apiKey))
59+
.addInterceptor(authInterceptor)
5760
.addInterceptor(new UserAgentInterceptor(algoliaAgent))
5861
.addInterceptor(new RetryStrategy(statefulHosts));
5962
if (options.getRequesterConfig() != null) {
@@ -62,6 +65,15 @@ private Requester defaultRequester(String appId, String apiKey, String clientNam
6265
return builder.build(options);
6366
}
6467

68+
/**
69+
* Helper method to switch the API key used to authenticate the requests.
70+
*
71+
* @param apiKey The new API key to be used from now on.
72+
*/
73+
public void setClientApiKey(@Nonnull String apiKey) {
74+
this.authInterceptor.setApiKey(apiKey);
75+
}
76+
6577
/**
6678
* Executes an HTTP request asynchronously and returns a {@link CompletableFuture} of the response
6779
* deserialized into a specified type.

algoliasearch/src/main/java/com/algolia/internal/interceptors/AuthInterceptor.java

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,13 +12,17 @@ public final class AuthInterceptor implements Interceptor {
1212
private static final String HEADER_API_KEY = "x-algolia-api-key";
1313

1414
private final String applicationId;
15-
private final String apiKey;
15+
private String apiKey;
1616

1717
public AuthInterceptor(String applicationId, String apiKey) {
1818
this.applicationId = applicationId;
1919
this.apiKey = apiKey;
2020
}
2121

22+
public void setApiKey(String apiKey) {
23+
this.apiKey = apiKey;
24+
}
25+
2226
@Nonnull
2327
@Override
2428
public Response intercept(Chain chain) throws IOException {

0 commit comments

Comments
 (0)