Now that the KScope hangover is done with, I thought we’d look at another example of how REST with Groovy works. Today, we will look at another example using the HPCM REST API; this one deals with executing tasks, such as running rulesets. While our earlier example used the “GET” method, we now will be using the “POST” method.
The POST method requires not just the URL, but also a payload (or parameters) that needs to get passed to the URL. So, not as simple as the “GET”. According to the documentation, the syntax looks like below.
Before we get to the payload, let’s make sure we have the right URL. The format for the URL is:
http://{HostName}/profitability/rest/v1/applications/{applicationName}/povs/{povGroupMember}/jobs /runLedgerCalculationJob
which might translate into the example below:
Now, let’s look at the payload. Payload parameters need to be passed through to the URI in JSON format. How can we do this? First off, we need to import a JSON library (download and place it in the Groovy library, if not already available).
This will allow us to set up the parameters in JSON format, so that the URL can understand them. The code might look like the screenshot below. The “put” actions below add the individual parameters needed for the payload (scroll up to the documentation screenshot. You should see a direct correlation between the “put” actions and the parameters needed for the POST).
Finally, to push the payload into the URL, we use the code below.
If we add all those pieces of code together, we might get something that looks like this:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 |
import org.json.JSONObject; // Pass variables serverUrl = "http://unlk.cube.com:19000" username = "unlkadmin" password = "unlkpwd" apiVersion = "v1" appName = "EntOH" pov = "December_FY15_Actual_Final" job = "runLedgerCalculationJob" userCredentials = username + ":" + password; basicAuth = "Basic " + javax.xml.bind.DatatypeConverter.printBase64Binary(userCredentials.getBytes()) dt = new Date().format("MM-dd-yyyy' 'HH:mm:ss") // Define the URL def address = serverUrl + "/profitability/rest/" + apiVersion + "/applications/" + appName + "/povs/" + pov + "/jobs/" + job // Parameter substitution to complete the URL def urlInfo = address.toURL() println "URL: ${address}" // Open connection and set connection properties def connection = urlInfo.openConnection() connection.setDoInput(true); connection.setDoOutput(true); connection.setRequestProperty("Content-Type", "application/json"); connection.setRequestProperty("Authorization", basicAuth); connection.setRequestMethod("POST"); connection.setUseCaches(false); // JSON object to pass payload parameters JSONObject jsonParam = new JSONObject(); // Create a new JSON object. jsonParam.put("isClearCalculated", "true"); // Pass each required parameter through with "put" jsonParam.put("isExecuteCalculations", "true"); jsonParam.put("isRunNow", "true"); jsonParam.put("comment", "Execution started at ${dt}"); jsonParam.put("exeType", "ALL_RULES"); jsonParam.put("stringDelimter", "_"); println jsonParam.toString() // Push payload to url OutputStreamWriter writer = new OutputStreamWriter(connection.getOutputStream()); writer.write(jsonParam.toString()); writer.flush(); println "Response Code/Message: ${connection.responseCode}" |
And if we execute it from the Groovy console, we get the output below.
Finally, if we check on the job library from Workspace, we see the job results as well.
The code is pretty basic and I highly recommend adding error trapping logic to your code (that’s for another day). You should also check for job completions IMO. You can also add this to ODI procedures (as I showed here) and take your integrations to the next level. If you want to learn more about REST and Groovy, please follow posts from the immense Jake Turrell and the delightful Joe Aultman. They do sterling work in this area.