Permalink
Browse files

login works with new XML

  • Loading branch information...
1 parent 8c64dfe commit 29d1eec620e58173e2407de940c4debc6cfbe1d5 @AlgoTrader committed Apr 1, 2012
Showing with 136 additions and 81 deletions.
  1. +23 −0 LICENSE
  2. +56 −34 lib/betfair_decoder.js
  3. +57 −47 lib/betfair_encoder.js
View
23 LICENSE
@@ -0,0 +1,23 @@
+Copyright 2012 Anton Zemlyanov.
+All rights reserved.
+
+Permission is hereby granted, free of charge, to any person
+obtaining a copy of this software and associated documentation
+files (the "Software"), to deal in the Software without
+restriction, including without limitation the rights to use,
+copy, modify, merge, publish, distribute, sublicense, and/or sell
+copies of the Software, and to permit persons to whom the
+Software is furnished to do so, subject to the following
+conditions:
+
+The above copyright notice and this permission notice shall be
+included in all copies or substantial portions of the Software.
+
+THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
+EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
+OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
+NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
+HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
+WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
+FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
+OTHER DEALINGS IN THE SOFTWARE.
View
@@ -6,67 +6,82 @@
//Exported properties:
//decode - converts Betfair SOAP XML response into native JavaScript object
-var xmljs = require('libxmljs');
+var DomJS = require('dom-js').DomJS;
+// var xmljs = require('libxmljs');
var util = require('util');
exports.decode = function(xml, schema) {
// parse XML
+ var domjs = new DomJS;
+ var root;
try {
- var xmlDoc = xmljs.parseXmlString(xml);
+ domjs.parse(xml, function(err, res) {
+ // console.log(err);
+ //console.log(util.inspect(res, true, 10));
+ root = res;
+ return false;
+ });
} catch (ex) {
return {
error : 'Bad XML in response'
};
}
- // get SOAP body tag element
- var soapBody = xmlDoc.get('/soap:Envelope/soap:Body', {
- soap : 'http://schemas.xmlsoap.org/soap/envelope/'
- });
- if (!soapBody)
+ // check Envelope and get SOAP body
+ if (root.name != "soap:Envelope" && root.children.length != 1)
return {
- error : 'Not SOAP response'
+ error : 'Bad SOAP response, no Envelope'
};
+ var body = root.children[0];
- // check is SOAP fault
- var soapFault = soapBody.get('soap:Fault', {
- soap : 'http://schemas.xmlsoap.org/soap/envelope/'
- });
- if (soapFault)
+ // check Body and get Fault or Result
+ if (body.name != "soap:Body" && body.children.length != 1)
+ return {
+ error : 'Bad SOAP response, no Body'
+ };
+ var faultOrAction = body.children[0];
+
+ // check is SOAP Fault
+ if (faultOrAction.name === "soap:Fault")
return {
error : 'SOAP Fault'
};
- // find SOAP result tag
- var result = soapBody.get('*/n:Result', {
- n : schema
- });
- if (!result)
+ // check Action tag
+ if (faultOrAction.children.length != 1)
+ return {
+ error : 'Bad SOAP response, no Action tag'
+ };
+ var result = faultOrAction.children[0];
+
+ // check Response tag
+ if (!endsWith(result.name, 'Result'))
return {
- error : 'No SOAP result'
+ error : 'Bad SOAP response, no Result tag'
};
- // valid SOAP result, parse it
- var decoded = decodeNode(result);
+ var decodedData = decodeNode(result);
- return decoded;
+ return decodedData;
}
function decodeNode(node) {
var nodeType = detectNodeType(node);
- var children = node.childNodes();
+ var children = node.children;
+ //console.log(nodeType);
switch (nodeType) {
case 'array':
var arr = [];
- for ( var child in node.childNodes()) {
- arr.push(decodeNode(children[child]));
+ for ( var child in children) {
+ // arr.push(decodeNode(children[child]));
}
return arr;
case 'object':
var obj = {};
- for ( var child in node.childNodes()) {
- obj[children[child].name()] = decodeNode(children[child]);
+ for ( var childIndex in children) {
+ //console.log(childIndex);
+ obj[children[childIndex].name] = decodeNode(children[childIndex]);
}
return obj;
case 'datetime':
@@ -79,20 +94,27 @@ function decodeNode(node) {
}
function detectNodeType(node) {
- var children = node.childNodes();
- var attrType = node.attr('type');
-
- if (children.length === 0) {
+ var children = node.children;
+ var attrType = node.attributes && node.attributes['xsi:type'];
+ //console.log('===========================');
+ //console.log(children);
+ //console.log(attrType);
+ //console.log('===========================');
+
+ if (children && children.length === 0) {
return 'string';
- } else if (children.length === 1 && children[0].name() === 'text') {
- if (attrType && attrType.value().match(/^xsd:dateTime/))
+ } else if (children.length === 1 && children[0].text) {
+ if (attrType && attrType.match(/^xsd:dateTime/))
return 'datetime';
else
return 'string';
- } else if (attrType && attrType.value().match(/ArrayOf/)) {
+ } else if (attrType && attrType.match(/ArrayOf/)) {
return 'array';
}
return 'object';
+}
+function endsWith(str, suffix) {
+ return str.indexOf(suffix, str.length - suffix.length) !== -1;
}
View
@@ -1,97 +1,107 @@
-// (C) 2012 Anton Zemlyanov
-//
-// The module "encodes" native JavaScript objects into SOAP xml invocation requests
-// see Sports API documentation on http://bdp.betfair.com
-//
-// Exported properties:
-// encode - converts native JavaScript object into Betfair SOAP XML request
+//(C) 2012 Anton Zemlyanov
-var xmljs = require('libxmljs');
+//The module "encodes" native JavaScript objects into SOAP xml invocation requests
+//see Sports API documentation on http://bdp.betfair.com
+
+//Exported properties:
+//encode - converts native JavaScript object into Betfair SOAP XML request
+
+var DomJS = require('dom-js').DomJS;
+var Element = require('dom-js').Element;
+var Text = require('dom-js').Text;
+// var xmljs = require('libxmljs');
var util = require('util');
exports.encode = function(action, schema, input) {
- var xmldoc = new xmljs.Document();
-
- // soap:Envelope
- xmldoc.node('soap:Envelope');
- var root = xmldoc.root();
- root.attr({
- 'xmlns:soap' : 'http://schemas.xmlsoap.org/soap/envelope/'
- });
- root.attr({
- 'xmlns:xsi' : 'http://www.w3.org/2001/XMLSchema-instance'
- });
- root.attr({
+ // Envelope tag
+ var root = new Element('soap:Envelope', {
+ 'xmlns:soap' : 'http://schemas.xmlsoap.org/soap/envelope/',
+ 'xmlns:xsi' : 'http://www.w3.org/2001/XMLSchema-instance',
'xmlns:xsd' : 'http://www.w3.org/'
});
- var soapBody = root.node('soap:Body');
+ // Body tag
+ var body = new Element('soap:Body');
+ root.children.push(body);
- var action = soapBody.node(action);
- action.attr({
+ // action tag
+ var act = new Element(action, {
xmlns : schema
});
+ body.children.push(act);
+
+ // request tag
+ var request = new Element("request");
+ act.children.push(request);
- var request = action.node('request');
+ // incode input
encodeObject(input, request);
- return xmldoc.toString();
+ // serialize document to XML
+ // var domjs = new DomJS();
+ var text = root.toXml();
+ console.log("text", text);
+
+ return text;
}
function encodeObject(object, parent) {
for ( var itemKey in object) {
var itemVal = object[itemKey];
- // console.log(itemKey + "===" + itemVal);
-
+ //console.log(itemKey + "===" + itemVal);
+ //console.log(itemVal);
switch (typeof itemVal) {
case 'string':
- // console.log("string");
- parent.node(itemKey, itemVal);
+ var strElement = new Element(itemKey, {}, [ new Text(itemVal) ]);
+ parent.children.push(strElement);
break;
case 'number':
- // console.log("number");
- parent.node(itemKey, itemVal.toString());
+ var strElement = new Element(itemKey, {}, [ new Text(itemVal
+ .toString()) ]);
+ parent.children.push(strElement);
break;
case 'object':
- // console.log("object");
- // console.log(util.isArray(itemVal));
if (util.isArray(itemVal)) {
// encode array
- var node = parent.node(itemKey, '');
- encodeArray(itemVal, node);
+ var arrElement = new Element(itemKey);
+ arrElement.children.push(encodeArray(itemVal, arrElement));
+ parent.children.push(arrElement);
} else {
// encode object
- var node = parent.node(itemKey, '');
- encodeObject(itemVal, node);
+ var objElement = new Element(itemKey);
+ arrElement.children.push(encodeObject(itemVal, objElement));
+ parent.children.push(objElement);
}
-
- break;
default:
// console.log("unknown");
break;
}
}
+ return;
}
function encodeArray(array, parent) {
- //console.log("array", array);
+ // console.log("array", array);
var itemTagName = array.shift();
for ( var itemKey in array) {
var itemVal = array[itemKey];
- //console.log("itemVal", itemVal);
-
+ console.log("encodeArray: itemVal", itemVal);
switch (typeof itemVal) {
case 'string':
- // console.log("string");
- parent.node(itemTagName, itemVal);
+ console.log("string");
+ var strElement = new Element(itemKey, {}, [ new Text(itemVal) ]);
+ parent.children.push(strElement);
break;
case 'number':
- // console.log("number");
- parent.node(itemTagName, itemVal.toString());
+ console.log("number");
+ var strElement = new Element(itemKey, {}, [ new Text(itemVal
+ .toString()) ]);
+ parent.children.push(strElement);
break;
default:
// console.log("unknown");
break;
}
}
-}
+ return;
+}

0 comments on commit 29d1eec

Please sign in to comment.