Skip to content

Callback promises migration #162

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 35 commits into from
Nov 15, 2022
Merged
Show file tree
Hide file tree
Changes from 15 commits
Commits
Show all changes
35 commits
Select commit Hold shift + click to select a range
4235212
code refactor
akaila-splunk Jun 27, 2022
3d29152
Update http.js
akaila-splunk Jul 5, 2022
27c389c
Update context.js
akaila-splunk Jul 5, 2022
e398c2d
Update service.js
akaila-splunk Jul 5, 2022
18e2f92
Update service.js
akaila-splunk Jul 5, 2022
4d6a05f
Update service.js
akaila-splunk Jul 12, 2022
a523a50
Promise refractoring
ashah-splunk Aug 2, 2022
0e19ed4
promise refactor changes
ashah-splunk Aug 4, 2022
2d8e4d2
Test cases updated with Async/await
ashah-splunk Aug 5, 2022
c231ff4
refactor crud func. methods
akaila-splunk Aug 8, 2022
dfe4c01
browser test cases updated.
ashah-splunk Aug 9, 2022
9a3fb7b
code refactoring
ashah-splunk Aug 9, 2022
14a09ef
Update endpoint.js
ashah-splunk Aug 9, 2022
9e1c203
update methods comment and example
akaila-splunk Aug 12, 2022
c4ee9e6
Update proxy_http.js
akaila-splunk Aug 17, 2022
8d6a651
testcase refactor
akaila-splunk Aug 30, 2022
c54ed14
Update test_utils.js
ashah-splunk Aug 30, 2022
13db35a
review updates
ashah-splunk Sep 19, 2022
8bb498a
Update browser.test.entry.js
ashah-splunk Sep 19, 2022
e640a36
merge updates from develop branch
ashah-splunk Sep 20, 2022
9c2633e
refactor browser side testcases
akaila-splunk Oct 11, 2022
882af21
updated compile files
ashah-splunk Oct 11, 2022
271c275
conflict resolve
ashah-splunk Oct 12, 2022
507f2f8
compiled files conflict resolve
ashah-splunk Oct 12, 2022
804c828
Revert "conflict resolve"
ashah-splunk Oct 12, 2022
6f73da3
Revert "compiled files conflict resolve"
ashah-splunk Oct 12, 2022
b6b17a8
conflict resolve
ashah-splunk Oct 12, 2022
dc131cc
Merge branch 'develop' into callback-promises-migration
ashah-splunk Oct 12, 2022
750f32d
compiled files
ashah-splunk Oct 12, 2022
c281e53
README changes
ashah-splunk Oct 14, 2022
cd9e97c
replace statuscode with 504
akaila-splunk Oct 31, 2022
6b7d83b
compiled files
ashah-splunk Oct 31, 2022
122ba74
Update CHANGELOG.md
akaila-splunk Oct 31, 2022
4465c19
update browser-side abort timeout
akaila-splunk Nov 2, 2022
e7aa58d
compiled files
ashah-splunk Nov 2, 2022
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1,691 changes: 845 additions & 846 deletions bin/cli.js
100755 → 100644

Large diffs are not rendered by default.

510 changes: 0 additions & 510 deletions client/browser_async.js

This file was deleted.

1,336 changes: 618 additions & 718 deletions client/browser_context.js

Large diffs are not rendered by default.

9,755 changes: 3,941 additions & 5,814 deletions client/browser_service.js

Large diffs are not rendered by default.

802 changes: 576 additions & 226 deletions client/browser_utils.js

Large diffs are not rendered by default.

250 changes: 94 additions & 156 deletions lib/context.js

Large diffs are not rendered by default.

119 changes: 58 additions & 61 deletions lib/http.js
Original file line number Diff line number Diff line change
Expand Up @@ -65,8 +65,7 @@
*/
init: function() {

// We perform the bindings so that every function works
// properly when it is passed as a callback.
// We perform the bindings so that every function works properly
this.get = utils.bind(this, this.get);
this.del = utils.bind(this, this.del);
this.post = utils.bind(this, this.post);
Expand All @@ -89,7 +88,7 @@
* Returns all cookies formatted as a string to be put into the Cookie Header.
*/
_getCookieString: function() {
var cookieString = "";
let cookieString = "";

utils.forEach(this._cookieStore, function (cookieValue, cookieKey) {
cookieString += cookieKey;
Expand All @@ -107,8 +106,8 @@
*/
_parseCookieHeader: function(cookieHeader) {
// Returns an object of form { $cookieKey: $cookieValue, $optionalCookieAttributeName: $""value, ... }
var parsedCookieObject = CookieHandler.parse(cookieHeader);
var cookie = {};
let parsedCookieObject = CookieHandler.parse(cookieHeader);
let cookie = {};

// This gets the first key value pair into an object and just repeatedly returns thereafter
utils.forEach(parsedCookieObject, function(cookieValue, cookieKey) {
Expand All @@ -129,19 +128,19 @@
* @param {Object} headers An object of headers for this request.
* @param {Object} params Parameters for this request.
* @param {Number} timeout A timeout period.
* @param {Function} callback The function to call when the request is complete: `(err, response)`.
* @param {Number} response_timeout A timeout period for aborting a request in milisecs (0 means no timeout).
*
* @method splunkjs.Http
*/
get: function(url, headers, params, timeout, callback, isAsync) {
var message = {
get: function(url, headers, params, timeout, response_timeout, isAsync) {
let message = {
method: "GET",
headers: headers,
timeout: timeout,
query: params
response_timeout: response_timeout,
query: params,
};

return this.request(url, message, callback, isAsync);
return this.request(url, message, isAsync);
},

/**
Expand All @@ -151,20 +150,21 @@
* @param {Object} headers An object of headers for this request.
* @param {Object} params Parameters for this request.
* @param {Number} timeout A timeout period.
* @param {Function} callback The function to call when the request is complete: `(err, response)`.
* @param {Number} response_timeout A timeout period for aborting a request in milisecs (0 means no timeout).
*
* @method splunkjs.Http
*/
post: function(url, headers, params, timeout, callback) {
post: function(url, headers, params, timeout, response_timeout) {
headers["Content-Type"] = "application/x-www-form-urlencoded";
var message = {
let message = {
method: "POST",
headers: headers,
timeout: timeout,
response_timeout: response_timeout,
post: params
};

return this.request(url, message, callback);
return this.request(url, message);
},

/**
Expand All @@ -174,19 +174,20 @@
* @param {Object} headers An object of headers for this request.
* @param {Object} params Query parameters for this request.
* @param {Number} timeout A timeout period.
* @param {Function} callback The function to call when the request is complete: `(err, response)`.
* @param {Number} response_timeout A timeout period for aborting a request in milisecs (0 means no timeout).
*
* @method splunkjs.Http
*/
del: function(url, headers, params, timeout, callback) {
var message = {
del: function(url, headers, params, timeout, response_timeout) {
let message = {
method: "DELETE",
headers: headers,
timeout: timeout,
response_timeout: response_timeout,
query: params
};

return this.request(url, message, callback);
return this.request(url, message);
},

/**
Expand All @@ -197,20 +198,19 @@
*
* @param {String} url The encoded URL of the request.
* @param {Object} message An object with values for method, headers, timeout, and encoded body.
* @param {Function} callback The function to call when the request is complete: `(err, response)`.
*
* @method splunkjs.Http
* @see makeRequest
*/
request: function(url, message, callback, isAsync) {
request: function(url, message, isAsync) {
var that = this;
var query = utils.getWithVersion(this.version, queryBuilderMap)(message);
var post = message.post || {};
let query = utils.getWithVersion(this.version, queryBuilderMap)(message);
let post = message.post || {};

var encodedUrl = url + "?" + Http.encode(query);
var body = message.body ? message.body : Http.encode(post);
let encodedUrl = url + "?" + Http.encode(query);
let body = message.body ? message.body : Http.encode(post);

var cookieString = that._getCookieString();
let cookieString = that._getCookieString();

if (cookieString.length !== 0) {
message.headers["Cookie"] = cookieString;
Expand All @@ -220,44 +220,42 @@
delete message.headers["Authorization"];
}

var options = {
let options = {
method: message.method,
headers: message.headers,
timeout: message.timeout,
response_timeout: message.response_timeout,
query: message.query,
body: body
};

// Now we can invoke the user-provided HTTP class,
// passing in our "wrapped" callback
if(isAsync) {
return this.makeRequestAsync(encodedUrl, options);
}
else {
var wrappedCallback = function(response) {
callback = callback || function() {};

// Handle cookies if 'set-cookie' header is in the response

var cookieHeaders = response.response.headers['set-cookie'];
if (cookieHeaders) {
utils.forEach(cookieHeaders, function (cookieHeader) {
var cookie = that._parseCookieHeader(cookieHeader);
that._cookieStore[cookie.key] = cookie.value;
});
}

// Handle callback

if (response.status < 400 && response.status !== "abort") {
callback(null, response);
let res = this.makeRequest(encodedUrl, options);
return res.then((response) => {
if(!response){
return;
}
else {
callback(response);
}
};
// Handle cookies if 'set-cookie' header is in the response
let cookieHeaders = response.response.headers['set-cookie'];
if (cookieHeaders) {
utils.forEach(cookieHeaders, function (cookieHeader) {
let cookie = that._parseCookieHeader(cookieHeader);
that._cookieStore[cookie.key] = cookie.value;
});
}

return this.makeRequest(encodedUrl, options, wrappedCallback);
if (response.status < 400 && response.status !== "abort") {
return response;
}
else {
throw response;
}
}).catch((error) => {
throw error;
});
}
},

Expand All @@ -267,11 +265,10 @@
*
* @param {String} url The encoded URL of the request.
* @param {Object} message An object with values for method, headers, timeout, and encoded body.
* @param {Function} callback The function to call when the request is complete: `(err, response)`.
*
* @method splunkjs.Http
*/
makeRequest: function(url, message, callback) {
makeRequest: function(url, message) {
throw new Error("UNDEFINED FUNCTION - OVERRIDE REQUIRED");
},

Expand All @@ -298,9 +295,9 @@
* @method splunkjs.Http
*/
_buildResponse: function(error, response, data) {
var complete_response, json = {};
let complete_response, json = {};

var contentType = null;
let contentType = null;
if (response && response.headers) {
contentType = utils.trim(response.headers["content-type"] || response.headers["Content-Type"] || response.headers["Content-type"] || response.headers["contentType"]);
}
Expand Down Expand Up @@ -347,10 +344,10 @@
* @function splunkjs.Http
*/
Http.encode = function(params) {
var encodedStr = "";
let encodedStr = "";

// We loop over all the keys so we encode them.
for (var key in params) {
for (let key in params) {
if (params.hasOwnProperty(key)) {
// Only append the ampersand if we already have
// something encoded, and the last character isn't
Expand All @@ -360,19 +357,19 @@
}

// Get the value
var value = params[key];
let value = params[key];

// If it's an array, we loop over each value
// and encode it in the form &key=value[i]
if (value instanceof Array) {
for (var i = 0; i < value.length; i++) {
for (let i = 0; i < value.length; i++) {
encodedStr = encodedStr + key + "=" + encodeURIComponent(value[i]) + "&";
}
}
else if (typeof value === "object") {
for(var innerKey in value) {
for(let innerKey in value) {
if (value.hasOwnProperty(innerKey)) {
var innerValue = value[innerKey];
let innerValue = value[innerKey];
encodedStr = encodedStr + key + "=" + encodeURIComponent(value[innerKey]) + "&";
}
}
Expand Down
Loading