Forums

Articles
Create
cancel
Showing results for 
Search instead for 
Did you mean: 

How to consume Workday Rest APIs using ScriptRunner

raj-kumar2
Contributor
December 30, 2019

Hi,

We have a requirement where we want to populate custom fields based on response generated by Workday API.

Can anyone advise on this? I am new to groovy and need help with code.

Regards,

Raj

2 answers

1 accepted

0 votes
Answer accepted
Derek Fields _RightStar_
Community Champion
January 7, 2020

This version works exactly as expected. Note that I am passing only the extra part of the endpoint into the "get" call. The way the get works is that is determines whether you are passing in the enter URL starting with http... or whether you are passing only the specific endpoint, which is concatenated to the server that you set up when you create the client.

import groovy.json.JsonOutput
import groovy.json.JsonSlurper
import org.apache.commons.codec.binary.Base64
public class RestClient {
String server;
/***
* Constructor
* @Param server - the base url for the server to be called
* @Param uid - username
* @Param pwd - password
*/
RestClient(String server = "https://jsonplaceholder.typicode.com") {
this.server = server;
}

/***
* Sends a GET to the REST endpoint.
* @Param endpoint - the endpoint to be addressed
* @return a json object that can be cast to the expected type
*/
Object get(String endpoint) {
URL url
// This allows the endpoint to be fully specified or relative to the server
if (endpoint.startsWith("http")) {
url = new URL(endpoint)
} else {
url = new URL(server + endpoint)
}

// Call the REST endpoint.
URLConnection connection = url.openConnection()

connection.setRequestProperty("Content-Type", "application/json");
connection.setRequestProperty("Accept", "application/json");
connection.connect()
def json = null

json = new JsonSlurper().parse(connection.getInputStream())
return json
}
}

RestClient r = new RestClient();
def results1 = r.get("/todos/1");
def results2 = r.get("https://jsonplaceholder.typicode.com/todos/1");
[results1, results2]

 The output demonstrates the either way of calling the endpoint results in the same thing

raj-kumar2
Contributor
January 8, 2020

Hi Derek,

Great!!...Thanks a lot :)

It is working for me as well.  Could you please guide me with the complete code how to populate this response into Jira Custom field as I have already tried the code which you have shared earlier but it is not working for me.

Thanks in advance.

Regards,

Raj

0 votes
Derek Fields _RightStar_
Community Champion
December 30, 2019

I am not familiar with the Workday REST API; assuming that it returns the values as a JSON document, then the general pseudo-code for doing this is straightforward;

  • Call the Workday REST API, retrieving the results as a JSON document
  • Convert the JSON document into a Groovy Object (usually a Map of some sort)

Once you have the values in your map (lets call it results), you have to do something like the following:

issue.setCustomFieldValue(ComponentAccessor.getCustomFieldManager().getCustomFieldObjectsByName("<FIELD NAME>")[0], results["<Workday Field Name>"]);

(Note that this lacks defensive coding, so if you misspell the custom field name or change it later, it will fail)

Once you have set each of the custom field values, you need to store the values back to the issue with something like this:

def currentUser = ComponentAccessor.getJiraAuthenticationContext().getLoggedInUser() ;
ComponentAccessor.getIssueManager().update(currentUser, issue, EventDispatchOption.DO_NOT_DISPATCH, true);

I hope this helps. 

raj-kumar2
Contributor
January 1, 2020

Hi Derek,

Thanks, it is very useful. Can you please share a sample of complete code using REST API in Groovy and retrieving results as JSON? Then, I can proceed with above information shared by you in converting the json into a groovy object and placing the output in Jira fields.

 

Regards,

Raj

Derek Fields _RightStar_
Community Champion
January 2, 2020

Here is a simple Groovy REST-oriented class that I created to make it simple to make REST calls. Note the usage section at the bottom that shows how to turn the response into a Groovy object

import groovy.json.JsonOutput
import groovy.json.JsonSlurper
import org.apache.commons.codec.binary.Base64

public class RestClient {

String server
String uid
String pwd

/***
* Constructor
* @param server - the base url for the server to be called
* @param uid - username
* @param pwd - password
*/
RestClient(String server = Params.server, String uid, String pwd = Params.pwd) {
this.server = server
this.uid = uid
this.pwd = pwd
}

/***
* Sends a GET to the REST endpoint.
* @param endpoint - the endpoint to be addressed
* @return a json object that can be cast to the expected type
*/
Object get(String endpoint) {

URL url
// This allows the endpoint to be fully specified or relative to the server
if (endpoint.startsWith("http")) {
url = new URL(endpoint)
} else {
url = new URL(server + endpoint)
}
// Call the REST endpoint.
URLConnection connection = url.openConnection()
connection.setRequestProperty("Authorization", "Basic " + this.basic_auth(this.uid, this.pwd))
connection.setRequestProperty("Content-Type", "application/json")
connection.setRequestProperty("Accept", "application/json")

connection.connect()

def json = null
try {
json = new JsonSlurper().parse(connection.getInputStream())
} catch(all) {
log.error("Failure on REST GET to ${endpoint}\n${connection.getResponseMessage()}")
}
return json
}

}

 You would use this as follow:

RestClient restClient = new RestClient("https://...","username","password");

def response = restClient.get("/rest/url/...");

I am sure that there are more elegant ways to accomplish this, but it has been working for me.

raj-kumar2
Contributor
January 6, 2020

Hi Derek,

Thank you so much for your valuable input. I have tried to consume one of the free API using above code in script runner , unfortunately it is printing wrong output i.e. "RestClient" ...Here is my code below:

 

import groovy.json.JsonOutput
import groovy.json.JsonSlurper
import org.apache.commons.codec.binary.Base64

public class RestClient {

String server;
//String uid
//String pwd

/***
* Constructor
* @Param server - the base url for the server to be called
* @Param uid - username
* @Param pwd - password
*/
RestClient(String server = "https://jsonplaceholder.typicode.com/todos") {
this.server = server;
//this.uid = uid
//this.pwd = pwd
}

/***
* Sends a GET to the REST endpoint.
* @Param endpoint - the endpoint to be addressed
* @return a json object that can be cast to the expected type
*/
Object get(String endpoint) {

URL url
// This allows the endpoint to be fully specified or relative to the server
if (endpoint.startsWith("http")) {

url = new URL("/1")
}

else {
url = new URL(server + endpoint)
}
// Call the REST endpoint.
URLConnection connection = url.openConnection()
//connection.setRequestProperty("Authorization", "Basic " + this.basic_auth(this.uid, this.pwd))
connection.setRequestProperty("Content-Type", "application/json");
connection.setRequestProperty("Accept", "application/json");

connection.connect()

def json = null
try {
json = new JsonSlurper().parse(connection.getInputStream())
} catch(all) {
//log.error("Failure on REST GET to ${endpoint}\n${connection.getRe}")
}
return json

 

}
}

 

Appreciate if you help me with correct code to cosume free API ("https://jsonplaceholder.typicode.com/todos/1") using scriptRunner to get desired Json response.

 

Thanks in Advance.

 

Regards,

Raj

Suggest an answer

Log in or Sign up to answer