Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Gradle Enterprise Export API #893

Closed
bootstraponline opened this issue Jul 16, 2020 · 0 comments · Fixed by #913
Closed

Gradle Enterprise Export API #893

bootstraponline opened this issue Jul 16, 2020 · 0 comments · Fixed by #913
Assignees
Labels

Comments

@bootstraponline
Copy link
Contributor

bootstraponline commented Jul 16, 2020

Author the user story for this feature

As a Gradle user, I want to export build data from Gradle Enterprise so I can have metrics on build performance.


Let's add a working Kotlin sample in the Flank repo that demonstrates fetching data from the Gradle Enterprise Export API. We should also submit this example in the upstream repo

Here's a simple example:

import okhttp3.*
import okhttp3.sse.EventSource
import okhttp3.sse.EventSourceListener
import okhttp3.sse.EventSources.createFactory
import java.time.Duration

fun main() {
    val id = ""
    val password = ""
    val url = "https://enterprise-training.gradle.com/build-export/v1/builds/since/now?stream"

    val client = OkHttpClient.Builder()
        .connectTimeout(Duration.ZERO)
        .readTimeout(Duration.ZERO)
        .authenticator(
        object : Authenticator {
            override fun authenticate(route: Route?, response: Response): Request? {
                return response.request
                    .newBuilder()
                    .addHeader("Authorization", Credentials.basic(id, password))
                    .build()
            }
        }
    ).build()

    val factory = createFactory(client)

    val request = Request.Builder()
        .url(url).build()
    val listener = object : EventSourceListener() {
        override fun onClosed(eventSource: EventSource) {
            println("onClosed")
            super.onClosed(eventSource)
        }

        override fun onEvent(eventSource: EventSource, id: String?, type: String?, data: String) {
            println("onEvent $id $type $data")
            super.onEvent(eventSource, id, type, data)
        }

        override fun onFailure(eventSource: EventSource, t: Throwable?, response: Response?) {
            println("onFailure $t $response")
            super.onFailure(eventSource, t, response)
        }

        override fun onOpen(eventSource: EventSource, response: Response) {
            println("onOpen $response")
            super.onOpen(eventSource, response)
        }
    }
    factory.newEventSource(request, listener)
}
plugins {
    kotlin("jvm") version "1.3.72"
}

group = "org.example"
version = "1.0-SNAPSHOT"

repositories {
    mavenCentral()
}

dependencies {
    implementation(kotlin("stdlib-jdk8"))
    implementation("org.jetbrains.kotlinx:kotlinx-coroutines-core:1.3.7")
    implementation("com.squareup.okhttp3:okhttp:4.8.0")
    implementation("com.squareup.okhttp3:okhttp-sse:4.8.0")

}

tasks {
    compileKotlin {
        kotlinOptions.jvmTarget = "1.8"
    }
    compileTestKotlin {
        kotlinOptions.jvmTarget = "1.8"
    }
}

Context

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants