-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlab02.main.js
182 lines (162 loc) · 6.23 KB
/
lab02.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
// Update this constant with your ServiceNow credentials
const options = {
url: 'https://dev50714.service-now.com/',
username: 'admin',
password: '9w*6hPP$R82k6hP#Vd#mMjYca5vyjrdm'
};
/**
* 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 get
* @description Call the ServiceNow GET API.
*
* @param {string} serviceNowTable - The table target of the ServiceNow table API.
* @param {iapCallback} callback - Callback a function.
* @param {*} 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(serviceNowTable, callback) {
// Initialize return arguments for callback
let callbackData = null;
let callbackError = null;
// Construct API call to send to ServiceNow.
// The request constructor has an options parameter
// that holds the HTTP request method, credentials, and the API's URL.
// Some properties are hardcoded, like the method and part of the URI.
// Some properties are read from global const options.
// Some properties are passed into function get() through parameters.
const requestOptions = {
method: 'GET',
auth: {
user: options.username,
pass: options.password,
},
baseUrl: options.url,
uri: `/api/now/table/${serviceNowTable}?sysparm_limit=1`,
};
// Send Request to ServiceNow.
// We are passing variable requestOptions for the first argument.
// We are passing an anonymous function, an error-first callback,
// for the second argument.
request(requestOptions, (error, response, body) => {
/**
* Process ServiceNow error, response and body.
* Check error and response code to make sure
* response is good.
*/
if (error) {
console.error('Error present.');
callbackError = error;
} else if (!validResponseRegex.test(response.statusCode)) {
console.error('Bad response code.');
callbackError = response;
} else if (response.body.includes('Instance Hibernating page')) {
callbackError = 'Service Now instance is hibernating';
console.error(callbackError);
} else {
callbackData = response;
}
return callback(callbackData, callbackError);
});
}
/**
* @function post
* @description Call the ServiceNow POST API.
*
* @param {string} serviceNowTable - The table target of the ServiceNow table API.
* @param {iapCallback} callback - Callback a function.
* @param {*} 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(serviceNowTable, callback) {
// Initialize return arguments for callback
let callbackData = null;
let callbackError = null;
// Construct API call to send to ServiceNow.
// The request constructor has an options parameter
// that holds the HTTP request method, credentials, and the API's URL.
// Some properties are hardcoded, like the method and part of the URI.
// Some properties are read from global const options.
// Some properties are passed into function post() through parameters.
const requestOptions = {
method: 'POST',
auth: {
user: options.username,
pass: options.password,
},
baseUrl: options.url,
uri: `/api/now/table/${serviceNowTable}`,
};
// Send Request to ServiceNow.
// We are passing variable requestOptions for the first argument.
// We are passing an anonymous function, an error-first callback,
// for the second argument.
request(requestOptions, (error, response, body) => {
/**
* Process ServiceNow error, response and body.
* Check error and response code to make sure
* response is good.
*/
if (error) {
console.error('Error present.');
callbackError = error;
} else if (!validResponseRegex.test(response.statusCode)) {
console.error('Bad response code.');
callbackError = response;
} else if (response.body.includes('Instance Hibernating page')) {
callbackError = 'Service Now instance is hibernating';
console.error(callbackError);
} else {
callbackData = response;
}
return callback(callbackData, callbackError);
});
}
/*
* This section is used to test your project.
* We will test both get() and post() functions.
* If either function returns data, print the returned data to console on STDOUT.
* If either function returns an error, print the returned data to the console on STDERR.
*/
function main() {
get('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('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();