Groovy web console

subscribe to the feed Subscribe
to this
site

Generic external call out

Published 2 months ago by RUtger de Vries with tags externale call out
Actions  ➤ Edit in console Back to console Show/hide line numbers View recent scripts
import groovy.json.JsonSlurper
import javax.ws.rs.core.Response
import java.net.URI

templateProduct = 'TEST'

//get adjustments
	path = '/generic/enrollmentproductadjustments'
	q = 'enrollmentProduct.code.eq(' + templateProduct + ')'
	groupBy = 'sequence,scheduleDefinition.code,startDate'
	adjustments = sendRequest(method: 'GET', path: path, q: q, groupBy: groupBy, orderBy: groupBy)

def sendRequest(args){
    Response response = null
	
     //create request uri
    if(args.nextLink) {
          URI next = new URI(args.nextLink)
          target = args.webTarget ? webTarget(args.webTarget, next) : webTarget(defaultEnv, next)
          target = target.request()
    } else {
        items = []
        target = args.webTarget ? webTarget(args.webTarget) : webTarget(defaultEnv)
        target = target.path(args.path)
  
          //add parameters
        target = args.q ? target.queryParam("q", args.q) : target
        target = args.groupBy ? target.queryParam("groupBy", args.groupBy) : target
        target = args.orderBy ? target.queryParam("orderBy", args.orderBy) : target.queryParam("orderBy", "id")
        target = args.limit ? target.queryParam("limit", args.limit) : target.queryParam("limit", "200")
        target = target.request()
  
        //add headers
        target = args.fields && args.expand ? target.header('accept', 'application/json;expand=' + expand + ';fields=' + fields) : target
        target = args.fields && !args.expand ? target.header('accept', 'application/json' + ';fields=' + fields) : target
        target = !args.fields && args.expand ? target.header('accept', 'application/json;expand=' + expand) : target
     }
	
    //build and send request
     switch(args.method){
        case 'GET':
            target = target.buildGet()
            response = target.invoke()
            break;
        case 'DELETE':
            response = target.delete()
            break;
        case 'POST':
            target = target.build(args.method, Entity.json(args.body.toString()))
            response = target.invoke()
            break;
        case 'PUT':
            target = target.build(args.method, Entity.json(args.body.toString()))
            response = target.invoke()
            break;
        case 'PATCH':
            target = target.build(args.method, Entity.json(args.body.toString()))
            response = target.invoke()
            break;
     }	
	
     //check response status
    status = response.getStatus()
    if(![200, 201, 204].contains(status)){
        exception = args?.error + ' Response: status= ' + status + ', reason=' + response.toString() + ', message= ' +           response.readEntity(String.class)
    throw new Exception(exception)
     }
	
    //read response
    stringResponse = response.readEntity(String.class)
	
     //return response. Check if pagination is needed (max items API is 200)
     if(stringResponse){
        jsonResponse = new JsonSlurper().parseText(stringResponse)
        if(args.method == 'GET'){
            items += jsonResponse.items
            if(jsonResponse.hasMore){
                nextLink = jsonResponse.links.find{it.rel == 'next'}.href
                sendRequest(method: args.method, nextLink: nextLink)
            }
       return items
        }
        if(args.method != 'GET'){
            return jsonResponse
        }  
    } else {
        return null
    }
}