import com.fasterxml.jackson.databind.ObjectMapper

def API_KEY = "your API key"
def requestUrl = "https://app.biodock.ai/api/external/pipelines"

// Open the connection
def url = new URL(requestUrl)
def connection = url.openConnection() as HttpURLConnection
connection.requestMethod = "GET"
connection.setRequestProperty("X-API-KEY", API_KEY)
connection.setRequestProperty("Content-Type", "application/json")

// Print subset of keys and values
def printJsonObject(obj, indent = "") {
    if (obj instanceof Map) {
        // obj is another JSON object
        obj.each { k, v ->
            println("${indent}${k} :")
            printJsonObject(v, indent + "    ")
        }
    } else if (obj instanceof List) {
        // obj is a JSON array
        obj.eachWithIndex { val, i ->
            println("${indent}[${i + 1}] :")
            printJsonObject(val, indent + "    ")
        }
    } else {
        // obj is a simple value (string, number, boolean, null)
        println("${indent}${obj}")
    }
}

int responseCode = connection.responseCode
if (responseCode == HttpURLConnection.HTTP_OK) {
    // Read the raw response text
    def responseText = connection.inputStream.getText('UTF-8')
    
    // Parse JSON into a Map using Jackson ObjectMapper
    def mapper = new ObjectMapper()
    def data = mapper.readValue(responseText, Map)
    
    // Convert the keySet of the map to a list
    def keys = new ArrayList(data.keySet())
    
    // Calculate the end index
    def endIndex = keys.size() - 2
    
    // Print the responses
    for (int i = 0; i <= endIndex; i++) {
        def k = keys[i]
        println("${k} :")
        printJsonObject(data[k], "    ")
    }
} else {
    println("HTTP request failed with response code: ${responseCode}")
}
