Skip to content

Commit 5e48330

Browse files
authored
Rename Options to Config (#68)
1 parent d2360b9 commit 5e48330

File tree

9 files changed

+64
-64
lines changed

9 files changed

+64
-64
lines changed

examples/example-project/app/src/main/kotlin/com/gabrielfeo/gradle/enterprise/api/example/Main.kt

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
package com.gabrielfeo.gradle.enterprise.api.example
22

3+
import com.gabrielfeo.gradle.enterprise.api.Config
34
import com.gabrielfeo.gradle.enterprise.api.GradleEnterpriseApi
45
import com.gabrielfeo.gradle.enterprise.api.example.analysis.mostFrequentBuilds
56
import okhttp3.OkHttpClient
@@ -13,10 +14,10 @@ import okhttp3.OkHttpClient
1314
val clientBuilder = OkHttpClient.Builder()
1415

1516
suspend fun main() {
16-
val newOptions = GradleEnterpriseApi.options.copy(
17+
val newConfig = Config(
1718
clientBuilder = clientBuilder,
1819
)
19-
val gradleEnterpriseApi = GradleEnterpriseApi.newInstance(newOptions)
20+
val gradleEnterpriseApi = GradleEnterpriseApi.newInstance(newConfig)
2021
runAllAnalysis(gradleEnterpriseApi)
2122
gradleEnterpriseApi.shutdown()
2223
}

library/src/main/kotlin/com/gabrielfeo/gradle/enterprise/api/Options.kt renamed to library/src/main/kotlin/com/gabrielfeo/gradle/enterprise/api/Config.kt

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ import kotlin.time.Duration.Companion.days
1111
* Library configuration options.
1212
*/
1313
@Suppress("MemberVisibilityCanBePrivate", "unused")
14-
data class Options(
14+
data class Config(
1515

1616
/**
1717
* Enables debug logging from the library. All logging is output to stderr. By default, uses
@@ -70,10 +70,10 @@ data class Options(
7070
?: 60_000L,
7171

7272
/**
73-
* See [CacheOptions].
73+
* See [CacheConfig].
7474
*/
75-
val cacheOptions: CacheOptions =
76-
CacheOptions(),
75+
val cacheConfig: CacheConfig =
76+
CacheConfig(),
7777
) {
7878

7979
/**
@@ -118,7 +118,7 @@ data class Options(
118118
* itself is upgraded, cache should be [clear]ed.
119119
*/
120120
@Suppress("MemberVisibilityCanBePrivate")
121-
data class CacheOptions(
121+
data class CacheConfig(
122122

123123
/**
124124
* Whether caching is enabled. By default, uses environment variable

library/src/main/kotlin/com/gabrielfeo/gradle/enterprise/api/GradleEnterpriseApi.kt

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ import retrofit2.create
2222
* GradleEnterpriseApi.buildsApi.getBuilds(...)
2323
* ```
2424
*
25-
* However, if you need to change [options] at runtime or own the instance's lifecycle (e.g.
25+
* However, if you need to change [config] at runtime or own the instance's lifecycle (e.g.
2626
* with an IoC container like Dagger), create a new instance:
2727
*
2828
* ```kotlin
@@ -41,7 +41,7 @@ interface GradleEnterpriseApi {
4141
/**
4242
* Library configuration options.
4343
*/
44-
val options: Options
44+
val config: Config
4545

4646
/**
4747
* Release resources allowing the program to finish before the internal client's idle timeout.
@@ -57,24 +57,24 @@ interface GradleEnterpriseApi {
5757
/**
5858
* Create a new instance of `GradleEnterpriseApi` with new options.
5959
*/
60-
fun newInstance(options: Options): GradleEnterpriseApi {
61-
return RealGradleEnterpriseApi(options)
60+
fun newInstance(config: Config): GradleEnterpriseApi {
61+
return RealGradleEnterpriseApi(config)
6262
}
6363
}
6464

6565
}
6666

6767
private class RealGradleEnterpriseApi(
68-
override val options: Options = Options(),
68+
override val config: Config = Config(),
6969
) : GradleEnterpriseApi {
7070

7171
private val okHttpClient by lazy {
72-
buildOkHttpClient(options = options)
72+
buildOkHttpClient(config = config)
7373
}
7474

7575
private val retrofit: Retrofit by lazy {
7676
buildRetrofit(
77-
options,
77+
config,
7878
okHttpClient,
7979
Serializer.moshi,
8080
)
Lines changed: 25 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
package com.gabrielfeo.gradle.enterprise.api.internal
22

3-
import com.gabrielfeo.gradle.enterprise.api.Options
3+
import com.gabrielfeo.gradle.enterprise.api.Config
44
import com.gabrielfeo.gradle.enterprise.api.internal.auth.HttpBearerAuth
55
import com.gabrielfeo.gradle.enterprise.api.internal.caching.CacheEnforcingInterceptor
66
import com.gabrielfeo.gradle.enterprise.api.internal.caching.CacheHitLoggingInterceptor
@@ -13,55 +13,55 @@ import java.util.logging.Level
1313
import java.util.logging.Logger
1414

1515
internal fun buildOkHttpClient(
16-
options: Options,
17-
) = with(options.clientBuilder) {
18-
readTimeout(Duration.ofMillis(options.readTimeoutMillis))
19-
if (options.cacheOptions.cacheEnabled) {
20-
cache(buildCache(options))
16+
config: Config,
17+
) = with(config.clientBuilder) {
18+
readTimeout(Duration.ofMillis(config.readTimeoutMillis))
19+
if (config.cacheConfig.cacheEnabled) {
20+
cache(buildCache(config))
2121
}
22-
addInterceptors(options)
23-
addNetworkInterceptors(options)
22+
addInterceptors(config)
23+
addNetworkInterceptors(config)
2424
build().apply {
25-
options.maxConcurrentRequests?.let {
25+
config.maxConcurrentRequests?.let {
2626
dispatcher.maxRequests = it
2727
dispatcher.maxRequestsPerHost = it
2828
}
2929
}
3030
}
3131

32-
private fun OkHttpClient.Builder.addInterceptors(options: Options) {
33-
if (options.debugLoggingEnabled && options.cacheOptions.cacheEnabled) {
32+
private fun OkHttpClient.Builder.addInterceptors(config: Config) {
33+
if (config.debugLoggingEnabled && config.cacheConfig.cacheEnabled) {
3434
addInterceptor(CacheHitLoggingInterceptor())
3535
}
3636
}
3737

38-
private fun OkHttpClient.Builder.addNetworkInterceptors(options: Options) {
39-
if (options.cacheOptions.cacheEnabled) {
40-
addNetworkInterceptor(buildCacheEnforcingInterceptor(options))
38+
private fun OkHttpClient.Builder.addNetworkInterceptors(config: Config) {
39+
if (config.cacheConfig.cacheEnabled) {
40+
addNetworkInterceptor(buildCacheEnforcingInterceptor(config))
4141
}
42-
if (options.debugLoggingEnabled) {
42+
if (config.debugLoggingEnabled) {
4343
addNetworkInterceptor(HttpLoggingInterceptor().apply { level = BODY })
4444
}
45-
addNetworkInterceptor(HttpBearerAuth("bearer", options.apiToken()))
45+
addNetworkInterceptor(HttpBearerAuth("bearer", config.apiToken()))
4646
}
4747

4848
internal fun buildCache(
49-
options: Options
49+
config: Config
5050
): Cache {
51-
val cacheDir = options.cacheOptions.cacheDir
52-
val maxSize = options.cacheOptions.maxCacheSize
53-
if (options.debugLoggingEnabled) {
51+
val cacheDir = config.cacheConfig.cacheDir
52+
val maxSize = config.cacheConfig.maxCacheSize
53+
if (config.debugLoggingEnabled) {
5454
val logger = Logger.getGlobal()
5555
logger.log(Level.INFO, "HTTP cache dir: $cacheDir (max ${maxSize}B)")
5656
}
5757
return Cache(cacheDir, maxSize)
5858
}
5959

6060
private fun buildCacheEnforcingInterceptor(
61-
options: Options,
61+
config: Config,
6262
) = CacheEnforcingInterceptor(
63-
longTermCacheUrlPattern = options.cacheOptions.longTermCacheUrlPattern,
64-
longTermCacheMaxAge = options.cacheOptions.longTermCacheMaxAge,
65-
shortTermCacheUrlPattern = options.cacheOptions.shortTermCacheUrlPattern,
66-
shortTermCacheMaxAge = options.cacheOptions.shortTermCacheMaxAge,
63+
longTermCacheUrlPattern = config.cacheConfig.longTermCacheUrlPattern,
64+
longTermCacheMaxAge = config.cacheConfig.longTermCacheMaxAge,
65+
shortTermCacheUrlPattern = config.cacheConfig.shortTermCacheUrlPattern,
66+
shortTermCacheMaxAge = config.cacheConfig.shortTermCacheMaxAge,
6767
)

library/src/main/kotlin/com/gabrielfeo/gradle/enterprise/api/internal/Retrofit.kt

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,18 @@
11
package com.gabrielfeo.gradle.enterprise.api.internal
22

3-
import com.gabrielfeo.gradle.enterprise.api.Options
3+
import com.gabrielfeo.gradle.enterprise.api.Config
44
import com.squareup.moshi.Moshi
55
import okhttp3.OkHttpClient
66
import retrofit2.Retrofit
77
import retrofit2.converter.moshi.MoshiConverterFactory
88
import retrofit2.converter.scalars.ScalarsConverterFactory
99

1010
internal fun buildRetrofit(
11-
options: Options,
11+
config: Config,
1212
client: OkHttpClient,
1313
moshi: Moshi,
1414
) = with(Retrofit.Builder()) {
15-
val url = options.apiUrl
15+
val url = config.apiUrl
1616
check("/api/" in url) { "A valid API URL must end in /api/" }
1717
val instanceUrl = url.substringBefore("api/")
1818
baseUrl(instanceUrl)

library/src/test/kotlin/com/gabrielfeo/gradle/enterprise/api/CacheOptionsTest.kt renamed to library/src/test/kotlin/com/gabrielfeo/gradle/enterprise/api/CacheConfigTest.kt

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,9 @@
11
package com.gabrielfeo.gradle.enterprise.api
22

33
import com.gabrielfeo.gradle.enterprise.api.internal.*
4-
import org.junit.jupiter.api.assertDoesNotThrow
54
import kotlin.test.*
65

7-
class CacheOptionsTest {
6+
class CacheConfigTest {
87

98
@BeforeTest
109
fun before() {
@@ -15,23 +14,23 @@ class CacheOptionsTest {
1514

1615
@Test
1716
fun `default longTermCacheUrlPattern matches attributes URLs`() {
18-
Options.CacheOptions().longTermCacheUrlPattern.assertMatches(
17+
Config.CacheConfig().longTermCacheUrlPattern.assertMatches(
1918
"https://ge.gradle.org/api/builds/tgnsqkb2rhlni/gradle-attributes",
2019
"https://ge.gradle.org/api/builds/tgnsqkb2rhlni/maven-attributes",
2120
)
2221
}
2322

2423
@Test
2524
fun `default longTermCacheUrlPattern matches build cache performance URLs`() {
26-
Options.CacheOptions().longTermCacheUrlPattern.assertMatches(
25+
Config.CacheConfig().longTermCacheUrlPattern.assertMatches(
2726
"https://ge.gradle.org/api/builds/tgnsqkb2rhlni/gradle-build-cache-performance",
2827
"https://ge.gradle.org/api/builds/tgnsqkb2rhlni/maven-build-cache-performance",
2928
)
3029
}
3130

3231
@Test
3332
fun `default shortTermCacheUrlPattern matches builds URLs`() {
34-
Options.CacheOptions().shortTermCacheUrlPattern.assertMatches(
33+
Config.CacheConfig().shortTermCacheUrlPattern.assertMatches(
3534
"https://ge.gradle.org/api/builds?since=0",
3635
"https://ge.gradle.org/api/builds?since=0&maxBuilds=2",
3736
)

library/src/test/kotlin/com/gabrielfeo/gradle/enterprise/api/OptionsTest.kt renamed to library/src/test/kotlin/com/gabrielfeo/gradle/enterprise/api/ConfigTest.kt

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ import com.gabrielfeo.gradle.enterprise.api.internal.*
44
import org.junit.jupiter.api.assertDoesNotThrow
55
import kotlin.test.*
66

7-
class OptionsTest {
7+
class ConfigTest {
88

99
@BeforeTest
1010
fun before() {
@@ -17,27 +17,27 @@ class OptionsTest {
1717
fun `Given no URL set in env, error`() {
1818
env = FakeEnv()
1919
assertFails {
20-
Options()
20+
Config()
2121
}
2222
}
2323

2424
@Test
2525
fun `Given URL set in env, apiUrl is env URL`() {
2626
(env as FakeEnv)["GRADLE_ENTERPRISE_API_URL"] = "https://example.com/api/"
27-
assertEquals("https://example.com/api/", Options().apiUrl)
27+
assertEquals("https://example.com/api/", Config().apiUrl)
2828
}
2929

3030
@Test
3131
fun `Given macOS and keychain token, keychain token used`() {
3232
(env as FakeEnv)["GRADLE_ENTERPRISE_API_TOKEN"] = "bar"
3333
keychain = FakeKeychain("gradle-enterprise-api-token" to "foo")
34-
assertEquals("foo", Options().apiToken())
34+
assertEquals("foo", Config().apiToken())
3535
}
3636

3737
@Test
3838
fun `Given macOS but no keychain token, env token used`() {
3939
(env as FakeEnv)["GRADLE_ENTERPRISE_API_TOKEN"] = "bar"
40-
assertEquals("bar", Options().apiToken())
40+
assertEquals("bar", Config().apiToken())
4141
}
4242

4343
@Test
@@ -48,35 +48,35 @@ class OptionsTest {
4848
error("Error: Tried to access macOS keychain in Linux")
4949
}
5050
systemProperties = FakeSystemProperties.linux
51-
assertEquals("bar", Options().apiToken())
51+
assertEquals("bar", Config().apiToken())
5252
}
5353

5454
@Test
5555
fun `Given macOS and no token anywhere, error`() {
5656
assertFails {
57-
Options().apiToken()
57+
Config().apiToken()
5858
}
5959
}
6060

6161
@Test
6262
fun `Given Linux and no env token, fails`() {
6363
systemProperties = FakeSystemProperties.linux
6464
assertFails {
65-
Options().apiToken()
65+
Config().apiToken()
6666
}
6767
}
6868

6969
@Test
7070
fun `maxConcurrentRequests accepts int`() {
7171
(env as FakeEnv)["GRADLE_ENTERPRISE_API_MAX_CONCURRENT_REQUESTS"] = "1"
7272
assertDoesNotThrow {
73-
Options().maxConcurrentRequests
73+
Config().maxConcurrentRequests
7474
}
7575
}
7676

7777
@Test
7878
fun `Given timeout set in env, readTimeoutMillis returns env value`() {
7979
(env as FakeEnv)["GRADLE_ENTERPRISE_API_READ_TIMEOUT_MILLIS"] = "100000"
80-
assertEquals(100_000L, Options().readTimeoutMillis)
80+
assertEquals(100_000L, Config().readTimeoutMillis)
8181
}
8282
}

library/src/test/kotlin/com/gabrielfeo/gradle/enterprise/api/OkHttpClientTest.kt

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -92,10 +92,10 @@ class OkHttpClientTest {
9292
env = fakeEnv
9393
systemProperties = FakeSystemProperties.macOs
9494
keychain = FakeKeychain()
95-
val options = when (clientBuilder) {
96-
null -> Options()
97-
else -> Options(clientBuilder = clientBuilder)
95+
val config = when (clientBuilder) {
96+
null -> Config()
97+
else -> Config(clientBuilder = clientBuilder)
9898
}
99-
return buildOkHttpClient(options)
99+
return buildOkHttpClient(config)
100100
}
101101
}

library/src/test/kotlin/com/gabrielfeo/gradle/enterprise/api/RetrofitTest.kt

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -34,10 +34,10 @@ class RetrofitTest {
3434
env = fakeEnv
3535
systemProperties = FakeSystemProperties.macOs
3636
keychain = FakeKeychain()
37-
val options = Options()
37+
val config = Config()
3838
return buildRetrofit(
39-
options = options,
40-
client = buildOkHttpClient(options),
39+
config = config,
40+
client = buildOkHttpClient(config),
4141
moshi = Moshi.Builder().build()
4242
)
4343
}

0 commit comments

Comments
 (0)