2017-01-24 03:07:22 +01:00
|
|
|
/*
|
2019-04-09 20:39:42 +02:00
|
|
|
* MinIO Cloud Storage (C) 2016 MinIO, Inc.
|
2017-01-24 03:07:22 +01:00
|
|
|
*
|
|
|
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
* you may not use this file except in compliance with the License.
|
|
|
|
* You may obtain a copy of the License at
|
|
|
|
*
|
|
|
|
* http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
*
|
|
|
|
* Unless required by applicable law or agreed to in writing, software
|
|
|
|
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
* See the License for the specific language governing permissions and
|
|
|
|
* limitations under the License.
|
|
|
|
*/
|
|
|
|
|
2018-03-26 21:49:12 +02:00
|
|
|
import SuperAgent from 'superagent-es6-promise';
|
|
|
|
import url from 'url'
|
|
|
|
import Moment from 'moment'
|
2017-01-24 03:07:22 +01:00
|
|
|
|
|
|
|
export default class JSONrpc {
|
|
|
|
constructor(params) {
|
|
|
|
this.endpoint = params.endpoint
|
|
|
|
this.namespace = params.namespace
|
2018-03-26 21:49:12 +02:00
|
|
|
this.version = '2.0';
|
2017-01-24 03:07:22 +01: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 21:49:12 +02:00
|
|
|
case 'http:': {
|
|
|
|
this.scheme = 'http'
|
2017-01-24 03:07:22 +01:00
|
|
|
if (parsedUrl.port === 0) {
|
|
|
|
this.port = 80
|
|
|
|
}
|
|
|
|
break
|
|
|
|
}
|
2018-03-26 21:49:12 +02:00
|
|
|
case 'https:': {
|
|
|
|
this.scheme = 'https'
|
2017-01-24 03:07:22 +01:00
|
|
|
if (parsedUrl.port === 0) {
|
|
|
|
this.port = 443
|
|
|
|
}
|
|
|
|
break
|
|
|
|
}
|
|
|
|
default: {
|
2018-03-26 21:49:12 +02:00
|
|
|
throw new Error('Unknown protocol: ' + parsedUrl.protocol)
|
2017-01-24 03:07:22 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
// call('Get', {id: NN, params: [...]}, function() {})
|
|
|
|
call(method, options, token) {
|
|
|
|
if (!options) {
|
|
|
|
options = {}
|
|
|
|
}
|
|
|
|
if (!options.id) {
|
2018-03-26 21:49:12 +02:00
|
|
|
options.id = 1;
|
2017-01-24 03:07:22 +01:00
|
|
|
}
|
|
|
|
if (!options.params) {
|
2018-03-26 21:49:12 +02:00
|
|
|
options.params = {};
|
2017-01-24 03:07:22 +01:00
|
|
|
}
|
|
|
|
const dataObj = {
|
|
|
|
id: options.id,
|
|
|
|
jsonrpc: this.version,
|
|
|
|
params: options.params ? options.params : {},
|
2018-03-26 21:49:12 +02:00
|
|
|
method: this.namespace ? this.namespace + '.' + method : method
|
2017-01-24 03:07:22 +01:00
|
|
|
}
|
|
|
|
let requestParams = {
|
|
|
|
host: this.host,
|
|
|
|
port: this.port,
|
|
|
|
path: this.path,
|
|
|
|
scheme: this.scheme,
|
2018-03-26 21:49:12 +02:00
|
|
|
method: 'POST',
|
2017-01-24 03:07:22 +01:00
|
|
|
headers: {
|
2018-03-26 21:49:12 +02:00
|
|
|
'Content-Type': 'application/json',
|
|
|
|
'x-amz-date': Moment().utc().format('YYYYMMDDTHHmmss') + 'Z'
|
2018-03-22 20:25:56 +01:00
|
|
|
}
|
2017-01-24 03:07:22 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
if (token) {
|
2018-03-26 21:49:12 +02:00
|
|
|
requestParams.headers.Authorization = 'Bearer ' + token
|
2017-01-24 03:07:22 +01: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)
|
|
|
|
}
|
|
|
|
}
|