Skip to content

Commit 9f359a9

Browse files
committed
Move options to an object
1 parent cd34e5f commit 9f359a9

File tree

8 files changed

+135
-124
lines changed

8 files changed

+135
-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()

src/main/kotlin/com/gabrielfeo/gradle/enterprise/api/GradleEnterpriseApiExtensions.kt renamed to src/main/kotlin/com/gabrielfeo/gradle/enterprise/api/ApiExtensions.kt

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

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)