Files
elasticsearch-js/api/api/delete_by_query_rethrottle.js
2018-12-13 16:53:21 +01:00

125 lines
3.5 KiB
JavaScript

'use strict'
function buildDeleteByQueryRethrottle (opts) {
// eslint-disable-next-line no-unused-vars
const { makeRequest, ConfigurationError, result } = opts
/**
* Perform a [delete_by_query_rethrottle](https://www.elastic.co/guide/en/elasticsearch/reference/current/docs-delete-by-query.html) request
*
* @param {string} task_id - The task id to rethrottle
* @param {number} requests_per_second - The throttle to set on this request in floating sub-requests per second. -1 means set no throttle.
*/
return function deleteByQueryRethrottle (params, options, callback) {
options = options || {}
if (typeof options === 'function') {
callback = options
options = {}
}
if (typeof params === 'function' || params == null) {
callback = params
params = {}
options = {}
}
// promises support
if (callback == null) {
return new Promise((resolve, reject) => {
deleteByQueryRethrottle(params, options, (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['requests_per_second'] == null && params['requestsPerSecond'] == null) {
return callback(
new ConfigurationError('Missing required parameter: requests_per_second or requestsPerSecond'),
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 = [
'requests_per_second',
'pretty',
'human',
'error_trace',
'source',
'filter_path'
]
const acceptedQuerystringCamelCased = [
'requestsPerSecond',
'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 = 'POST'
}
// 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 = options.ignore || null
if (typeof ignore === 'number') {
ignore = [ignore]
}
// build request object
const parts = ['_delete_by_query', params['task_id'] || params['taskId'], '_rethrottle']
const request = {
method,
path: '/' + parts.filter(Boolean).map(encodeURIComponent).join('/'),
body: '',
querystring
}
const requestOptions = {
ignore,
requestTimeout: options.requestTimeout || null,
maxRetries: options.maxRetries || null,
asStream: options.asStream || false,
headers: options.headers || null
}
return makeRequest(request, requestOptions, callback)
}
}
module.exports = buildDeleteByQueryRethrottle