Commit 342c2afe by Thimo Kraemer

Added plain JSON protocol

parent 02b55154
/*
* KSS-RPC v0.3-beta
* KSS-RPC v0.4-beta
*
* Copyright (c) 2017, joonis new media
* Author: Thimo Kraemer <thimo.kraemer@joonis.de>
......@@ -61,7 +61,7 @@
******************************************************************************************/
var kss = {
version: '0.3-beta',
version: '0.4-beta',
_initialized: false,
_ruleSheets: {},
_eventBinders: {},
......@@ -73,7 +73,7 @@ var kss = {
// Default configuration
logging: 'warn',
config: {
protocol: 'json-rpc', /* json-rpc | xml-rpc | url-encoded */
protocol: 'json-rpc', /* json-rpc | xml-rpc | url-encoded | json */
endpoint: '',
dateEncoding: 'iso8601', /* @timestamp@ | asp.net | class-hinting */
timeout: 10000
......@@ -248,17 +248,17 @@ kss.dateToISO8601 = function(date) {
/**
* Date.parse with progressive enhancement for ISO 8601 <https://github.com/csnover/js-iso8601>
* © 2011 Colin Snover <http://zetafleet.com>
* © 2011 Colin Snover <http://zetafleet.com>
* Released under MIT license.
*/
kss.ISO8601toDate = function(date) {
// ES5 §15.9.4.2 states that the string should attempt to be parsed as a Date Time String Format string
// before falling back to any implementation-specific date parsing, so that’s what we do, even if native
// ES5 §15.9.4.2 states that the string should attempt to be parsed as a Date Time String Format string
// before falling back to any implementation-specific date parsing, so that's what we do, even if native
// implementations could be faster
var s;
// 1 YYYY 2 MM 3 DD 4 HH 5 mm 6 ss 7 msec 8 Z 9 ± 10 tzHH 11 tzmm
if ((s = /^(\d{4}|[+\-]\d{6})(?:-(\d{2})(?:-(\d{2}))?)?(?:T(\d{2}):(\d{2})(?::(\d{2})(?:\.(\d{3}))?)?(?:(Z)|([+\-])(\d{2})(?::(\d{2}))?)?)?$/.exec(date))) {
// avoid NaN timestamps caused by “undefined” values being passed to Date.UTC
// avoid NaN timestamps caused by undefined values being passed to Date.UTC
var numericKeys = [ 1, 4, 5, 6, 7, 10, 11 ];
for (var i = 0, k; (k = numericKeys[i]); ++i) {
s[k] = +s[k] || 0;
......@@ -936,7 +936,7 @@ kss.RPC = function(endpoint, options) {
this._endpoint = endpoint;
//Set other default options
this._methodList = [];
// "json-rpc" || "xml-rpc" || "url-encoded"
// "json-rpc" || "xml-rpc" || "url-encoded" || "json"
this._protocol = 'json-rpc';
this._timeout = 10000;
// "iso8601" || "@timestamp@" || "class-hinting" || "asp.net"
......@@ -953,6 +953,7 @@ kss.RPC = function(endpoint, options) {
case 'json-rpc-v2':
case 'xml-rpc':
case 'url-encoded':
case 'json':
this._protocol = protocol;
break;
default:
......@@ -1028,15 +1029,20 @@ kss.RPC.prototype.invoke = function(method, params,
var postData, headers;
var url = this._endpoint;
// Prepare the URL-ENCODED request
if (this._protocol == 'url-encoded') {
// Prepare the URL-ENCODED or JSON request
if (this._protocol == 'url-encoded' || this._protocol == 'json') {
if (url && url.substr(-1) != '/') {
url += '/';
}
url += method;
postData = params || {};
var contentType = 'application/x-www-form-urlencoded';
if (this._protocol == 'json') {
postData = kss.toJSON(postData, this._dateEncoding);
contentType = 'application/json';
}
headers = {
'Content-Type': 'application/x-www-form-urlencoded',
'Content-Type': contentType,
'Accept': 'application/json'
};
}
......@@ -1110,12 +1116,12 @@ kss.RPC.prototype._getRPCResponse = function(xhr) {
throw Error("Malformed XML document.");
response = kss.parseXMLRPC(xhr.responseXML);
}
// JSON-RPC || URL-ENCODED
// JSON-RPC || URL-ENCODED || JSON
else {
// Remove security comment delimiters... really?
// json.replace(/^\/\*-secure-([\s\S]*)\*\/\s*$/, "$1");
response = kss.parseJSON(xhr.responseText, this._decodeISO8601);
if (this._protocol == 'url-encoded') {
if (this._protocol == 'url-encoded' || this._protocol == 'json') {
response = {result: response};
}
}
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment