diff --git a/src/main/scala/algoliasearch/ApiClient.scala b/src/main/scala/algoliasearch/ApiClient.scala index 6d10169c..79bee859 100644 --- a/src/main/scala/algoliasearch/ApiClient.scala +++ b/src/main/scala/algoliasearch/ApiClient.scala @@ -40,6 +40,8 @@ abstract class ApiClient( throw AlgoliaClientException("`apiKey` is missing.") } + private val authInterceptor = new AuthInterceptor(appId, apiKey) + private val requester = options.customRequester match { case Some(customRequester) => customRequester case None => @@ -62,7 +64,7 @@ abstract class ApiClient( val builder = HttpRequester .builder(options.customFormats.getOrElse(formats)) - .withInterceptor(new AuthInterceptor(appId, apiKey)) + .withInterceptor(authInterceptor) .withInterceptor(new UserAgentInterceptor(algoliaAgent)) .withInterceptor(new RetryStrategy(statefulHosts)) @@ -89,4 +91,8 @@ abstract class ApiClient( override def close(): Unit = { Try(requester.close()) } + + def setClientApiKey(apiKey: String): Unit = { + authInterceptor.setApiKey(apiKey) + } } diff --git a/src/main/scala/algoliasearch/internal/interceptor/AuthInterceptor.scala b/src/main/scala/algoliasearch/internal/interceptor/AuthInterceptor.scala index 2d32bb8b..b9b72f17 100644 --- a/src/main/scala/algoliasearch/internal/interceptor/AuthInterceptor.scala +++ b/src/main/scala/algoliasearch/internal/interceptor/AuthInterceptor.scala @@ -11,12 +11,16 @@ import okhttp3.{Interceptor, Response} */ private[algoliasearch] class AuthInterceptor( applicationId: String, - apiKey: String + private var apiKey: String ) extends Interceptor { private val HeaderApplicationId = "x-algolia-application-id" private val HeaderApiKey = "x-algolia-api-key" + def setApiKey(newApiKey: String): Unit = { + apiKey = newApiKey + } + override def intercept(chain: Interceptor.Chain): Response = { val originalRequest = chain.request() val builder = originalRequest.newBuilder()