Skip to content
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

Added Http Namespace #650

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
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
193 changes: 193 additions & 0 deletions api/api/http.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,193 @@
/*
* Copyright OpenSearch Contributors
* SPDX-License-Identifier: Apache-2.0
*
* The OpenSearch Contributors require contributions made to
* this file be licensed under the Apache-2.0 license or a
* compatible open source license.
*
*/

'use strict';

/** @namespace API-HTTP */

const { normalizeArguments, kConfigurationError } = require('../utils');

function HttpApi(transport, ConfigurationError) {
this.transport = transport;
this[kConfigurationError] = ConfigurationError;
}

HttpApi.prototype.request = function (method, params, options, callback) {
[params, options, callback] = normalizeArguments(params, options, callback);
if (Array.isArray(params.body)) {
params.bulkBody = params.body;
delete params.body;
}
options = options || {};
options.headers = params.headers || options.headers;
return this.transport.request({ ...params, method }, options, callback);
};

/**
* Make a customized CONNECT request.
*
* @memberOf API-HTTP
*
* @param {Object} params
* @param {Object} params.path - The URL of the request
* @param {Object} [params.querystring] - The querystring parameters
* @param {Object} [params.headers] - The request headers
* @param {Object} [params.body] - The request body
*
* @param {Object} [options] - Options for {@link Transport#request}
* @param {function} [callback] - Callback that handles errors and response
*
* @returns {{abort: function(), then: function(), catch: function()}|Promise<never>|*}
*/
HttpApi.prototype.connect = function (params, options, callback) {
return this.request('CONNECT', params, options, callback);
};

/**
* Make a customized DELETE request.
*
* @memberOf API-HTTP
*
* @param {Object} params
* @param {Object} params.path - The URL of the request
* @param {Object} [params.querystring] - The querystring parameters
* @param {Object} [params.headers] - The request headers
* @param {Object} [params.body] - The request body
*
* @param {Object} [options] - Options for {@link Transport#request}
* @param {function} [callback] - Callback that handles errors and response
*
* @returns {{abort: function(), then: function(), catch: function()}|Promise<never>|*}
*/
HttpApi.prototype.delete = function (params, options, callback) {
return this.request('DELETE', params, options, callback);
};

/**
* Make a customized GET request.
*
* @memberOf API-HTTP
*
* @param {Object} params
* @param {Object} params.path - The URL of the request
* @param {Object} [params.querystring] - The querystring parameters
* @param {Object} [params.headers] - The request headers
* @param {Object} [params.body] - The request body
*
* @param {Object} [options] - Options for {@link Transport#request}
* @param {function} [callback] - Callback that handles errors and response
*
* @returns {{abort: function(), then: function(), catch: function()}|Promise<never>|*}
*/
HttpApi.prototype.get = function (params, options, callback) {
return this.request('GET', params, options, callback);
};

/**
* Make a customized HEAD request.
*
* @memberOf API-HTTP
*
* @param {Object} params
* @param {Object} params.path - The URL of the request
* @param {Object} [params.querystring] - The querystring parameters
* @param {Object} [params.headers] - The request headers
* @param {Object} [params.body] - The request body
*
* @param {Object} [options] - Options for {@link Transport#request}
* @param {function} [callback] - Callback that handles errors and response
*
* @returns {{abort: function(), then: function(), catch: function()}|Promise<never>|*}
*/
HttpApi.prototype.head = function (params, options, callback) {
return this.request('HEAD', params, options, callback);
};

/**
* Make a customized OPTIONS request.
*
* @memberOf API-HTTP
*
* @param {Object} params
* @param {Object} params.path - The URL of the request
* @param {Object} [params.querystring] - The querystring parameters
* @param {Object} [params.headers] - The request headers
* @param {Object} [params.body] - The request body
*
* @param {Object} [options] - Options for {@link Transport#request}
* @param {function} [callback] - Callback that handles errors and response
*
* @returns {{abort: function(), then: function(), catch: function()}|Promise<never>|*}
*/
HttpApi.prototype.options = function (params, options, callback) {
return this.request('OPTIONS', params, options, callback);
};

/**
* Make a customized POST request.
*
* @memberOf API-HTTP
*
* @param {Object} params
* @param {Object} params.path - The URL of the request
* @param {Object} [params.querystring] - The querystring parameters
* @param {Object} [params.headers] - The request headers
* @param {Object} [params.body] - The request body
*
* @param {Object} [options] - Options for {@link Transport#request}
* @param {function} [callback] - Callback that handles errors and response
*
* @returns {{abort: function(), then: function(), catch: function()}|Promise<never>|*}
*/
HttpApi.prototype.post = function (params, options, callback) {
return this.request('POST', params, options, callback);
};

/**
* Make a customized PUT request.
*
* @memberOf API-HTTP
*
* @param {Object} params
* @param {Object} params.path - The URL of the request
* @param {Object} [params.querystring] - The querystring parameters
* @param {Object} [params.headers] - The request headers
* @param {Object} [params.body] - The request body
*
* @param {Object} [options] - Options for {@link Transport#request}
* @param {function} [callback] - Callback that handles errors and response
*
* @returns {{abort: function(), then: function(), catch: function()}|Promise<never>|*}
*/
HttpApi.prototype.put = function (params, options, callback) {
return this.request('PUT', params, options, callback);
};

/**
* Make a customized TRACE request.
*
* @memberOf API-HTTP
*
* @param {Object} params
* @param {Object} params.path - The URL of the request
* @param {Object} [params.querystring] - The querystring parameters
* @param {Object} [params.headers] - The request headers
* @param {Object} [params.body] - The request body
*
* @param {Object} [options] - Options for {@link Transport#request}
* @param {function} [callback] - Callback that handles errors and response
*
* @returns {{abort: function(), then: function(), catch: function()}|Promise<never>|*}
*/
HttpApi.prototype.trace = function (params, options, callback) {
return this.request('TRACE', params, options, callback);
};

module.exports = HttpApi;
11 changes: 11 additions & 0 deletions api/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@ const getScriptApi = require('./api/get_script');
const getScriptContextApi = require('./api/get_script_context');
const getScriptLanguagesApi = require('./api/get_script_languages');
const getSourceApi = require('./api/get_source');
const HttpApi = require('./api/http');
const indexApi = require('./api/index');
const IndicesApi = require('./api/indices');
const infoApi = require('./api/info');
Expand Down Expand Up @@ -89,6 +90,7 @@ const kCat = Symbol('Cat');
const kCluster = Symbol('Cluster');
const kDanglingIndices = Symbol('DanglingIndices');
const kFeatures = Symbol('Features');
const kHttp = Symbol('Http');
const kIndices = Symbol('Indices');
const kIngest = Symbol('Ingest');
const kNodes = Symbol('Nodes');
Expand All @@ -103,6 +105,7 @@ function OpenSearchAPI(opts) {
this[kCluster] = null;
this[kDanglingIndices] = null;
this[kFeatures] = null;
this[kHttp] = null;
this[kIndices] = null;
this[kIngest] = null;
this[kNodes] = null;
Expand Down Expand Up @@ -264,6 +267,14 @@ Object.defineProperties(OpenSearchAPI.prototype, {
return this.getSource;
},
},
http: {
get() {
if (this[kHttp] === null) {
this[kHttp] = new HttpApi(this.transport, this[kConfigurationError]);
}
return this[kHttp];
},
},
indices: {
get() {
if (this[kIndices] === null) {
Expand Down
Loading
Loading