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

com.bugsnag.android.gradle.internal.BugsnagHttpClientHelper.kt Maven / Gradle / Ivy

There is a newer version: 8.1.0
Show newest version
package com.bugsnag.android.gradle.internal

import com.bugsnag.android.gradle.internal.BuildServiceBugsnagHttpClientHelper.Params
import okhttp3.Interceptor
import okhttp3.OkHttpClient
import okhttp3.Response
import org.gradle.api.provider.Property
import org.gradle.api.provider.Provider
import org.gradle.api.services.BuildService
import org.gradle.api.services.BuildServiceParameters
import java.time.Duration

/** A simple API for providing a shared [OkHttpClient] instance for shared use in upload tasks. */
interface BugsnagHttpClientHelper : AutoCloseable {
    val okHttpClient: OkHttpClient

    override fun close() {
        okHttpClient.dispatcher.executorService.shutdown()
        okHttpClient.connectionPool.evictAll()
        okHttpClient.cache?.close()
    }
}

/** A [BuildService] implementation of [BugsnagHttpClientHelper]. */
abstract class BuildServiceBugsnagHttpClientHelper
    : BuildService, BugsnagHttpClientHelper {

    interface Params : BuildServiceParameters {
        val timeoutMillis: Property
        val retryCount: Property
    }

    override val okHttpClient: OkHttpClient by lazy {
        newClient(parameters.timeoutMillis.get(), parameters.retryCount.get())
    }
}

/** A simple instance-based [BugsnagHttpClientHelper] for use on Gradle <6.1. */
class LegacyBugsnagHttpClientHelper(
    timeoutMillis: Provider,
    retryCount: Provider
) : BugsnagHttpClientHelper {
    override val okHttpClient: OkHttpClient by lazy { newClient(timeoutMillis.get(), retryCount.get()) }
}

internal fun newClient(
    timeoutMillis: Long,
    retryCount: Int
): OkHttpClient {
    val timeoutDuration = Duration.ofMillis(timeoutMillis)
    val interceptor = retryInterceptor(retryCount)
    return OkHttpClient.Builder()
        .readTimeout(Duration.ZERO)
        .writeTimeout(Duration.ZERO)
        .connectTimeout(Duration.ZERO)
        .callTimeout(timeoutDuration)
        .addInterceptor(interceptor)
        .build()
}

internal fun retryInterceptor(maxRetries: Int): Interceptor {
    return object : Interceptor {
        override fun intercept(chain: Interceptor.Chain): Response {
            var attempts = 0
            var cause: Throwable?
            do {
                try {
                    val request = chain.request()
                    return chain.proceed(request)
                } catch (exc: Throwable) {
                    cause = exc
                }
                attempts++
            } while (attempts < maxRetries)
            throw IllegalStateException("Bugsnag request failed to complete", cause)
        }
    }
}





© 2015 - 2025 Weber Informatics LLC | Privacy Policy