15
15
import java .util .concurrent .CompletableFuture ;
16
16
import java .util .concurrent .ExecutorService ;
17
17
import java .util .stream .Collectors ;
18
+ import javax .annotation .Nonnull ;
18
19
import org .jetbrains .annotations .Nullable ;
19
20
20
21
/**
@@ -26,6 +27,7 @@ public abstract class ApiClient implements Closeable {
26
27
27
28
private final Requester requester ;
28
29
private final ExecutorService executor ;
30
+ private AuthInterceptor authInterceptor ;
29
31
30
32
/** Constructs a new instance of the {@link ApiClient}. */
31
33
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
52
54
List <StatefulHost > statefulHosts = hosts .stream ().map (StatefulHost ::new ).collect (Collectors .toList ());
53
55
54
56
JsonSerializer serializer = JsonSerializer .builder ().setCustomConfig (options .getMapperConfig ()).build ();
57
+ this .authInterceptor = new AuthInterceptor (appId , apiKey );
55
58
HttpRequester .Builder builder = new HttpRequester .Builder (serializer )
56
- .addInterceptor (new AuthInterceptor ( appId , apiKey ) )
59
+ .addInterceptor (authInterceptor )
57
60
.addInterceptor (new UserAgentInterceptor (algoliaAgent ))
58
61
.addInterceptor (new RetryStrategy (statefulHosts ));
59
62
if (options .getRequesterConfig () != null ) {
@@ -62,6 +65,15 @@ private Requester defaultRequester(String appId, String apiKey, String clientNam
62
65
return builder .build (options );
63
66
}
64
67
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
+
65
77
/**
66
78
* Executes an HTTP request asynchronously and returns a {@link CompletableFuture} of the response
67
79
* deserialized into a specified type.
0 commit comments