Create
cancel
Showing results for 
Search instead for 
Did you mean: 
Sign up Log in

Behaviour and external API does not return correctly

serge calderara
Rising Star
Rising Star
Rising Stars are recognized for providing high-quality answers to other users. Rising Stars receive a certificate of achievement and are on the path to becoming Community Leaders.
September 24, 2021

Dear all,

I am trying to build a behaviour which convert a text field in a select list box by collecting data from an exteranal API.

For that I follow the samples provided in this link below :

Select List Conversions (adaptavist.com)

I have for that created a simple REST API call which collect VM information from Azure and which return the following sample data from POSTMAN

85n6fGXLB8.png

From my custom Rest endpoint, I need to return the *tags list items* in order to be visible into the Select List field of my behavior

The issue I met is that I do not know, or at least it is not so clear from sample on the way I should format the return response from the API in order that behavior gets the data

In my sample snipet code below, I need to return, as in the sample, a New JsonBuilder object based on my json return by the API but I could not get the correct syntax to replace in ???????? in order that my field slection contains items *com, aem, dispatcher* from tags element of my original return json above

2DJku6rAdg.png

Any idea ?

I am struggling this for a full week now and I am stuck, hoping you can help

regards

 

1 answer

Suggest an answer

Log in or Sign up to answer
0 votes
Peter-Dave Sheehan
Community Leader
Community Leader
Community Leaders are connectors, ambassadors, and mentors. On the online community, they serve as thought leaders, product experts, and moderators.
September 24, 2021

The select conversion method need the JSON in very specific format.

So you'll need to prepare a return object with that format.

Based on what you provided, I'm expecting the vmInfo object returned by httpBuilder.request to look something like this (as you would deeclarie it in groovy)

[
name:'dummy',
location:"don't care",
tags:[
app:'appname',
product:'aem',
type:'dispatcher']
]

So if I start with building a replacement variable with this data, I can use that to construct the output object


def vmInfo = [name:'dummy', location:"don't care", tags:[app:'appname', product:'aem', type:'dispatcher']]
//we're only interested in the tags ... I've opted to include both tag key and value sepearated by colon, but you can adjust that format
def tags = vmInfo.tags.collect{k,v-> "$k : $v"}

//normally, the query should come from your rest api with
//def query = queryParams.getFirst("query") as String
// but I used this for testing
def query = 'prod'
//this is the object we need to return
def singleSelectRet = [
items : tags.collect{tag ->
[
value: tag,
label: tag,
html: tag.replaceAll(/(?i)$query/) { "<b>${it}</b>" }
]
},
total : tags.size(),
footer : "Select a tag"
]
return Response.ok(new JsonBuilder(singleSelectRet).toString()).build()
TAGS
AUG Leaders

Atlassian Community Events