Skip to content

Commit

Permalink
Rename Options to Config (#68)
Browse files Browse the repository at this point in the history
  • Loading branch information
gabrielfeo authored May 22, 2023
1 parent d2360b9 commit 5e48330
Show file tree
Hide file tree
Showing 9 changed files with 64 additions and 64 deletions.
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -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()
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -70,10 +70,10 @@ data class Options(
?: 60_000L,

/**
* See [CacheOptions].
* See [CacheConfig].
*/
val cacheOptions: CacheOptions =
CacheOptions(),
val cacheConfig: CacheConfig =
CacheConfig(),
) {

/**
Expand Down Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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.
Expand All @@ -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,
)
Expand Down
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -13,55 +13,55 @@ 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)")
}
return Cache(cacheDir, maxSize)
}

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,
)
Original file line number Diff line number Diff line change
@@ -1,18 +1,18 @@
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
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)
Expand Down
Original file line number Diff line number Diff line change
@@ -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() {
Expand All @@ -15,23 +14,23 @@ 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",
)
}

@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",
)
}

@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",
)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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() {
Expand All @@ -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
Expand All @@ -48,35 +48,35 @@ 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()
}
}

@Test
fun `Given Linux and no env token, fails`() {
systemProperties = FakeSystemProperties.linux
assertFails {
Options().apiToken()
Config().apiToken()
}
}

@Test
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)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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()
)
}
Expand Down

0 comments on commit 5e48330

Please sign in to comment.