Created
August 29, 2022 09:14
-
-
Save YagmurOzden/0be3b49d54bd430163016b05206e472b to your computer and use it in GitHub Desktop.
Rest Template for using rest operations.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// VMware vRealize Orchestrator action sample | |
// vRA 8.7.0 | |
// input type: restHost [REST:RESTHost] | |
// return type: Any | |
function createRestOperation(method, urlTemplate) { | |
var op = new RESTOperation(urlTemplate); | |
op.name = urlTemplate; | |
op.method = method; | |
op.urlTemplate = urlTemplate; | |
op.defaultContentType = "application/json"; | |
//attach to host | |
operation = restHost.addOperation(op); | |
if (!restHost.skipRestHostUpdate) { | |
RESTHostManager.updateHost(restHost); | |
} | |
} | |
function getRestOperation(method, urlTemplate) { | |
for each (var id in restHost.getOperations()) { | |
var o = restHost.getOperation(id); | |
if (o.method == method && o.urlTemplate == urlTemplate) { | |
return o; | |
} | |
} | |
return null; | |
} | |
function getResponseValue(options, response){ | |
if(options && options.returnResponseObject == true){ | |
var responseValue = { | |
contentAsString : response.contentAsString, | |
contentLength : response.contentLength, | |
statusCode : response.statusCode, | |
responseHeaders : response.getAllHeaders() | |
} | |
}else{ | |
var responseValue = {}; // new lines from skycoding | |
if(response.contentAsString != ""){ | |
responseValue = JSON.parse(response.contentAsString); | |
} | |
} | |
if (response.contentAsString && response.contentAsString != "") { | |
return responseValue; | |
} | |
} | |
function runRestOp(method, urlTemplate, params, headers, content, options) { | |
var op = getRestOperation(method, urlTemplate); | |
if (op == null) { | |
System.log("Rest Operation[method:" + method + ";template:" + urlTemplate + "] not found. Creating one."); | |
createRestOperation(method, urlTemplate); | |
op = getRestOperation(method, urlTemplate); | |
} | |
if (!op) { | |
throw "Operation not found and wasn't able to create it." | |
} | |
if (!params) { | |
params = []; | |
} | |
for (var i = 0; i < params.length; i++) { | |
params[i] = encodeURIComponent(params[i]); | |
} | |
System.debug("================================================"); | |
System.debug("Invoking REST Request:"); | |
System.debug("\tMethod: " + method); | |
System.debug("\tURL Template: " + urlTemplate); | |
System.debug("\tParams: " + params); | |
System.debug("\tContent: " + (content ? JSON.stringify(content) : null)); | |
var request = op.createRequest(params, (content ? JSON.stringify(content) : null)); | |
request.setHeader("Accept", "application/json"); | |
if (headers) { | |
for (var key in headers) { | |
var value = headers[key]; | |
System.debug("Key: " + key + ", Value: " + value); | |
request.setHeader(key, value); | |
} | |
} | |
var response = request.execute(); | |
System.debug("REST Response: "); | |
System.debug("\tStatus Code: " + response.statusCode); | |
System.debug("\tContent: " + response.contentAsString); | |
System.debug("================================================"); | |
if (("" + response.statusCode).indexOf("20") != 0) { | |
throw response.statusCode + " Content: " + responseValue; | |
} | |
var responseValue = getResponseValue(options, response); | |
return responseValue; | |
} | |
var RestTemplate = new function() {} | |
RestTemplate.get = function(urlTemplate, params, headers, options) { | |
return runRestOp("GET", urlTemplate, params, headers, null, options) | |
} | |
RestTemplate.post = function(urlTemplate, params, content, headers, options) { | |
return runRestOp("POST", urlTemplate, params, headers, content, options) | |
} | |
RestTemplate.put = function(urlTemplate, params, content, headers, options) { | |
return runRestOp("PUT", urlTemplate, params, headers, content, options) | |
} | |
RestTemplate.delete = function(urlTemplate, params, headers, options) { | |
return runRestOp("DELETE", urlTemplate, params, headers, null, options) | |
} | |
RestTemplate.patch = function(urlTemplate, params, headers, content, options) { | |
return runRestOp("PATCH", urlTemplate, params, headers, content, options) | |
} | |
return RestTemplate; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment