2017-01-23 21:07:22 -05:00
|
|
|
/*
|
2017-02-22 20:27:26 -05:00
|
|
|
* Minio Cloud Storage (C) 2016 Minio, Inc.
|
2017-01-23 21:07:22 -05: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-21 14:09:23 -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-21 14:09:23 -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-21 14:09:23 -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-21 14:09:23 -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-21 14:09:23 -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-21 14:09:23 -04:00
|
|
|
options.id = 1
|
2017-01-23 21:07:22 -05:00
|
|
|
}
|
|
|
|
if (!options.params) {
|
2018-03-21 14:09:23 -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-22 15:25:56 -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-21 14:09:23 -04:00
|
|
|
method: "POST",
|
2017-01-23 21:07:22 -05:00
|
|
|
headers: {
|
2018-03-21 14:09:23 -04:00
|
|
|
"Content-Type": "application/json",
|
|
|
|
"x-amz-date":
|
|
|
|
Moment()
|
|
|
|
.utc()
|
2018-03-22 15:25:56 -04:00
|
|
|
.format("YYYYMMDDTHHmmss") + "Z"
|
|
|
|
}
|
2017-01-23 21:07:22 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
if (token) {
|
2018-03-21 14:09:23 -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)
|
|
|
|
}
|
|
|
|
}
|