Constructor for splunkjs.Http
Constructor for splunkjs.Http
Name | Type | Description |
---|---|---|
isSplunk | Boolean | Whether or not this is HTTP instance is for talking with splunkjs. |
init: function(isSplunk) {
// Whether or not this HTTP provider is talking to Splunk or not
this.isSplunk = (isSplunk === undefined ? true : isSplunk);
// We perform the bindings so that every function works
// properly when it is passed as a callback.
this.get = utils.bind(this, this.get);
this.del = utils.bind(this, this.del);
this.post = utils.bind(this, this.post);
this.request = utils.bind(this, this.request);
this._buildResponse = utils.bind(this, this._buildResponse);
},
Construct a unified response
This function will generate a unified response given the parameters
Name | Type | Description |
---|---|---|
error | Object | Error object if there was one for the request |
response | Object | The actual response object |
data | Object | The response data |
_buildResponse: function(error, response, data) {
var complete_response, json = {};
var contentType = null;
if (response && response.headers) {
contentType = utils.trim(response.headers["content-type"] || response.headers["Content-Type"]);
}
if (utils.startsWith(contentType, "application/json")) {
json = this.parseJson(data) || {};
}
logger.printMessages(json.messages);
complete_response = {
response: response,
status: (response ? response.statusCode : 0),
data: json,
error: error
};
return complete_response;
}
});
})();
Client-specific JSON parsing logic
This function encapsulates the actual logic for parsing the JSON response.
Name | Type | Description |
---|---|---|
json | String | JSON to parse |
parseJson: function(json) {
throw new Error("UNDEFINED FUNCTION - OVERRIDE REQUIRED");
},
Client-specific request logic
This function encapsulates the actual logic for performing a request, and is meant to be overriden by subclasses.
Name | Type | Description |
---|---|---|
url | String | URL to request (already encoded) |
message | Object | Object with values for method, headers, timeout and encoded body |
Callback | Function | for when the request is complete: |
makeRequest: function(url, message, callback) {
throw new Error("UNDEFINED FUNCTION - OVERRIDE REQUIRED");
},
Perform a request
This function sets up everything to handle the response from a request, but delegates the actual calling to the subclass using makeRequest
.
Name | Type | Description |
---|---|---|
url | String | URL to request (already encoded) |
message | Object | Object with values for method, headers, timeout and encoded body |
Callback | Function | for when the request is complete: |
request: function(url, message, callback) {
var wrappedCallback = function(response) {
callback = callback || function() {};
if (response.status < 400 && response.status !== "abort") {
callback(null, response);
}
else {
callback(response);
}
};
// Now we can invoke the user-provided HTTP class,
// passing in our "wrapped" callback
return this.makeRequest(url, message, wrappedCallback);
},
Perform a DELETE request
Name | Type | Description |
---|---|---|
url | String | URL to request |
headers | Object | Object of headers for this request |
params | Object | Query parameters for this request |
timeout | Number | Timeout (currently ignored) |
callback | Function | Callback for when the request is complete: |
del: function(url, headers, params, timeout, callback) {
var encoded_url = url + "?" + root.encode(params);
var message = {
method: "DELETE",
headers: headers,
timeout: timeout
};
return this.request(encoded_url, message, callback);
},
Perform a POST request
Name | Type | Description |
---|---|---|
url | String | URL to request |
headers | Object | Object of headers for this request |
params | Object | Body parameters for this request |
timeout | Number | Timeout (currently ignored) |
callback | Function | Callback for when the request is complete: |
post: function(url, headers, params, timeout, callback) {
headers["Content-Type"] = "application/x-www-form-urlencoded";
var message = {
method: "POST",
headers: headers,
timeout: timeout,
body: root.encode(params)
};
return this.request(url, message, callback);
},
Perform a POST request
Name | Type | Description |
---|---|---|
url | String | URL to request |
headers | Object | Object of headers for this request |
params | Object | Body parameters for this request |
timeout | Number | Timeout (currently ignored) |
callback | Function | Callback for when the request is complete: |
get: function(url, headers, params, timeout, callback) {
var encoded_url = url + "?" + root.encode(params);
var message = {
method: "GET",
headers: headers,
timeout: timeout
};
return this.request(encoded_url, message, callback);
},
Helper function to encode a dictionary of values into a URL-encoded format.
Name | Type | Description |
---|---|---|
params | Object | Parameters to URL-encode |
// should be a=1&b=2&b=3&b=4
encode({a: 1, b: [2,3,4]})
root.encode = function(params) {
var encodedStr = "";
// We loop over all the keys so we encode them.
for (var key in params) {
if (params.hasOwnProperty(key)) {
// Only append the ampersand if we already have
// something encoded, and the last character isn't
// already an ampersand
if (encodedStr && encodedStr[encodedStr.length - 1] !== "&") {
encodedStr = encodedStr + "&";
}
// Get the value
var 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++) {
encodedStr = encodedStr + key + "=" + encodeURIComponent(value[i]) + "&";
}
}
else if (typeof value === "object") {
for(var innerKey in value) {
if (value.hasOwnProperty(innerKey)) {
var innerValue = value[innerKey];
encodedStr = encodedStr + key + "=" + encodeURIComponent(value[innerKey]) + "&";
}
}
}
else {
// If it's not an array, we just encode it
encodedStr = encodedStr + key + "=" + encodeURIComponent(value);
}
}
}
if (encodedStr[encodedStr.length - 1] === '&') {
encodedStr = encodedStr.substr(0, encodedStr.length - 1);
}
return encodedStr;
};