diff --git a/examples/example-project/app/src/main/kotlin/com/gabrielfeo/gradle/enterprise/api/example/Main.kt b/examples/example-project/app/src/main/kotlin/com/gabrielfeo/gradle/enterprise/api/example/Main.kt index ef590032..f21edf2b 100644 --- a/examples/example-project/app/src/main/kotlin/com/gabrielfeo/gradle/enterprise/api/example/Main.kt +++ b/examples/example-project/app/src/main/kotlin/com/gabrielfeo/gradle/enterprise/api/example/Main.kt @@ -1,5 +1,6 @@ package com.gabrielfeo.gradle.enterprise.api.example +import com.gabrielfeo.gradle.enterprise.api.Config import com.gabrielfeo.gradle.enterprise.api.GradleEnterpriseApi import com.gabrielfeo.gradle.enterprise.api.example.analysis.mostFrequentBuilds import okhttp3.OkHttpClient @@ -13,10 +14,10 @@ import okhttp3.OkHttpClient val clientBuilder = OkHttpClient.Builder() suspend fun main() { - val newOptions = GradleEnterpriseApi.options.copy( + val newConfig = Config( clientBuilder = clientBuilder, ) - val gradleEnterpriseApi = GradleEnterpriseApi.newInstance(newOptions) + val gradleEnterpriseApi = GradleEnterpriseApi.newInstance(newConfig) runAllAnalysis(gradleEnterpriseApi) gradleEnterpriseApi.shutdown() } diff --git a/library/src/main/kotlin/com/gabrielfeo/gradle/enterprise/api/Options.kt b/library/src/main/kotlin/com/gabrielfeo/gradle/enterprise/api/Config.kt similarity index 98% rename from library/src/main/kotlin/com/gabrielfeo/gradle/enterprise/api/Options.kt rename to library/src/main/kotlin/com/gabrielfeo/gradle/enterprise/api/Config.kt index e923ff86..8e5c7e8d 100644 --- a/library/src/main/kotlin/com/gabrielfeo/gradle/enterprise/api/Options.kt +++ b/library/src/main/kotlin/com/gabrielfeo/gradle/enterprise/api/Config.kt @@ -11,7 +11,7 @@ import kotlin.time.Duration.Companion.days * Library configuration options. */ @Suppress("MemberVisibilityCanBePrivate", "unused") -data class Options( +data class Config( /** * Enables debug logging from the library. All logging is output to stderr. By default, uses @@ -70,10 +70,10 @@ data class Options( ?: 60_000L, /** - * See [CacheOptions]. + * See [CacheConfig]. */ - val cacheOptions: CacheOptions = - CacheOptions(), + val cacheConfig: CacheConfig = + CacheConfig(), ) { /** @@ -118,7 +118,7 @@ data class Options( * itself is upgraded, cache should be [clear]ed. */ @Suppress("MemberVisibilityCanBePrivate") - data class CacheOptions( + data class CacheConfig( /** * Whether caching is enabled. By default, uses environment variable diff --git a/library/src/main/kotlin/com/gabrielfeo/gradle/enterprise/api/GradleEnterpriseApi.kt b/library/src/main/kotlin/com/gabrielfeo/gradle/enterprise/api/GradleEnterpriseApi.kt index 2ff3ec44..cffebc4e 100644 --- a/library/src/main/kotlin/com/gabrielfeo/gradle/enterprise/api/GradleEnterpriseApi.kt +++ b/library/src/main/kotlin/com/gabrielfeo/gradle/enterprise/api/GradleEnterpriseApi.kt @@ -22,7 +22,7 @@ import retrofit2.create * GradleEnterpriseApi.buildsApi.getBuilds(...) * ``` * - * However, if you need to change [options] at runtime or own the instance's lifecycle (e.g. + * However, if you need to change [config] at runtime or own the instance's lifecycle (e.g. * with an IoC container like Dagger), create a new instance: * * ```kotlin @@ -41,7 +41,7 @@ interface GradleEnterpriseApi { /** * Library configuration options. */ - val options: Options + val config: Config /** * Release resources allowing the program to finish before the internal client's idle timeout. @@ -57,24 +57,24 @@ interface GradleEnterpriseApi { /** * Create a new instance of `GradleEnterpriseApi` with new options. */ - fun newInstance(options: Options): GradleEnterpriseApi { - return RealGradleEnterpriseApi(options) + fun newInstance(config: Config): GradleEnterpriseApi { + return RealGradleEnterpriseApi(config) } } } private class RealGradleEnterpriseApi( - override val options: Options = Options(), + override val config: Config = Config(), ) : GradleEnterpriseApi { private val okHttpClient by lazy { - buildOkHttpClient(options = options) + buildOkHttpClient(config = config) } private val retrofit: Retrofit by lazy { buildRetrofit( - options, + config, okHttpClient, Serializer.moshi, ) diff --git a/library/src/main/kotlin/com/gabrielfeo/gradle/enterprise/api/internal/OkHttpClient.kt b/library/src/main/kotlin/com/gabrielfeo/gradle/enterprise/api/internal/OkHttpClient.kt index ed7734a6..5e96bfbe 100644 --- a/library/src/main/kotlin/com/gabrielfeo/gradle/enterprise/api/internal/OkHttpClient.kt +++ b/library/src/main/kotlin/com/gabrielfeo/gradle/enterprise/api/internal/OkHttpClient.kt @@ -1,6 +1,6 @@ package com.gabrielfeo.gradle.enterprise.api.internal -import com.gabrielfeo.gradle.enterprise.api.Options +import com.gabrielfeo.gradle.enterprise.api.Config import com.gabrielfeo.gradle.enterprise.api.internal.auth.HttpBearerAuth import com.gabrielfeo.gradle.enterprise.api.internal.caching.CacheEnforcingInterceptor import com.gabrielfeo.gradle.enterprise.api.internal.caching.CacheHitLoggingInterceptor @@ -13,44 +13,44 @@ import java.util.logging.Level import java.util.logging.Logger internal fun buildOkHttpClient( - options: Options, -) = with(options.clientBuilder) { - readTimeout(Duration.ofMillis(options.readTimeoutMillis)) - if (options.cacheOptions.cacheEnabled) { - cache(buildCache(options)) + config: Config, +) = with(config.clientBuilder) { + readTimeout(Duration.ofMillis(config.readTimeoutMillis)) + if (config.cacheConfig.cacheEnabled) { + cache(buildCache(config)) } - addInterceptors(options) - addNetworkInterceptors(options) + addInterceptors(config) + addNetworkInterceptors(config) build().apply { - options.maxConcurrentRequests?.let { + config.maxConcurrentRequests?.let { dispatcher.maxRequests = it dispatcher.maxRequestsPerHost = it } } } -private fun OkHttpClient.Builder.addInterceptors(options: Options) { - if (options.debugLoggingEnabled && options.cacheOptions.cacheEnabled) { +private fun OkHttpClient.Builder.addInterceptors(config: Config) { + if (config.debugLoggingEnabled && config.cacheConfig.cacheEnabled) { addInterceptor(CacheHitLoggingInterceptor()) } } -private fun OkHttpClient.Builder.addNetworkInterceptors(options: Options) { - if (options.cacheOptions.cacheEnabled) { - addNetworkInterceptor(buildCacheEnforcingInterceptor(options)) +private fun OkHttpClient.Builder.addNetworkInterceptors(config: Config) { + if (config.cacheConfig.cacheEnabled) { + addNetworkInterceptor(buildCacheEnforcingInterceptor(config)) } - if (options.debugLoggingEnabled) { + if (config.debugLoggingEnabled) { addNetworkInterceptor(HttpLoggingInterceptor().apply { level = BODY }) } - addNetworkInterceptor(HttpBearerAuth("bearer", options.apiToken())) + addNetworkInterceptor(HttpBearerAuth("bearer", config.apiToken())) } internal fun buildCache( - options: Options + config: Config ): Cache { - val cacheDir = options.cacheOptions.cacheDir - val maxSize = options.cacheOptions.maxCacheSize - if (options.debugLoggingEnabled) { + val cacheDir = config.cacheConfig.cacheDir + val maxSize = config.cacheConfig.maxCacheSize + if (config.debugLoggingEnabled) { val logger = Logger.getGlobal() logger.log(Level.INFO, "HTTP cache dir: $cacheDir (max ${maxSize}B)") } @@ -58,10 +58,10 @@ internal fun buildCache( } private fun buildCacheEnforcingInterceptor( - options: Options, + config: Config, ) = CacheEnforcingInterceptor( - longTermCacheUrlPattern = options.cacheOptions.longTermCacheUrlPattern, - longTermCacheMaxAge = options.cacheOptions.longTermCacheMaxAge, - shortTermCacheUrlPattern = options.cacheOptions.shortTermCacheUrlPattern, - shortTermCacheMaxAge = options.cacheOptions.shortTermCacheMaxAge, + longTermCacheUrlPattern = config.cacheConfig.longTermCacheUrlPattern, + longTermCacheMaxAge = config.cacheConfig.longTermCacheMaxAge, + shortTermCacheUrlPattern = config.cacheConfig.shortTermCacheUrlPattern, + shortTermCacheMaxAge = config.cacheConfig.shortTermCacheMaxAge, ) diff --git a/library/src/main/kotlin/com/gabrielfeo/gradle/enterprise/api/internal/Retrofit.kt b/library/src/main/kotlin/com/gabrielfeo/gradle/enterprise/api/internal/Retrofit.kt index 0ed25c64..359f9f2a 100644 --- a/library/src/main/kotlin/com/gabrielfeo/gradle/enterprise/api/internal/Retrofit.kt +++ b/library/src/main/kotlin/com/gabrielfeo/gradle/enterprise/api/internal/Retrofit.kt @@ -1,6 +1,6 @@ package com.gabrielfeo.gradle.enterprise.api.internal -import com.gabrielfeo.gradle.enterprise.api.Options +import com.gabrielfeo.gradle.enterprise.api.Config import com.squareup.moshi.Moshi import okhttp3.OkHttpClient import retrofit2.Retrofit @@ -8,11 +8,11 @@ import retrofit2.converter.moshi.MoshiConverterFactory import retrofit2.converter.scalars.ScalarsConverterFactory internal fun buildRetrofit( - options: Options, + config: Config, client: OkHttpClient, moshi: Moshi, ) = with(Retrofit.Builder()) { - val url = options.apiUrl + val url = config.apiUrl check("/api/" in url) { "A valid API URL must end in /api/" } val instanceUrl = url.substringBefore("api/") baseUrl(instanceUrl) diff --git a/library/src/test/kotlin/com/gabrielfeo/gradle/enterprise/api/CacheOptionsTest.kt b/library/src/test/kotlin/com/gabrielfeo/gradle/enterprise/api/CacheConfigTest.kt similarity index 81% rename from library/src/test/kotlin/com/gabrielfeo/gradle/enterprise/api/CacheOptionsTest.kt rename to library/src/test/kotlin/com/gabrielfeo/gradle/enterprise/api/CacheConfigTest.kt index 9b128509..c5f1bcef 100644 --- a/library/src/test/kotlin/com/gabrielfeo/gradle/enterprise/api/CacheOptionsTest.kt +++ b/library/src/test/kotlin/com/gabrielfeo/gradle/enterprise/api/CacheConfigTest.kt @@ -1,10 +1,9 @@ package com.gabrielfeo.gradle.enterprise.api import com.gabrielfeo.gradle.enterprise.api.internal.* -import org.junit.jupiter.api.assertDoesNotThrow import kotlin.test.* -class CacheOptionsTest { +class CacheConfigTest { @BeforeTest fun before() { @@ -15,7 +14,7 @@ class CacheOptionsTest { @Test fun `default longTermCacheUrlPattern matches attributes URLs`() { - Options.CacheOptions().longTermCacheUrlPattern.assertMatches( + Config.CacheConfig().longTermCacheUrlPattern.assertMatches( "https://ge.gradle.org/api/builds/tgnsqkb2rhlni/gradle-attributes", "https://ge.gradle.org/api/builds/tgnsqkb2rhlni/maven-attributes", ) @@ -23,7 +22,7 @@ class CacheOptionsTest { @Test fun `default longTermCacheUrlPattern matches build cache performance URLs`() { - Options.CacheOptions().longTermCacheUrlPattern.assertMatches( + Config.CacheConfig().longTermCacheUrlPattern.assertMatches( "https://ge.gradle.org/api/builds/tgnsqkb2rhlni/gradle-build-cache-performance", "https://ge.gradle.org/api/builds/tgnsqkb2rhlni/maven-build-cache-performance", ) @@ -31,7 +30,7 @@ class CacheOptionsTest { @Test fun `default shortTermCacheUrlPattern matches builds URLs`() { - Options.CacheOptions().shortTermCacheUrlPattern.assertMatches( + Config.CacheConfig().shortTermCacheUrlPattern.assertMatches( "https://ge.gradle.org/api/builds?since=0", "https://ge.gradle.org/api/builds?since=0&maxBuilds=2", ) diff --git a/library/src/test/kotlin/com/gabrielfeo/gradle/enterprise/api/OptionsTest.kt b/library/src/test/kotlin/com/gabrielfeo/gradle/enterprise/api/ConfigTest.kt similarity index 82% rename from library/src/test/kotlin/com/gabrielfeo/gradle/enterprise/api/OptionsTest.kt rename to library/src/test/kotlin/com/gabrielfeo/gradle/enterprise/api/ConfigTest.kt index ee8799cc..769a54e1 100644 --- a/library/src/test/kotlin/com/gabrielfeo/gradle/enterprise/api/OptionsTest.kt +++ b/library/src/test/kotlin/com/gabrielfeo/gradle/enterprise/api/ConfigTest.kt @@ -4,7 +4,7 @@ import com.gabrielfeo.gradle.enterprise.api.internal.* import org.junit.jupiter.api.assertDoesNotThrow import kotlin.test.* -class OptionsTest { +class ConfigTest { @BeforeTest fun before() { @@ -17,27 +17,27 @@ class OptionsTest { fun `Given no URL set in env, error`() { env = FakeEnv() assertFails { - Options() + Config() } } @Test fun `Given URL set in env, apiUrl is env URL`() { (env as FakeEnv)["GRADLE_ENTERPRISE_API_URL"] = "https://example.com/api/" - assertEquals("https://example.com/api/", Options().apiUrl) + assertEquals("https://example.com/api/", Config().apiUrl) } @Test fun `Given macOS and keychain token, keychain token used`() { (env as FakeEnv)["GRADLE_ENTERPRISE_API_TOKEN"] = "bar" keychain = FakeKeychain("gradle-enterprise-api-token" to "foo") - assertEquals("foo", Options().apiToken()) + assertEquals("foo", Config().apiToken()) } @Test fun `Given macOS but no keychain token, env token used`() { (env as FakeEnv)["GRADLE_ENTERPRISE_API_TOKEN"] = "bar" - assertEquals("bar", Options().apiToken()) + assertEquals("bar", Config().apiToken()) } @Test @@ -48,13 +48,13 @@ class OptionsTest { error("Error: Tried to access macOS keychain in Linux") } systemProperties = FakeSystemProperties.linux - assertEquals("bar", Options().apiToken()) + assertEquals("bar", Config().apiToken()) } @Test fun `Given macOS and no token anywhere, error`() { assertFails { - Options().apiToken() + Config().apiToken() } } @@ -62,7 +62,7 @@ class OptionsTest { fun `Given Linux and no env token, fails`() { systemProperties = FakeSystemProperties.linux assertFails { - Options().apiToken() + Config().apiToken() } } @@ -70,13 +70,13 @@ class OptionsTest { fun `maxConcurrentRequests accepts int`() { (env as FakeEnv)["GRADLE_ENTERPRISE_API_MAX_CONCURRENT_REQUESTS"] = "1" assertDoesNotThrow { - Options().maxConcurrentRequests + Config().maxConcurrentRequests } } @Test fun `Given timeout set in env, readTimeoutMillis returns env value`() { (env as FakeEnv)["GRADLE_ENTERPRISE_API_READ_TIMEOUT_MILLIS"] = "100000" - assertEquals(100_000L, Options().readTimeoutMillis) + assertEquals(100_000L, Config().readTimeoutMillis) } } \ No newline at end of file diff --git a/library/src/test/kotlin/com/gabrielfeo/gradle/enterprise/api/OkHttpClientTest.kt b/library/src/test/kotlin/com/gabrielfeo/gradle/enterprise/api/OkHttpClientTest.kt index c8f422d2..41f68205 100644 --- a/library/src/test/kotlin/com/gabrielfeo/gradle/enterprise/api/OkHttpClientTest.kt +++ b/library/src/test/kotlin/com/gabrielfeo/gradle/enterprise/api/OkHttpClientTest.kt @@ -92,10 +92,10 @@ class OkHttpClientTest { env = fakeEnv systemProperties = FakeSystemProperties.macOs keychain = FakeKeychain() - val options = when (clientBuilder) { - null -> Options() - else -> Options(clientBuilder = clientBuilder) + val config = when (clientBuilder) { + null -> Config() + else -> Config(clientBuilder = clientBuilder) } - return buildOkHttpClient(options) + return buildOkHttpClient(config) } } diff --git a/library/src/test/kotlin/com/gabrielfeo/gradle/enterprise/api/RetrofitTest.kt b/library/src/test/kotlin/com/gabrielfeo/gradle/enterprise/api/RetrofitTest.kt index 0b7f7ef3..b3f23c84 100644 --- a/library/src/test/kotlin/com/gabrielfeo/gradle/enterprise/api/RetrofitTest.kt +++ b/library/src/test/kotlin/com/gabrielfeo/gradle/enterprise/api/RetrofitTest.kt @@ -34,10 +34,10 @@ class RetrofitTest { env = fakeEnv systemProperties = FakeSystemProperties.macOs keychain = FakeKeychain() - val options = Options() + val config = Config() return buildRetrofit( - options = options, - client = buildOkHttpClient(options), + config = config, + client = buildOkHttpClient(config), moshi = Moshi.Builder().build() ) }