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 <[email protected]>
Co-authored-by: Thomas Raffray <[email protected]>
Co-authored-by: Pierre Millot <[email protected]>
  • Loading branch information
3 people committed Sep 6, 2024
1 parent 2288273 commit 343019b
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 2 deletions.
8 changes: 7 additions & 1 deletion src/main/scala/algoliasearch/ApiClient.scala
Original file line number Diff line number Diff line change
Expand Up @@ -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 =>
Expand All @@ -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))

Expand All @@ -89,4 +91,8 @@ abstract class ApiClient(
override def close(): Unit = {
Try(requester.close())
}

def setClientApiKey(apiKey: String): Unit = {
authInterceptor.setApiKey(apiKey)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down

0 comments on commit 343019b

Please sign in to comment.