io.github.wulkanowy.sdk.scrapper.interceptor.UserAgentInterceptor.kt Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of sdk-scrapper Show documentation
Show all versions of sdk-scrapper Show documentation
Unified way of retrieving data from the UONET+ register through mobile api and scraping api
package io.github.wulkanowy.sdk.scrapper.interceptor
import io.github.wulkanowy.sdk.scrapper.defaultUserAgentTemplate
import io.github.wulkanowy.sdk.scrapper.getFormattedString
import okhttp3.Interceptor
import okhttp3.Response
/**
* @see JSoup default user agent
* @see User Agent Strings - Google Chrome
*/
internal class UserAgentInterceptor(
private val androidVersion: String,
private val buildTag: String,
private val userAgentTemplate: String,
) : Interceptor {
private val userAgent by lazy {
try {
getFormattedString(
template = userAgentTemplate.ifBlank { defaultUserAgentTemplate },
androidVersion = androidVersion,
buildTag = buildTag,
)
} catch (e: Throwable) {
getFormattedString(
template = defaultUserAgentTemplate,
androidVersion = androidVersion,
buildTag = buildTag,
)
}
}
override fun intercept(chain: Interceptor.Chain): Response {
val formatted = userAgent
return chain.proceed(
chain.request().newBuilder()
.addHeader("User-Agent", formatted)
.build(),
)
}
}
© 2015 - 2024 Weber Informatics LLC | Privacy Policy