welsonjs/lib/jsonrpc2.js

59 lines
1.4 KiB
JavaScript
Raw Normal View History

2024-04-07 06:55:16 +00:00
// jsonrpc2.js
2024-06-30 16:27:51 +00:00
// JSON-RPC 2.0 wrapper for WelsonJS framework
2024-04-07 06:55:16 +00:00
// Namhyeon Go <abuse@catswords.net>
// https://github.com/gnh1201/welsonjs
2024-04-07 06:48:40 +00:00
var HTTP = require("lib/http");
2024-06-26 07:37:04 +00:00
function jsonrpc2(url) {
this.url = url;
2024-06-30 16:27:51 +00:00
this.userAgent = "php-httpproxy/0.1.5 (Client; WelsonJS; abuse@catswords.net)";
this.setUserAgent = function(agent) {
this.userAgent = agent;
};
2024-06-26 08:17:15 +00:00
this.invoke = function(method, params, id) {
2024-04-07 06:53:55 +00:00
var result;
var response = HTTP.create("MSXML")
2024-04-07 06:48:40 +00:00
.setContentType("application/json")
.setDataType("json")
2024-06-30 16:27:51 +00:00
.setUserAgent(this.userAgent)
.setRequestBody(wrap(method, params, id))
2024-04-07 06:48:40 +00:00
.open("POST", this.url)
.send()
.responseBody
;
2024-04-07 06:53:55 +00:00
if ("error" in response) {
console.error(response.error.message);
return;
}
if ("result" in response) {
result = response.result;
}
return result;
2024-04-07 06:48:40 +00:00
}
}
2024-06-30 16:27:51 +00:00
function wrap(method, params, id) {
return {
2024-06-26 07:37:04 +00:00
"jsonrpc": "2.0",
"method": method,
"params": params,
"id": id
2024-06-30 16:27:51 +00:00
};
2024-06-26 07:37:04 +00:00
}
function create(url) {
return new jsonrpc2(url);
2024-04-07 06:48:40 +00:00
}
2024-06-30 16:27:51 +00:00
exports.wrap = wrap;
2024-04-07 06:48:40 +00:00
exports.create = create;
2024-06-30 16:27:51 +00:00
exports.VERSIONINFO = "JSON-RPC 2.0 wrapper (jsonrpc2.js) version 0.1.4";
2024-04-07 06:48:40 +00:00
exports.AUTHOR = "abuse@catswords.net";
exports.global = global;
exports.require = global.require;