2017-01-23 21:07:22 -05:00
|
|
|
/*
|
2021-04-19 13:30:42 -04:00
|
|
|
* Copyright (c) 2015-2021 MinIO, Inc.
|
2017-01-23 21:07:22 -05:00
|
|
|
*
|
2021-04-19 13:30:42 -04:00
|
|
|
* This file is part of MinIO Object Storage stack
|
2017-01-23 21:07:22 -05:00
|
|
|
*
|
2021-04-19 13:30:42 -04:00
|
|
|
* This program is free software: you can redistribute it and/or modify
|
|
|
|
* it under the terms of the GNU Affero General Public License as published by
|
|
|
|
* the Free Software Foundation, either version 3 of the License, or
|
|
|
|
* (at your option) any later version.
|
2017-01-23 21:07:22 -05:00
|
|
|
*
|
2021-04-19 13:30:42 -04:00
|
|
|
* This program is distributed in the hope that it will be useful
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU Affero General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU Affero General Public License
|
|
|
|
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
2017-01-23 21:07:22 -05:00
|
|
|
*/
|
|
|
|
|
2018-03-26 15:49:12 -04:00
|
|
|
import SuperAgent from 'superagent-es6-promise';
|
|
|
|
import url from 'url'
|
|
|
|
import Moment from 'moment'
|
2017-01-23 21:07:22 -05:00
|
|
|
|
|
|
|
export default class JSONrpc {
|
|
|
|
constructor(params) {
|
|
|
|
this.endpoint = params.endpoint
|
|
|
|
this.namespace = params.namespace
|
2018-03-26 15:49:12 -04:00
|
|
|
this.version = '2.0';
|
2017-01-23 21:07:22 -05:00
|
|
|
const parsedUrl = url.parse(this.endpoint)
|
|
|
|
this.host = parsedUrl.hostname
|
|
|
|
this.path = parsedUrl.path
|
|
|
|
this.port = parsedUrl.port
|
|
|
|
|
|
|
|
switch (parsedUrl.protocol) {
|
2018-03-26 15:49:12 -04:00
|
|
|
case 'http:': {
|
|
|
|
this.scheme = 'http'
|
2017-01-23 21:07:22 -05:00
|
|
|
if (parsedUrl.port === 0) {
|
|
|
|
this.port = 80
|
|
|
|
}
|
|
|
|
break
|
|
|
|
}
|
2018-03-26 15:49:12 -04:00
|
|
|
case 'https:': {
|
|
|
|
this.scheme = 'https'
|
2017-01-23 21:07:22 -05:00
|
|
|
if (parsedUrl.port === 0) {
|
|
|
|
this.port = 443
|
|
|
|
}
|
|
|
|
break
|
|
|
|
}
|
|
|
|
default: {
|
2018-03-26 15:49:12 -04:00
|
|
|
throw new Error('Unknown protocol: ' + parsedUrl.protocol)
|
2017-01-23 21:07:22 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
// call('Get', {id: NN, params: [...]}, function() {})
|
|
|
|
call(method, options, token) {
|
|
|
|
if (!options) {
|
|
|
|
options = {}
|
|
|
|
}
|
|
|
|
if (!options.id) {
|
2018-03-26 15:49:12 -04:00
|
|
|
options.id = 1;
|
2017-01-23 21:07:22 -05:00
|
|
|
}
|
|
|
|
if (!options.params) {
|
2018-03-26 15:49:12 -04:00
|
|
|
options.params = {};
|
2017-01-23 21:07:22 -05:00
|
|
|
}
|
|
|
|
const dataObj = {
|
|
|
|
id: options.id,
|
|
|
|
jsonrpc: this.version,
|
|
|
|
params: options.params ? options.params : {},
|
2018-03-26 15:49:12 -04:00
|
|
|
method: this.namespace ? this.namespace + '.' + method : method
|
2017-01-23 21:07:22 -05:00
|
|
|
}
|
|
|
|
let requestParams = {
|
|
|
|
host: this.host,
|
|
|
|
port: this.port,
|
|
|
|
path: this.path,
|
|
|
|
scheme: this.scheme,
|
2018-03-26 15:49:12 -04:00
|
|
|
method: 'POST',
|
2017-01-23 21:07:22 -05:00
|
|
|
headers: {
|
2018-03-26 15:49:12 -04:00
|
|
|
'Content-Type': 'application/json',
|
|
|
|
'x-amz-date': Moment().utc().format('YYYYMMDDTHHmmss') + 'Z'
|
2018-03-22 15:25:56 -04:00
|
|
|
}
|
2017-01-23 21:07:22 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
if (token) {
|
2018-03-26 15:49:12 -04:00
|
|
|
requestParams.headers.Authorization = 'Bearer ' + token
|
2017-01-23 21:07:22 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
let req = SuperAgent.post(this.endpoint)
|
|
|
|
for (let key in requestParams.headers) {
|
|
|
|
req.set(key, requestParams.headers[key])
|
|
|
|
}
|
|
|
|
// req.set('Access-Control-Allow-Origin', 'http://localhost:8080')
|
|
|
|
return req.send(JSON.stringify(dataObj)).then(res => res)
|
|
|
|
}
|
|
|
|
}
|