121 lines
3.4 KiB
JavaScript
121 lines
3.4 KiB
JavaScript
'use strict'
|
|
|
|
function buildXpackMlGetCalendarEvents (opts) {
|
|
// eslint-disable-next-line no-unused-vars
|
|
const { makeRequest, ConfigurationError, result } = opts
|
|
/**
|
|
* Perform a [xpack.ml.get_calendar_events](undefined) request
|
|
*
|
|
* @param {string} calendar_id - The ID of the calendar containing the events
|
|
* @param {string} job_id - Get events for the job. When this option is used calendar_id must be '_all'
|
|
* @param {string} start - Get events after this time
|
|
* @param {date} end - Get events before this time
|
|
* @param {int} from - Skips a number of events
|
|
* @param {int} size - Specifies a max number of events to get
|
|
*/
|
|
return function xpackMlGetCalendarEvents (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) => {
|
|
xpackMlGetCalendarEvents(params, options, (err, body) => {
|
|
err ? reject(err) : resolve(body)
|
|
})
|
|
})
|
|
}
|
|
|
|
// check required parameters
|
|
if (params['calendar_id'] == null && params['calendarId'] == null) {
|
|
return callback(
|
|
new ConfigurationError('Missing required parameter: calendar_id or calendarId'),
|
|
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 = [
|
|
'job_id',
|
|
'start',
|
|
'end',
|
|
'from',
|
|
'size'
|
|
]
|
|
const acceptedQuerystringCamelCased = [
|
|
'jobId',
|
|
'start',
|
|
'end',
|
|
'from',
|
|
'size'
|
|
]
|
|
|
|
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 = options.ignore || null
|
|
if (typeof ignore === 'number') {
|
|
ignore = [ignore]
|
|
}
|
|
|
|
// build request object
|
|
const parts = ['_xpack', 'ml', 'calendars', params['calendar_id'] || params['calendarId'], 'events']
|
|
const request = {
|
|
method,
|
|
path: '/' + parts.filter(Boolean).map(encodeURIComponent).join('/'),
|
|
body: null,
|
|
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 = buildXpackMlGetCalendarEvents
|