Skip to content

Commit 343019b

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 2288273 commit 343019b

File tree

2 files changed

+12
-2
lines changed

2 files changed

+12
-2
lines changed

src/main/scala/algoliasearch/ApiClient.scala

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,8 @@ abstract class ApiClient(
4040
throw AlgoliaClientException("`apiKey` is missing.")
4141
}
4242

43+
private val authInterceptor = new AuthInterceptor(appId, apiKey)
44+
4345
private val requester = options.customRequester match {
4446
case Some(customRequester) => customRequester
4547
case None =>
@@ -62,7 +64,7 @@ abstract class ApiClient(
6264

6365
val builder = HttpRequester
6466
.builder(options.customFormats.getOrElse(formats))
65-
.withInterceptor(new AuthInterceptor(appId, apiKey))
67+
.withInterceptor(authInterceptor)
6668
.withInterceptor(new UserAgentInterceptor(algoliaAgent))
6769
.withInterceptor(new RetryStrategy(statefulHosts))
6870

@@ -89,4 +91,8 @@ abstract class ApiClient(
8991
override def close(): Unit = {
9092
Try(requester.close())
9193
}
94+
95+
def setClientApiKey(apiKey: String): Unit = {
96+
authInterceptor.setApiKey(apiKey)
97+
}
9298
}

src/main/scala/algoliasearch/internal/interceptor/AuthInterceptor.scala

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,12 +11,16 @@ import okhttp3.{Interceptor, Response}
1111
*/
1212
private[algoliasearch] class AuthInterceptor(
1313
applicationId: String,
14-
apiKey: String
14+
private var apiKey: String
1515
) extends Interceptor {
1616

1717
private val HeaderApplicationId = "x-algolia-application-id"
1818
private val HeaderApiKey = "x-algolia-api-key"
1919

20+
def setApiKey(newApiKey: String): Unit = {
21+
apiKey = newApiKey
22+
}
23+
2024
override def intercept(chain: Interceptor.Chain): Response = {
2125
val originalRequest = chain.request()
2226
val builder = originalRequest.newBuilder()

0 commit comments

Comments
 (0)