Skip to content

Commit 8488230

Browse files
Create RestMessageUtils.js
1 parent 02ea335 commit 8488230

File tree

1 file changed

+85
-0
lines changed

1 file changed

+85
-0
lines changed
Lines changed: 85 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,85 @@
1+
var RestMessageUtils = Class.create();
2+
RestMessageUtils.prototype = {
3+
initialize: function(rmObj, restMessage, restFunc) {
4+
this.RM = (restFunc && restMessage) ? new sn_ws.RESTMessageV2(restMessage, restFunc) : new sn_ws.RESTMessageV2();
5+
this.rmObj = rmObj;
6+
},
7+
8+
checkAndGetKeyValue: function(obj, key) {
9+
return obj.hasOwnProperty(key);
10+
11+
},
12+
13+
setQueryParams: function() {
14+
if (this.checkAndGetKeyValue(this.rmObj, 'queryParams'))
15+
for (var i in this.rmObj.queryParams)
16+
this.RM.setQueryParameter(i, this.rmObj.queryParams[i]);
17+
18+
},
19+
20+
variableSubs: function() {
21+
if (this.checkAndGetKeyValue(this.rmObj, 'variableInfo'))
22+
for (var i in this.rmObj.variableInfo)
23+
this.RM.setStringParameterNoEscape(i, this.rmObj.variableInfo[i]);
24+
},
25+
26+
setAuth: function() {
27+
var authType = this.rmObj.authType;
28+
29+
if (authType) {
30+
if (authType == 'APIKEY')
31+
return;
32+
else if (authType == 'BasicCreds')
33+
this.RM.setBasicAuth(this.rmObj.userName, this.rmObj.password);
34+
else if (authType == 'BasicAuthProfile')
35+
this.RM.setAuthenticationProfile('basic', this.rmObj.authProfile);
36+
37+
}
38+
39+
},
40+
41+
setRequestBody: function() {
42+
if (this.checkAndGetKeyValue(this.rmObj, 'requestBody'))
43+
this.RM.setRequestBody(typeof this.rmObj.requestBody == 'object' ? JSON.stringify(this.rmObj.requestBody) : this.rmObj.requestBody);
44+
},
45+
46+
setRequestHeaders: function() {
47+
if (this.checkAndGetKeyValue(this.rmObj, 'requestHeaders'))
48+
for (var i in this.rmObj.requestHeaders)
49+
this.RM.setRequestHeader(i, this.rmObj.requestHeaders[i]);
50+
},
51+
52+
setMidServer: function() {
53+
if (this.checkAndGetKeyValue(this.rmObj, 'midServer'))
54+
this.RM.setMidServer(this.rmObj.midServer);
55+
},
56+
57+
setEndpoint: function() {
58+
if (this.checkAndGetKeyValue(this.rmObj, 'endPoint'))
59+
this.RM.setEndpoint(this.rmObj.endPoint);
60+
},
61+
setHttpMethod: function() {
62+
if (this.checkAndGetKeyValue(this.rmObj, 'httpMethod'))
63+
this.RM.setHttpMethod(this.rmObj.httpMethod);
64+
},
65+
66+
execute: function() {
67+
try {
68+
this.setHttpMethod();
69+
this.setEndpoint();
70+
this.setRequestHeaders();
71+
this.setAuth();
72+
this.setRequestBody();
73+
this.setQueryParams();
74+
this.variableSubs();
75+
this.setMidServer();
76+
return this.RM.execute();
77+
} catch (err) {
78+
gs.error('REST Message execution failed: ' + err);
79+
}
80+
81+
},
82+
83+
84+
type: 'RestMessageUtils'
85+
};

0 commit comments

Comments
 (0)