I'm trying to configure a groovy script on Jira to get data from another system through it's API. The script is working and I'm getting a payload back from the API call. But the problem is that the API on the other system is using pagination and I'm only getting the first 100 objects. The payload response has the next page link provided in the header, but I'm struggling how to perform a call on all the pages and getting the results back.
Here is the initial code that I wrote for getting the first 100 objects:
import groovyx.net.http.HTTPBuilder
import static groovyx.net.http.ContentType.*
import groovyx.net.http.ContentType
import static groovyx.net.http.Method.*
import groovyx.net.http.HTTPBuilder
import groovyx.net.http.ContentType
import groovyx.net.http.HTTPBuilder
import static groovyx.net.http.ContentType.*
import groovyx.net.http.ContentType
import static groovyx.net.http.Method.*
import java.util.Map;
import groovy.json.*
// Define HTTP URL and call API
def TOKEN = '...'
def httpBuilder = new HTTPBuilder("https://....")
httpBuilder.setHeaders([Authorization: "Bearer ${TOKEN}"])
httpBuilder.headers.Accept = ContentType.JSON
httpBuilder.get(
path: "/api/v1.5/data/search",
requestContentType: ContentType.JSON,
query: [query: "search Host show name, #id"],
)
{
resp, reader ->
userList(resp, reader)
}
def userList(def resp, def reader){
def results = reader["results"]
//Collecting results and grouping based on 'ID' and 'Host'
def map = results[0].groupBy{it[2] }.collectEntries{
[it.value.collect{[id: '"'+ it[1] + '"', host: '"'+ it[0]+ '"'] }]
}
//converts map result into JSON readable string
def s = map.toMapString().replaceAll("^\\[|\\]\$|:null","").replaceAll("(?!^.)\\[", "{").replaceAll("\\](?!\$)","}").replaceAll("id","\"id\"").replaceAll("host","\"host\"")
}
//writing string to JSON file on server
public static def writeJson(def fileWriter){
final def filePath = '/tmp'
final def filename = 'ADDM_Printer'
fileWriter = new FileWriter("${filePath}/${filename}.json")
fileWriter.write(s)
fileWriter.close()
}
And here is the payload with the next page provided after running the script:
[{count=335, headings=[name, #id], kind=Host, next=https://xxxxx.preprod.local/api/v1.5/data/search?query=search+Host+show+name%2C+%23id&results_id=SG9zdABuvShtYwIAfr4AAGYGnA%3D%3D&offset=100, next_offset=100, offset=0, results=[[xxxx.preprod.local, 567372576d6765a87146fbe46e486f7374],....]
I tried to do a while loop after I performed the initial API request to get results from all the pages, like this:
//getting the next page URL
def urlnext = reader["next"].toString()
//removing [] from the URL
def nextpageURL = urlnext.replaceAll("^\\[|\\]|\\[", "")
def dataNextPages = []
def baseURL = ""
//get next page until 'Next' is null
while(nextpageURL != null){
def nextpage = new HTTPBuilder("${nextpageURL}")
//def nextpage = new HTTPBuilder("https://xxxx.preprod.local/api/v1.5/data/search?query=search+Host+show+name%2C+%23id&results_id=SG9zdABuvShtYwIAfr4AAGYFeQ%3D%3D&offset=200")
nextpage.setHeaders([Authorization: "Bearer ${TOKEN}"])
nextpage.headers.Accept = ContentType.JSON
def issueJson = nextpage.request(Method.GET) { req ->
response.success = { respond, json ->
def next = json["next"].toString()
def nextURL = next.replaceAll("^\\[|\\]|\\[", "")
nextpageURL = nextURL
dataNextPages = json["results"]
}
}
}
But the scripts fails and getting following error code: Script console script failed: java.lang.IllegalStateException: Target host is null
Sorry for my bad coding and I'm not sure how to resolve this. Any help is appreciated!