Files
elasticsearch-js/api/api/tasks.get.js
2018-11-15 17:46:33 +01:00

111 lines
2.9 KiB
JavaScript

'use strict'
function buildTasksGet (opts) {
// eslint-disable-next-line no-unused-vars
const { makeRequest, ConfigurationError, result } = opts
/**
* Perform a [tasks.get](http://www.elastic.co/guide/en/elasticsearch/reference/master/tasks.html) request
*
* @param {string} task_id - Return the task with specified id (node_id:task_number)
* @param {boolean} wait_for_completion - Wait for the matching tasks to complete (default: false)
* @param {time} timeout - Explicit operation timeout
*/
return function tasksGet (params, callback) {
if (typeof params === 'function' || params == null) {
callback = params
params = {}
}
// promises support
if (callback == null) {
return new Promise((resolve, reject) => {
tasksGet(params, (err, body) => {
err ? reject(err) : resolve(body)
})
})
}
// check required parameters
if (params['task_id'] == null && params['taskId'] == null) {
return callback(
new ConfigurationError('Missing required parameter: task_id or taskId'),
result
)
}
if (params.body != null) {
return callback(
new ConfigurationError('This API does not require a body'),
result
)
}
// build querystring object
const querystring = {}
const keys = Object.keys(params)
const acceptedQuerystring = [
'wait_for_completion',
'timeout',
'pretty',
'human',
'error_trace',
'source',
'filter_path'
]
const acceptedQuerystringCamelCased = [
'waitForCompletion',
'timeout',
'pretty',
'human',
'errorTrace',
'source',
'filterPath'
]
for (var i = 0, len = keys.length; i < len; i++) {
var key = keys[i]
if (acceptedQuerystring.indexOf(key) !== -1) {
querystring[key] = params[key]
} else {
var camelIndex = acceptedQuerystringCamelCased.indexOf(key)
if (camelIndex !== -1) {
querystring[acceptedQuerystring[camelIndex]] = params[key]
}
}
}
// configure http method
var method = params.method
if (method == null) {
method = 'GET'
}
// validate headers object
if (params.headers != null && typeof params.headers !== 'object') {
return callback(
new ConfigurationError(`Headers should be an object, instead got: ${typeof params.headers}`),
result
)
}
var ignore = params.ignore || null
if (typeof ignore === 'number') {
ignore = [ignore]
}
// build request object
const parts = ['_tasks', params['task_id'] || params['taskId']]
const request = {
method,
path: '/' + parts.filter(Boolean).map(encodeURIComponent).join('/'),
querystring,
body: null,
headers: params.headers || null,
ignore,
requestTimeout: params.requestTimeout || null
}
return makeRequest(request, callback)
}
}
module.exports = buildTasksGet