-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlab03.main.js
224 lines (197 loc) · 7.83 KB
/
lab03.main.js
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
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
// Update this constant with your ServiceNow credentials
const options = {
url: 'https://dev78934.service-now.com/login.do',
username: 'admin',
password: 'Manu@1089Disha'
};
/**
* Import the Node.js request package.
* See https://www.npmjs.com/package/request
*/
const request = require('request');
// We'll use this regular expression to verify REST API's HTTP response status code.
const validResponseRegex = /(2\d\d)/;
// Use JSDoc to create a JSDoc data type for an IAP callback.
// Call the new type iapCallback.
// Notice iapCallback is a data-first callback.
/**
* This is a [JSDoc comment]{@link http://usejsdoc.org/tags-description.html}.
* See http://usejsdoc.org/tags-description.html.
*
* /**
* @callback iapCallback
* @description A [callback function]{@link
* https://developer.mozilla.org/en-US/docs/Glossary/Callback_function}
* is a function passed into another function as an argument, which is
* then invoked inside the outer function to complete some kind of
* routine or action.
*
* @param {*} responseData - When no errors are caught, return data as a
* single argument to callback function.
* @param {error} [errorMessage] - If an error is caught, return error
* message in optional second argument to callback function.
*/
/**
* @function constructUri
* @description Build and return the proper URI by appending an optionally passed
* [URL query string]{@link https://en.wikipedia.org/wiki/Query_string}.
*
* @param {string} serviceNowTable - The table target of the ServiceNow table API.
* @param {string} [query] - Optional URL query string.
*
* @return {string} ServiceNow URL
*/
function constructUri(serviceNowTable, query = null) {
let uri = `/api/now/table/${serviceNowTable}`;
if (query) {
uri = uri + '?' + query;
}
return uri;
}
/**
* @function isHibernating
* @description Checks if request function responded with evidence of
* a hibernating ServiceNow instance.
*
* @param {object} response - The response argument passed by the request function in its callback.
*
* @return {boolean} Returns true if instance is hibernating. Otherwise returns false.
*/
function isHibernating(response) {
return response.body.includes('Instance Hibernating page')
&& response.body.includes('<html>')
&& response.statusCode === 200;
}
/**
* @function processRequestResults
* @description Inspect ServiceNow API response for an error, bad response code, or
* a hibernating instance. If any of those conditions are detected, return an error.
* Else return the API's response.
*
* @param {error} error - The error argument passed by the request function in its callback.
* @param {object} response - The response argument passed by the request function in its callback.
* @param {string} body - The HTML body argument passed by the request function in its callback.
* @param {iapCallback} callback - Callback a function.
* @param {(object|string)} callback.data - The API's response. Will be an object if sunnyday path.
* Will be HTML text if hibernating instance.
* @param {error} callback.error - The error property of callback.
*/
function processRequestResults(error, response, body,callback) {
/**
* You must build the contents of this function.
* Study your package and note which parts of the get()
* and post() functions evaluate and respond to data
* and/or errors the request() function returns.
* This function must not check for a hibernating instance;
* it must call function isHibernating.
*/
let callbackData = null;
let callbackError = null;
if (error) {
console.error('Error present.');
callbackError = error;
} else if (!validResponseRegex.test(response.statusCode)) {
console.error('Bad response code.');
callbackError = response;
} else if (isHibernating(response)) {
callbackError = 'Service Now instance is hibernating';
console.error(callbackError);
} else {
callbackData = response;
}
callback(callbackData, callbackError);
}
/**
* @function sendRequest
* @description Builds final options argument for request function
* from global const options and parameter callOptions.
* Executes request call, then verifies response.
*
* @param {object} callOptions - Passed call options.
* @param {string} callOptions.query - URL query string.
* @param {string} callOptions.serviceNowTable - The table target of the ServiceNow table API.
* @param {string} callOptions.method - HTTP API request method.
* @param {iapCallback} callback - Callback a function.
* @param {(object|string)} callback.data - The API's response. Will be an object if sunnyday path.
* Will be HTML text if hibernating instance.
* @param {error} callback.error - The error property of callback.
*/
function sendRequest(callOptions, callback) {
// Initialize return arguments for callback
if (callOptions.query)
uri = constructUri(callOptions.serviceNowTable, callOptions.query);
else
uri = constructUri(callOptions.serviceNowTable);
/**
* You must build the requestOptions object.
* This is not a simple copy/paste of the requestOptions object
* from the previous lab. There should be no
* hardcoded values.
*/
const requestOptions = {
method: callOptions.method,
auth: {
user: options.username,
pass: options.password,
},
baseUrl: options.url,
uri: uri,
};
//const requestOptions = {};
request(requestOptions, (error, response, body) => {
processRequestResults(error, response, body, (processedResults, processedError) => callback(processedResults, processedError));
});
}
/**
* @function get
* @description Call the ServiceNow GET API. Sets the API call's method and query,
* then calls sendRequest().
*
* @param {object} callOptions - Passed call options.
* @param {string} callOptions.serviceNowTable - The table target of the ServiceNow table API.
* @param {iapCallback} callback - Callback a function.
* @param {(object|string)} callback.data - The API's response. Will be an object if sunnyday path.
* Will be HTML text if hibernating instance.
* @param {error} callback.error - The error property of callback.
*/
function get(callOptions, callback) {
callOptions.method = 'GET';
callOptions.query = 'sysparm_limit=1';
sendRequest(callOptions, (results, error) => callback(results, error));
}
/**
* @function post
* @description Call the ServiceNow POST API. Sets the API call's method,
* then calls sendRequest().
*
* @param {object} callOptions - Passed call options.
* @param {string} callOptions.serviceNowTable - The table target of the ServiceNow table API.
* @param {iapCallback} callback - Callback a function.
* @param {(object|string)} callback.data - The API's response. Will be an object if sunnyday path.
* Will be HTML text if hibernating instance.
* @param {error} callback.error - The error property of callback.
*/
function post(callOptions, callback) {
callOptions.method = 'POST';
sendRequest(callOptions, (results, error) => callback(results, error));
}
/**
* @function main
* @description Tests get() and post() functions.
*/
function main() {
get({ serviceNowTable: 'change_request' }, (data, error) => {
if (error) {
console.error(`\nError returned from GET request:\n${JSON.stringify(error)}`);
}
console.log(`\nResponse returned from GET request:\n${JSON.stringify(data)}`)
});
post({ serviceNowTable: 'change_request' }, (data, error) => {
if (error) {
console.error(`\nError returned from POST request:\n${JSON.stringify(error)}`);
}
console.log(`\nResponse returned from POST request:\n${JSON.stringify(data)}`)
});
}
// Call main to run it.
main();