Skip to content

Commit

Permalink
Added user agent header to HTTP requests
Browse files Browse the repository at this point in the history
  • Loading branch information
Drumber committed Sep 29, 2023
1 parent 535d378 commit 62e9d61
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ import io.github.drumber.kitsune.util.deserializer.AlgoliaNumericValueDeserializ
import io.github.drumber.kitsune.util.network.AuthenticationInterceptor
import io.github.drumber.kitsune.util.network.AuthenticationInterceptorDummy
import io.github.drumber.kitsune.util.network.AuthenticationInterceptorImpl
import io.github.drumber.kitsune.util.network.UserAgentInterceptor
import okhttp3.OkHttpClient
import okhttp3.Request
import okhttp3.logging.HttpLoggingInterceptor
Expand Down Expand Up @@ -137,7 +138,8 @@ val networkModule = module {
}

private fun createHttpClientBuilder() = OkHttpClient.Builder()
.apply { if (BuildConfig.DEBUG) addInterceptor(createHttpLoggingInterceptor()) }
.addNetworkInterceptor(createUserAgentInterceptor())
.apply { if (BuildConfig.DEBUG) addNetworkInterceptor(createHttpLoggingInterceptor()) }
.connectTimeout(30, TimeUnit.SECONDS)
.readTimeout(60, TimeUnit.SECONDS)
.writeTimeout(60, TimeUnit.SECONDS)
Expand All @@ -152,6 +154,9 @@ private fun createHttpLoggingInterceptor() = HttpLoggingInterceptor().apply {
redactHeader("Authorization")
}

private fun createUserAgentInterceptor() =
UserAgentInterceptor("Kitsune/${BuildConfig.VERSION_NAME}")

private fun Scope.createAuthenticationInterceptor() = try {
AuthenticationInterceptorImpl(get())
} catch (t: Throwable) {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
package io.github.drumber.kitsune.util.network

import okhttp3.Interceptor
import okhttp3.Response

class UserAgentInterceptor(private val userAgent: String) : Interceptor {

override fun intercept(chain: Interceptor.Chain): Response {
val request = chain.request().newBuilder()
.header("User-Agent", userAgent)
.build()
return chain.proceed(request)
}
}

0 comments on commit 62e9d61

Please sign in to comment.