Skip to content

Commit

Permalink
Make methods of GradleEnterpriseApi suspend
Browse files Browse the repository at this point in the history
Even with useCoroutines, openapi-generator outputs methods
with Response<> wrapping by default. Response<> shouldn't
be useful here, so they're replaced with the bare types.
  • Loading branch information
gabrielfeo committed Jan 3, 2023
1 parent 7588ee4 commit f61e4b3
Show file tree
Hide file tree
Showing 4 changed files with 22 additions and 4 deletions.
19 changes: 19 additions & 0 deletions build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,25 @@ openApiGenerate {
packageName.set("com.gabrielfeo.gradle.enterprise.api.internal")
invokerPackage.set("com.gabrielfeo.gradle.enterprise.api.internal")
additionalProperties.put("library", "jvm-retrofit2")
additionalProperties.put("useCoroutines", true)
}

tasks.openApiGenerate.configure {
// Replace Response<X> with X in every method return type of GradleEnterpriseApi.kt
doLast {
val apiFile = File(
outputDir.get(),
"src/main/kotlin/com/gabrielfeo/gradle/enterprise/api/GradleEnterpriseApi.kt",
)
ant.withGroovyBuilder {
"replaceregexp"(
"file" to apiFile,
"match" to ": Response<(.*?)>$",
"replace" to """: \1""",
"flags" to "gm",
)
}
}
}

sourceSets {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.DelicateCoroutinesApi
import kotlinx.coroutines.GlobalScope
import kotlinx.coroutines.flow.*
import retrofit2.await

/**
* Gets builds on demand from the API, in as many requests as necessary. It allows
Expand All @@ -31,7 +30,7 @@ fun GradleEnterpriseApi.getBuildsFlow(
fromInstant = fromInstant,
fromBuild = fromBuild,
maxBuilds = API_MAX_BUILDS,
).await()
)
val pagedBuilds = firstBuilds.asFlow().pagedUntilLastBuild(maxPerRequest = API_MAX_BUILDS)
emitAll(pagedBuilds)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ internal fun Flow<Build>.pagedUntilLastBuild(
if (lastBuildId.isEmpty()) {
return@flow
} else while (true) {
val builds = api.getBuilds(fromBuild = lastBuildId, maxBuilds = maxPerRequest).await()
val builds = api.getBuilds(fromBuild = lastBuildId, maxBuilds = maxPerRequest)
emitAll(builds.asFlow())
when {
builds.isEmpty() || builds.size < API_MAX_BUILDS -> break
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ internal fun Flow<Build>.withGradleAttributes(
): Flow<Pair<Build, GradleAttributes>> =
map { build ->
build to scope.async {
api.getGradleAttributes(build.id).await()
api.getGradleAttributes(build.id)
}
}.buffer(Int.MAX_VALUE).map { (build, attrs) ->
build to attrs.await()
Expand Down

0 comments on commit f61e4b3

Please sign in to comment.