All Downloads are FREE. Search and download functionalities are using the official Maven repository.

io.github.wulkanowy.sdk.scrapper.interceptor.UserAgentInterceptor.kt Maven / Gradle / Ivy

Go to download

Unified way of retrieving data from the UONET+ register through mobile api and scraping api

There is a newer version: 2.7.0
Show newest version
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