Skip to content

Commit 5905c5f

Browse files
committed
Move options to an object
1 parent 611c406 commit 5905c5f

File tree

8 files changed

+131
-124
lines changed

8 files changed

+131
-124
lines changed

src/main/kotlin/com/gabrielfeo/gradle/enterprise/api/Api.kt

Lines changed: 0 additions & 95 deletions
Original file line numberDiff line numberDiff line change
@@ -13,25 +13,6 @@ val api: GradleEnterpriseApi by lazy {
1313
retrofit.create(GradleEnterpriseApi::class.java)
1414
}
1515

16-
/**
17-
* Provides the URL of a Gradle Enterprise API instance. By default, uses environment variable
18-
* `GRADLE_ENTERPRISE_URL`.
19-
*/
20-
var baseUrl: () -> String = {
21-
requireBaseUrl(envName = "GRADLE_ENTERPRISE_URL")
22-
}
23-
24-
/**
25-
* Provides the access token for a Gradle Enterprise API instance. By default, uses keychain entry
26-
* `gradle-enterprise-api-token` or environment variable `GRADLE_ENTERPRISE_URL`.
27-
*/
28-
var accessToken: () -> String = {
29-
requireToken(
30-
keychainName = "gradle-enterprise-api-token",
31-
envName = "GRADLE_ENTERPRISE_API_TOKEN",
32-
)
33-
}
34-
3516
/**
3617
* Shutdown the internal client, releasing resources and allowing the program to
3718
* finish before the client's idle timeout.
@@ -45,79 +26,3 @@ fun shutdown() {
4526
cache?.close();
4627
}
4728
}
48-
49-
/**
50-
* Regex pattern to match API URLs that are OK to store long-term in the HTTP cache, up to
51-
* [longTermCacheMaxAge] (1y by default, max value). By default, uses environment variable
52-
* `GRADLE_ENTERPRISE_API_LONG_TERM_CACHE_URL_PATTERN` or a pattern matching:
53-
* - {host}/api/builds/{id}/gradle-attributes
54-
* - {host}/api/builds/{id}/maven-attributes
55-
*
56-
* Gradle Enterprise API disallows HTTP caching, but this library forcefully removes such
57-
* restriction.
58-
*
59-
* Use `|` to define multiple patterns in one, e.g. `.*gradle-attributes|.*test-distribution`.
60-
*/
61-
var longTermCacheUrlPattern: Regex =
62-
System.getenv("GRADLE_ENTERPRISE_API_LONG_TERM_CACHE_URL_PATTERN")?.toRegex()
63-
?: """.*/api/builds/[\d\w]+/(?:gradle|maven)-attributes""".toRegex()
64-
65-
/**
66-
* Max age in seconds for URLs to be cached long-term (matched by [longTermCacheUrlPattern]).
67-
* By default, uses environment variable `GRADLE_ENTERPRISE_API_LONG_TERM_CACHE_MAX_AGE` or 1 year.
68-
*/
69-
var longTermCacheMaxAge: Long =
70-
System.getenv("GRADLE_ENTERPRISE_API_SHORT_TERM_CACHE_MAX_AGE")?.toLong()
71-
?: 365.days.inWholeSeconds
72-
73-
/**
74-
* Regex pattern to match API URLs that are OK to store short-term in the HTTP cache, up to
75-
* [shortTermCacheMaxAge] (1d by default). By default, uses environment variable
76-
* `GRADLE_ENTERPRISE_API_SHORT_TERM_CACHE_URL_PATTERN` or a pattern matching:
77-
* - {host}/api/builds
78-
*
79-
* Gradle Enterprise API disallows HTTP caching, but this library forcefully removes such
80-
* restriction.
81-
*
82-
* Use `|` to define multiple patterns in one, e.g. `.*gradle-attributes|.*test-distribution`.
83-
*/
84-
var shortTermCacheUrlPattern: Regex =
85-
System.getenv("GRADLE_ENTERPRISE_API_SHORT_TERM_CACHE_URL_PATTERN")?.toRegex()
86-
?: """.*/builds(?:\?.*|\Z)""".toRegex()
87-
88-
/**
89-
* Max age in seconds for URLs to be cached short-term (matched by [shortTermCacheUrlPattern]).
90-
* By default, uses environment variable `GRADLE_ENTERPRISE_API_SHORT_TERM_CACHE_MAX_AGE` or 1 day.
91-
*/
92-
var shortTermCacheMaxAge: Long =
93-
System.getenv("GRADLE_ENTERPRISE_API_SHORT_TERM_CACHE_MAX_AGE")?.toLong()
94-
?: 1.days.inWholeSeconds
95-
96-
/**
97-
* Maximum amount of concurrent requests allowed. Further requests will be queued. By default,
98-
* uses environment variable `GRADLE_ENTERPRISE_API_MAX_CONCURRENT_REQUESTS` or 15.
99-
*
100-
* https://square.github.io/okhttp/4.x/okhttp/okhttp3/-dispatcher
101-
*/
102-
var maxConcurrentRequests = System.getenv("GRADLE_ENTERPRISE_API_MAX_CONCURRENT_REQUESTS")?.toInt()
103-
?: 15
104-
105-
/**
106-
* Max size of the HTTP cache. By default, uses environment variable
107-
* `GRADLE_ENTERPRISE_API_MAX_CACHE_SIZE` or ~1 GB.
108-
*/
109-
var maxCacheSize = System.getenv("GRADLE_ENTERPRISE_API_MAX_CACHE_SIZE")?.toLong()
110-
?: 1_000_000_000L
111-
112-
/**
113-
* HTTP cache location. By default, uses environment variable `GRADLE_ENTERPRISE_API_CACHE_DIR`
114-
* or the system temporary folder (`java.io.tmpdir` / gradle-enterprise-api-kotlin-cache).
115-
*/
116-
var cacheDir = System.getenv("GRADLE_ENTERPRISE_API_CACHE_DIR")?.let(::File)
117-
?: File(System.getProperty("java.io.tmpdir"), "gradle-enterprise-api-kotlin-cache")
118-
119-
/**
120-
* Enables debug logging from the library. All logging is output to stderr. By default, uses
121-
* environment variable `GRADLE_ENTERPRISE_API_DEBUG_LOGGING` or `false`.
122-
*/
123-
var debugLoggingEnabled = System.getenv("GRADLE_ENTERPRISE_API_DEBUG_LOGGING").toBoolean()
Lines changed: 108 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,108 @@
1+
package com.gabrielfeo.gradle.enterprise.api
2+
3+
import com.gabrielfeo.gradle.enterprise.api.internal.requireBaseUrl
4+
import com.gabrielfeo.gradle.enterprise.api.internal.requireToken
5+
import java.io.File
6+
import kotlin.time.Duration.Companion.days
7+
8+
object Options {
9+
10+
/**
11+
* Provides the URL of a Gradle Enterprise API instance. By default, uses environment variable
12+
* `GRADLE_ENTERPRISE_URL`.
13+
*/
14+
var baseUrl: () -> String = {
15+
requireBaseUrl(envName = "GRADLE_ENTERPRISE_URL")
16+
}
17+
18+
/**
19+
* Provides the access token for a Gradle Enterprise API instance. By default, uses keychain entry
20+
* `gradle-enterprise-api-token` or environment variable `GRADLE_ENTERPRISE_URL`.
21+
*/
22+
var accessToken: () -> String = {
23+
requireToken(
24+
keychainName = "gradle-enterprise-api-token",
25+
envName = "GRADLE_ENTERPRISE_API_TOKEN",
26+
)
27+
}
28+
29+
/**
30+
* Maximum amount of concurrent requests allowed. Further requests will be queued. By default,
31+
* uses environment variable `GRADLE_ENTERPRISE_API_MAX_CONCURRENT_REQUESTS` or 15.
32+
*
33+
* https://square.github.io/okhttp/4.x/okhttp/okhttp3/-dispatcher
34+
*/
35+
var maxConcurrentRequests =
36+
System.getenv("GRADLE_ENTERPRISE_API_MAX_CONCURRENT_REQUESTS")?.toInt()
37+
?: 15
38+
39+
/**
40+
* HTTP cache location. By default, uses environment variable `GRADLE_ENTERPRISE_API_CACHE_DIR`
41+
* or the system temporary folder (`java.io.tmpdir` / gradle-enterprise-api-kotlin-cache).
42+
*/
43+
var cacheDir =
44+
System.getenv("GRADLE_ENTERPRISE_API_CACHE_DIR")?.let(::File)
45+
?: File(System.getProperty("java.io.tmpdir"), "gradle-enterprise-api-kotlin-cache")
46+
47+
/**
48+
* Max size of the HTTP cache. By default, uses environment variable
49+
* `GRADLE_ENTERPRISE_API_MAX_CACHE_SIZE` or ~1 GB.
50+
*/
51+
var maxCacheSize =
52+
System.getenv("GRADLE_ENTERPRISE_API_MAX_CACHE_SIZE")?.toLong()
53+
?: 1_000_000_000L
54+
55+
/**
56+
* Regex pattern to match API URLs that are OK to store long-term in the HTTP cache, up to
57+
* [longTermCacheMaxAge] (1y by default, max value). By default, uses environment variable
58+
* `GRADLE_ENTERPRISE_API_LONG_TERM_CACHE_URL_PATTERN` or a pattern matching:
59+
* - {host}/api/builds/{id}/gradle-attributes
60+
* - {host}/api/builds/{id}/maven-attributes
61+
*
62+
* Gradle Enterprise API disallows HTTP caching, but this library forcefully removes such
63+
* restriction.
64+
*
65+
* Use `|` to define multiple patterns in one, e.g. `.*gradle-attributes|.*test-distribution`.
66+
*/
67+
var longTermCacheUrlPattern: Regex =
68+
System.getenv("GRADLE_ENTERPRISE_API_LONG_TERM_CACHE_URL_PATTERN")?.toRegex()
69+
?: """.*/api/builds/[\d\w]+/(?:gradle|maven)-attributes""".toRegex()
70+
71+
/**
72+
* Max age in seconds for URLs to be cached long-term (matched by [longTermCacheUrlPattern]).
73+
* By default, uses environment variable `GRADLE_ENTERPRISE_API_LONG_TERM_CACHE_MAX_AGE` or 1 year.
74+
*/
75+
var longTermCacheMaxAge: Long =
76+
System.getenv("GRADLE_ENTERPRISE_API_SHORT_TERM_CACHE_MAX_AGE")?.toLong()
77+
?: 365.days.inWholeSeconds
78+
79+
/**
80+
* Regex pattern to match API URLs that are OK to store short-term in the HTTP cache, up to
81+
* [shortTermCacheMaxAge] (1d by default). By default, uses environment variable
82+
* `GRADLE_ENTERPRISE_API_SHORT_TERM_CACHE_URL_PATTERN` or a pattern matching:
83+
* - {host}/api/builds
84+
*
85+
* Gradle Enterprise API disallows HTTP caching, but this library forcefully removes such
86+
* restriction.
87+
*
88+
* Use `|` to define multiple patterns in one, e.g. `.*gradle-attributes|.*test-distribution`.
89+
*/
90+
var shortTermCacheUrlPattern: Regex =
91+
System.getenv("GRADLE_ENTERPRISE_API_SHORT_TERM_CACHE_URL_PATTERN")?.toRegex()
92+
?: """.*/builds(?:\?.*|\Z)""".toRegex()
93+
94+
/**
95+
* Max age in seconds for URLs to be cached short-term (matched by [shortTermCacheUrlPattern]).
96+
* By default, uses environment variable `GRADLE_ENTERPRISE_API_SHORT_TERM_CACHE_MAX_AGE` or 1 day.
97+
*/
98+
var shortTermCacheMaxAge: Long =
99+
System.getenv("GRADLE_ENTERPRISE_API_SHORT_TERM_CACHE_MAX_AGE")?.toLong()
100+
?: 1.days.inWholeSeconds
101+
102+
/**
103+
* Enables debug logging from the library. All logging is output to stderr. By default, uses
104+
* environment variable `GRADLE_ENTERPRISE_API_DEBUG_LOGGING` or `false`.
105+
*/
106+
var debugLoggingEnabled =
107+
System.getenv("GRADLE_ENTERPRISE_API_DEBUG_LOGGING").toBoolean()
108+
}

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

Lines changed: 3 additions & 3 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.debugLoggingEnabled
3+
import com.gabrielfeo.gradle.enterprise.api.Options
44
import java.util.logging.Level.INFO
55
import java.util.logging.Logger
66

@@ -20,7 +20,7 @@ internal fun requireToken(
2020

2121
private fun tokenFromEnv(varName: String): String? {
2222
return System.getenv(varName).also {
23-
if (debugLoggingEnabled && it.isNullOrBlank()) {
23+
if (Options.debugLoggingEnabled && it.isNullOrBlank()) {
2424
Logger.getGlobal().log(INFO, "Env var $varName=$it")
2525
}
2626
}
@@ -36,7 +36,7 @@ private fun tokenFromKeychain(keyName: String): String? {
3636
return process.inputStream.bufferedReader().use {
3737
it.readText().trim()
3838
}
39-
} else if (debugLoggingEnabled) {
39+
} else if (Options.debugLoggingEnabled) {
4040
Logger.getGlobal().log(INFO, "Failed to get key from keychain (exit $status)")
4141
}
4242
return null
Lines changed: 10 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,33 +1,28 @@
11
package com.gabrielfeo.gradle.enterprise.api.internal
22

3+
import com.gabrielfeo.gradle.enterprise.api.*
34
import com.gabrielfeo.gradle.enterprise.api.internal.auth.HttpBearerAuth
45
import com.gabrielfeo.gradle.enterprise.api.internal.caching.CacheEnforcingInterceptor
56
import com.gabrielfeo.gradle.enterprise.api.internal.caching.CacheHitLoggingInterceptor
6-
import com.gabrielfeo.gradle.enterprise.api.internal.caching.cache
7-
import com.gabrielfeo.gradle.enterprise.api.accessToken
8-
import com.gabrielfeo.gradle.enterprise.api.longTermCacheMaxAge
9-
import com.gabrielfeo.gradle.enterprise.api.longTermCacheUrlPattern
10-
import com.gabrielfeo.gradle.enterprise.api.maxConcurrentRequests
11-
import com.gabrielfeo.gradle.enterprise.api.shortTermCacheMaxAge
12-
import com.gabrielfeo.gradle.enterprise.api.shortTermCacheUrlPattern
7+
import com.gabrielfeo.gradle.enterprise.api.internal.caching.buildCache
138
import okhttp3.OkHttpClient
149

1510
internal val okHttpClient: OkHttpClient by lazy {
1611
OkHttpClient.Builder()
17-
.cache(cache)
18-
.addInterceptor(HttpBearerAuth("bearer", accessToken()))
12+
.cache(buildCache())
13+
.addInterceptor(HttpBearerAuth("bearer", Options.accessToken()))
1914
.addInterceptor(CacheHitLoggingInterceptor())
2015
.addNetworkInterceptor(buildCacheEnforcingInterceptor())
2116
.build()
2217
.apply {
23-
dispatcher.maxRequests = maxConcurrentRequests
24-
dispatcher.maxRequestsPerHost = maxConcurrentRequests
18+
dispatcher.maxRequests = Options.maxConcurrentRequests
19+
dispatcher.maxRequestsPerHost = Options.maxConcurrentRequests
2520
}
2621
}
2722

2823
private fun buildCacheEnforcingInterceptor() = CacheEnforcingInterceptor(
29-
longTermCacheUrlPattern = longTermCacheUrlPattern,
30-
longTermCacheMaxAge = longTermCacheMaxAge,
31-
shortTermCacheUrlPattern = shortTermCacheUrlPattern,
32-
shortTermCacheMaxAge = shortTermCacheMaxAge,
24+
longTermCacheUrlPattern = Options.longTermCacheUrlPattern,
25+
longTermCacheMaxAge = Options.longTermCacheMaxAge,
26+
shortTermCacheUrlPattern = Options.shortTermCacheUrlPattern,
27+
shortTermCacheMaxAge = Options.shortTermCacheMaxAge,
3328
)

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

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

3-
import com.gabrielfeo.gradle.enterprise.api.baseUrl
3+
import com.gabrielfeo.gradle.enterprise.api.Options
44
import com.gabrielfeo.gradle.enterprise.api.internal.infrastructure.Serializer
55
import retrofit2.Retrofit
66
import retrofit2.converter.moshi.MoshiConverterFactory
77
import retrofit2.converter.scalars.ScalarsConverterFactory
88

99
internal val retrofit: Retrofit by lazy {
1010
Retrofit.Builder()
11-
.baseUrl(baseUrl())
11+
.baseUrl(Options.baseUrl())
1212
.addConverterFactory(ScalarsConverterFactory.create())
1313
.addConverterFactory(MoshiConverterFactory.create(Serializer.moshi))
1414
.client(okHttpClient)
Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,14 @@
11
package com.gabrielfeo.gradle.enterprise.api.internal.caching
22

3-
import com.gabrielfeo.gradle.enterprise.api.cacheDir
4-
import com.gabrielfeo.gradle.enterprise.api.debugLoggingEnabled
5-
import com.gabrielfeo.gradle.enterprise.api.maxCacheSize
3+
import com.gabrielfeo.gradle.enterprise.api.Options
64
import okhttp3.Cache
75
import java.util.logging.Level.INFO
86
import java.util.logging.Logger
97

10-
internal val cache: Cache = run {
11-
if (debugLoggingEnabled) {
12-
Logger.getGlobal().log(INFO, "HTTP cache dir with max size $maxCacheSize: $cacheDir")
8+
internal fun buildCache(): Cache {
9+
if (Options.debugLoggingEnabled) {
10+
val logger = Logger.getGlobal()
11+
logger.log(INFO, "HTTP cache dir: ${Options.cacheDir} (max ${Options.maxCacheSize}B)")
1312
}
14-
Cache(cacheDir, maxSize = maxCacheSize)
13+
return Cache(Options.cacheDir, maxSize = Options.maxCacheSize)
1514
}

src/main/kotlin/com/gabrielfeo/gradle/enterprise/api/internal/caching/CacheHitLoggingInterceptor.kt

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

3-
import com.gabrielfeo.gradle.enterprise.api.debugLoggingEnabled
3+
import com.gabrielfeo.gradle.enterprise.api.Options
44
import okhttp3.Interceptor
55
import okhttp3.Response
66
import java.util.logging.Level
@@ -11,7 +11,7 @@ internal class CacheHitLoggingInterceptor(
1111
) : Interceptor {
1212

1313
override fun intercept(chain: Interceptor.Chain): Response {
14-
if (!debugLoggingEnabled) {
14+
if (!Options.debugLoggingEnabled) {
1515
return chain.proceed(chain.request())
1616
}
1717
val url = chain.request().url

0 commit comments

Comments
 (0)