Skip to content

Commit e793f94

Browse files
committed
Document public API and support more custom settings
1 parent 815cae7 commit e793f94

File tree

4 files changed

+77
-28
lines changed

4 files changed

+77
-28
lines changed
Lines changed: 64 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,82 @@
11
package com.gabrielfeo.gradle.enterprise.api
22

33
import com.gabrielfeo.gradle.enterprise.api.internal.*
4-
import retrofit2.create
4+
import java.io.File
55

6+
/**
7+
* The global instance of [GradleEnterpriseApi].
8+
*/
69
val api: GradleEnterpriseApi by lazy {
710
retrofit.create(GradleEnterpriseApi::class.java)
811
}
912

13+
/**
14+
* Provides the URL of a Gradle Enterprise API instance. By default, uses environment variable
15+
* `GRADLE_ENTERPRISE_URL`.
16+
*/
17+
var baseUrl: () -> String = {
18+
requireBaseUrl(envName = "GRADLE_ENTERPRISE_URL")
19+
}
20+
21+
/**
22+
* Provides the access token for a Gradle Enterprise API instance. By default, uses keychain entry
23+
* `gradle-enterprise-api-token` or environment variable `GRADLE_ENTERPRISE_URL`.
24+
*/
25+
var accessToken: () -> String = {
26+
requireToken(
27+
keychainName = "gradle-enterprise-api-token",
28+
envName = "GRADLE_ENTERPRISE_API_TOKEN",
29+
)
30+
}
31+
32+
/**
33+
* Shutdown the internal OkHttp client, releasing resources and allowing the program to finish
34+
* before the client's idle timeout.
35+
*
36+
* https://square.github.io/okhttp/4.x/okhttp/okhttp3/-ok-http-client/#shutdown-isnt-necessary
37+
*/
1038
fun shutdown() {
1139
okHttpClient.dispatcher.executorService.shutdownNow()
1240
}
1341

14-
var maxCacheSize = 500_000_000L
42+
/**
43+
* List of patterns matching API URLs that are OK to store in the HTTP cache. Matches by default:
44+
* - {host}/api/builds/{id}/gradle-attributes
45+
* - {host}/api/builds/{id}/maven-attributes
46+
*
47+
* By default, the Gradle Enterprise disallows HTTP caching via response headers. This library
48+
* removes such headers to forcefully allow caching, if the path is matched by any of these
49+
* patterns.
50+
*/
1551
val cacheablePaths: MutableList<Regex> = mutableListOf(
1652
""".*/api/builds/[\d\w]+/(?:gradle|maven)-attributes""".toRegex(),
1753
)
1854

19-
var baseUrl: () -> String = { requireBaseUrl() }
20-
var accessToken: () -> String = { requireToken() }
55+
/**
56+
* Maximum amount of concurrent requests allowed. Further requests will be queued. By default,
57+
* uses environment variable `GRADLE_ENTERPRISE_API_MAX_CONCURRENT_REQUESTS` or 15.
58+
*
59+
* https://square.github.io/okhttp/4.x/okhttp/okhttp3/-dispatcher
60+
*/
61+
var maxConcurrentRequests = System.getenv("GRADLE_ENTERPRISE_API_MAX_CONCURRENT_REQUESTS")?.toInt()
62+
?: 15
63+
64+
/**
65+
* Max size of the HTTP cache. By default, uses environment variable
66+
* `GRADLE_ENTERPRISE_API_MAX_CACHE_SIZE` or ~1 GB.
67+
*/
68+
var maxCacheSize = System.getenv("GRADLE_ENTERPRISE_API_MAX_CACHE_SIZE")?.toLong()
69+
?: 1_000_000_000L
70+
71+
/**
72+
* HTTP cache location. By default, uses environment variable `GRADLE_ENTERPRISE_API_CACHE_DIR`
73+
* or the system temporary folder (`java.io.tmpdir` / gradle-enterprise-api-kotlin-cache).
74+
*/
75+
var cacheDir = System.getenv("GRADLE_ENTERPRISE_API_CACHE_DIR")?.let(::File)
76+
?: File(System.getProperty("java.io.tmpdir"), "gradle-enterprise-api-kotlin-cache")
2177

22-
var maxConcurrentRequests = 15
78+
/**
79+
* Enables debug logging from the library. All logging is output to the program's standard streams.
80+
* By default, uses environment variable `GRADLE_ENTERPRISE_API_DEBUG_LOGGING` or `false`.
81+
*/
2382
var debugLoggingEnabled = System.getenv("GRADLE_ENTERPRISE_API_DEBUG_LOGGING").toBoolean()

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

Lines changed: 7 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -4,19 +4,16 @@ import com.gabrielfeo.gradle.enterprise.api.debugLoggingEnabled
44
import java.util.logging.Level.INFO
55
import java.util.logging.Logger
66

7-
private const val DEFAULT_KEY_NAME = "gradle-enterprise-api-token"
8-
private const val DEFAULT_VAR_NAME = "GRADLE_ENTERPRISE_API_TOKEN"
9-
107
internal fun requireToken(
11-
keyName: String = DEFAULT_KEY_NAME,
12-
varName: String = DEFAULT_VAR_NAME,
8+
keychainName: String,
9+
envName: String,
1310
): String {
14-
return tokenFromKeychain(keyName)
15-
?: tokenFromEnv(varName)
11+
return tokenFromKeychain(keychainName)
12+
?: tokenFromEnv(envName)
1613
?: error("""
17-
No API token. Either
18-
- create a key in macOS keychain labeled $DEFAULT_KEY_NAME
19-
- export in environment variable $DEFAULT_VAR_NAME
14+
No API token found. Either
15+
- create a key in macOS keychain labeled `gradle-enterprise-api-token`
16+
- export in environment variable `GRADLE_ENTERPRISE_API_TOKEN`
2017
- set the global property `accessToken`
2118
""".trimIndent())
2219
}

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

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

3-
private const val DEFAULT_VAR_NAME = "GRADLE_ENTERPRISE_URL"
4-
53
internal fun requireBaseUrl(
6-
varName: String = DEFAULT_VAR_NAME,
4+
envName: String,
75
): String {
8-
return checkNotNull(System.getenv(varName)) {
6+
return checkNotNull(System.getenv(envName)) {
97
"""
108
No base URL provided. Either
11-
- export an environment variable $DEFAULT_VAR_NAME
9+
- export an environment variable `GRADLE_ENTERPRISE_BASE_URL`
1210
- set the global property `baseUrl`
1311
""".trimIndent()
1412
}
Lines changed: 3 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,15 @@
11
package com.gabrielfeo.gradle.enterprise.api.internal.caching
22

3-
import com.gabrielfeo.gradle.enterprise.api.baseUrl
3+
import com.gabrielfeo.gradle.enterprise.api.cacheDir
44
import com.gabrielfeo.gradle.enterprise.api.debugLoggingEnabled
55
import com.gabrielfeo.gradle.enterprise.api.maxCacheSize
66
import okhttp3.Cache
7-
import okhttp3.HttpUrl.Companion.toHttpUrl
8-
import java.io.File
97
import java.util.logging.Level.INFO
108
import java.util.logging.Logger
119

1210
internal val cache: Cache = run {
13-
val host = baseUrl().toHttpUrl().host
14-
val tempDir = System.getProperty("java.io.tmpdir")
15-
val dir = File(tempDir, "gradle-enterprise-api-cache-$host")
1611
if (debugLoggingEnabled) {
17-
Logger.getGlobal().log(INFO, "HTTP cache dir: $dir")
12+
Logger.getGlobal().log(INFO, "HTTP cache dir with max size $maxCacheSize: $cacheDir")
1813
}
19-
Cache(dir, maxSize = maxCacheSize)
14+
Cache(cacheDir, maxSize = maxCacheSize)
2015
}

0 commit comments

Comments
 (0)