11# Gradle Enterprise API Kotlin
22
3+ [ ![ Release] ( https://jitpack.io/v/gabrielfeo/gradle-enterprise-api-kotlin.svg )] ( https://jitpack.io/#gabrielfeo/gradle-enterprise-api-kotlin )
4+ [ ![ Javadoc] ( https://img.shields.io/badge/javadoc-0.9-orange )] ( https://gabrielfeo.github.io/gradle-enterprise-api-kotlin/ )
5+
36A Kotlin library to access the [ Gradle Enterprise API] [ 1 ] , easy to use from Kotlin
47scripts:
58
69``` kotlin
7- val builds = api.getBuilds(since = 0 , maxBuilds = 10 ).execute().body()!!
8- builds.forEach {
10+ api.getBuilds(since = yesterday).forEach {
911 println (it)
1012}
1113```
1214
1315## Setup
1416
15- Set up your environment once and use the library from any script in your machine.
17+ Set up once and use the library from any script in your machine:
1618
1719- ` GRADLE_ENTERPRISE_URL ` environment variable: the URL of your Gradle Enterprise instance
1820- ` GRADLE_ENTERPRISE_API_TOKEN ` environment variable: an API access token for the Gradle
2628@file:Repository(" https://jitpack.io" )
2729@file:DependsOn(" com.github.gabrielfeo:gradle-enterprise-api-kotlin:1.0" )
2830
29- val builds = api.getBuilds(since = 0 , maxBuilds = 10 ).execute().body()!!
30- builds.forEach {
31- println (it)
32- }
31+ api.getBuild(id = " hy5nxbzfjxe5k" )
3332```
3433
35- See the [ sample script] ( ./sample.main.kts ) for a complete example.
36-
37- <details >
38- <summary >Optional setup</summary >
39-
40- All of the following have default values and are completely optional. See
41- [ Api.kt] ( src/main/kotlin/com/gabrielfeo/gradle/enterprise/api/Api.kt ) for details.
42-
43- ##### Caching
44-
45- Gradle Enterprise API disallows HTTP caching, but this library forces
46- caching for faster queries. Caching is split in two categories:
47-
48- 1 . Short-term cache (default max-age of 1 day)
49- - ` /api/builds `
50- 2 . Long-term cache (default max-age of 1 year)
51- - ` /api/builds/{id}/gradle-attributes `
52- - ` /api/builds/{id}/maven-attributes `
53-
54- max-age and cached URLs can be changed with options below.
55-
56- - ` GRADLE_ENTERPRISE_API_CACHE_DIR ` : HTTP cache location. Defaults to the system temporary
57- directory.
58- - ` GRADLE_ENTERPRISE_API_MAX_CACHE_SIZE ` : Max size of the HTTP cache in bytes. Defaults to ~ 1GB.
59- - ` GRADLE_ENTERPRISE_API_SHORT_TERM_CACHE_URL_PATTERN ` : Regex pattern to match API URLs that are
60- OK to store short-term in the HTTP cache.
61- - ` GRADLE_ENTERPRISE_API_SHORT_TERM_CACHE_MAX_AGE ` : Max age in seconds of each response stored
62- short-term in the HTTP cache.
63- - ` GRADLE_ENTERPRISE_API_LONG_TERM_CACHE_URL_PATTERN ` : Regex pattern to match API URLs that are
64- OK to store long-term in the HTTP cache.
65- - ` GRADLE_ENTERPRISE_API_LONG_TERM_CACHE_MAX_AGE ` : Max age in seconds of each response stored
66- long-term in the HTTP cache.
67-
68- ##### Concurrency
69-
70- - ` GRADLE_ENTERPRISE_API_MAX_CONCURRENT_REQUESTS ` : Maximum amount of concurrent requests
71- allowed. Defaults to 15.
72-
73- ##### Debugging
74-
75- - ` GRADLE_ENTERPRISE_API_DEBUG_LOGGING ` : ` true ` to enable debug logging from the library. Defaults
76- to ` false ` .
77-
78- </details >
34+ For configuring base URL and token via code and other available options, see the
35+ [ ` Options ` object] [ 8 ] .
7936
8037<details >
8138 <summary >Setup in full projects (non-scripts)</summary >
@@ -104,63 +61,43 @@ See the [sample script](./sample.main.kts) for a complete example.
10461 ```
10562
10663 </details >
107-
108- Any option can also be changed from code rather than from environment, as long as it's done
109- before the first ` api ` usage.
110-
111- ``` kotlin
112- baseUrl = { " https://my.ge.org" }
113- accessToken = { getFromVault(" ge-api-token" ) }
114- api.getBuilds(id = " hy5nxbzfjxe5k" )
115- ```
116-
11764</details >
11865
11966## Usage
12067
121- API endpoints are provided as a single interface: ` GradleEnterpriseApi ` . It's
68+ API endpoints are provided as a single interface: [ ` GradleEnterpriseApi ` ] [ 9 ] . The Javadoc is a
69+ the same as Gradle's online docs, as they're generated from the same spec. An instance is
12270initialized and ready-to-use as the global ` api ` instance:
12371
12472``` kotlin
12573api.getBuild(id = " hy5nxbzfjxe5k" )
12674```
12775
128- It's recommended to learn about endpoints and their responses through IDE auto-complete. Javadoc
129- appearing in auto-complete is the full API manual, same as Gradle's online docs.
130-
131- This library provides a few extension functions on top of the regular API:
76+ The library also provides a few extension functions on top of the regular API, such as paged
77+ requests and joining. See [ ` GradleEnterpriseApi ` extensions] [ 10 ] .
13278
13379``` kotlin
134- // Regular query to /api/builds, limited to 1000 builds server-side
80+ // Standard query to /api/builds, limited to 1000 builds server-side
13581api.getBuilds(since = lastMonth)
136- // Streams all available builds from a given date, split in as getBuilds
137- // as needed
82+ // Extension: Streams all available builds since given date (paging underneath)
13883api.getBuildsFlow(since = lastMonth)
13984```
14085
86+ It's recommended to call [ ` shutdown() ` ] [ 11 ] at the end of scripts to release resources and let the
87+ program exit. Otherwise, it'll keep running for an extra ~ 60s after code finishes, as an [ expected
88+ behavior of OkHttp] [ 4 ] .
89+
14190``` kotlin
142- // To get build scan data such as username, tags and custom values, one
143- // must usually query /api/builds/{id}/gradle-attributes per-build, which
144- // is verbose and slow (1 request at a time)
145- api.getBuildsFlow(since = lastMonth)
146- .map { build -> api.getGradleAttributes(id = build.id) }
147- // Streams all available builds as GradleAttributes from a given date,
148- // requesting more than 1 build at a time.
149- api.getGradleAttributesFlow(since = lastMonth)
91+ val builds = api.getBuilds()
92+ // do work ...
93+ shutdown()
15094```
15195
152-
15396## More info
15497
155- - Currently built for Gradle Enterprise ` 2022.4 ` , but can be used with previous versions.
98+ - Currently built for Gradle Enterprise ` 2022.4 ` , but should work fine with previous versions.
15699- Use JDK 8 or 14+ to run, if you want to avoid the [ "illegal reflective access" warning about
157100 Retrofit] [ 3 ]
158- - There is a global instance ` okHttpClient ` so you can change what's needed, but also concurrency
159- shortcuts ` maxConcurrentRequests ` and ` shutdown() ` .
160- - ` maxConcurrentRequests ` is useful to speed up scripts, but if you start getting HTTP 504 from
161- your GE instance, decreasing this value should help.
162- - The script will keep running for an extra ~ 60s after code finishes, as an [ expected behavior
163- of OkHttp] [ 4 ] , unless you call ` shutdown() ` (global function).
164101- All classes live in these two packages. If you need to make small edits to scripts where
165102 there's no auto-complete, wildcard imports can be used:
166103
@@ -180,3 +117,8 @@ API classes such as `GradleEnterpriseApi` and response models are generated from
180117[ 4 ] : https://github.com/square/retrofit/issues/3144#issuecomment-508300518
181118[ 5 ] : https://docs.gradle.com/enterprise/api-manual/#reference_documentation
182119[ 6 ] : https://github.com/OpenAPITools/openapi-generator/blob/master/modules/openapi-generator-gradle-plugin/README.adoc
120+ [ 7 ] : https://gabrielfeo.github.io/gradle-enterprise-api-kotlin/
121+ [ 8 ] : https://gabrielfeo.github.io/gradle-enterprise-api-kotlin/gradle-enterprise-api-kotlin/com.gabrielfeo.gradle.enterprise.api/-options/
122+ [ 9 ] : https://gabrielfeo.github.io/gradle-enterprise-api-kotlin/gradle-enterprise-api-kotlin/com.gabrielfeo.gradle.enterprise.api/-gradle-enterprise-api/
123+ [ 10 ] : https://gabrielfeo.github.io/gradle-enterprise-api-kotlin/gradle-enterprise-api-kotlin/com.gabrielfeo.gradle.enterprise.api/-gradle-enterprise-api/index.html#373241164%2FExtensions%2F769193423
124+ [ 11 ] : https://gabrielfeo.github.io/gradle-enterprise-api-kotlin/gradle-enterprise-api-kotlin/com.gabrielfeo.gradle.enterprise.api/shutdown.html
0 commit comments