📄 xmlrpc.js
字号:
@param url The URL of the service providing the method.
@param methodName The name of the method to invoke.
@param user=null The user name to use for HTTP authentication.
@param pass=null The password to use for HTTP authentication.
*/
publ.init = function(url, methodName, user, pass){
//this is pretty much a hack.
//we create a function which mimics this class and return it instead of really instanciating an object.
var fn=function(){
//sync or async call
if(typeof arguments[arguments.length-1] != "function"){
var data=getXML(fn.methodName,arguments);
var resp = postData(fn.url, fn.user, fn.password, data);
return handleResponse(resp);
}else{
var args=new Array();
for(var i=0;i<arguments.length;i++){
args.push(arguments[i]);
}
var cb = args.pop();
var data=getXML(fn.methodName, args);
postData(fn.url, fn.user, fn.password, data, function(resp){
var rslt = null;
var exc =null;
try{
rslt = handleResponse(resp);
}catch(e){
exc = e;
}
try{//call the callback for the async call.
cb(rslt,exc);
}catch(e){
}
args = null;
resp = null;
});
}
}
//make sure the function has the same property as an object created from this class.
fn.methodName = methodName;
fn.url = url;
fn.user = user;
fn.password=pass;
fn.toMulticall = this.toMulticall;
fn.toString = this.toString;
fn.setAuthentication=this.setAuthentication;
fn.constructor = this.constructor;
return fn;
}
/**
Returns the method representation for system.multicall.
@param All params will be passed to the remote method.
@return An object containing a member methodName and a member params(As required by system.multicall).
*/
publ.toMulticall = function(){
var multiCallable = new Object();
multiCallable.methodName = this.methodName;
var params = [];
for(var i=0;i<arguments.length;i++){
params[i] = arguments[i];
}
multiCallable.params = params;
return multiCallable;
}
/**
Sets username and password for HTTP Authentication.
@param user The user name.
@param pass The password.
*/
publ.setAuthentication = function(user, pass){
this.user = user;
this.password = pass;
}
///The name of the remote method.
publ.methodName;
///The url of the remote service containing the method.
publ.url;
///The user name used for HTTP authorization.
publ.user;
///The password used for HTTP authorization.
publ.password;
})
/**
Creates proxy objects which resemble the remote service.
Method calls of this proxy will result in calls to the service.
*/
mod.ServiceProxy=Class("ServiceProxy", function(publ){
/**
Initializes a new ServerProxy.
The arguments are interpreted as shown in the examples:
ServerProxy("url")
ServerProxy("url", ["methodName1",...])
ServerProxy("url", ["methodName1",...], "user", "pass")
ServerProxy("url", "user", "pass")
@param url The url of the service.
@param methodNames=[] Array of names of methods that can be called on the server.
If no methods are given then introspection is used to get the methodnames from the server.
@param user=null The user name to use for HTTP authentication.
@param pass=null The password to use for HTTP authentication.
*/
publ.init = function(url, methodNames, user, pass){
if(methodNames instanceof Array){
if(methodNames.length > 0){
var tryIntrospection=false;
}else{
var tryIntrospection=true;
}
}else{
pass=user;
user=methodNames;
methodNames=[];
var tryIntrospection=true;
}
this._url = url;
this._user = user;
this._password = pass;
this._addMethodNames(methodNames);
if(tryIntrospection){
try{//it's ok if it fails.
this._introspect();
}catch(e){
}
}
}
/**
Adds new XMLRPCMethods to the proxy server which can then be invoked.
@param methodNames Array of names of methods that can be called on the server.
*/
publ._addMethodNames = function(methodNames){
for(var i=0;i<methodNames.length;i++){
var obj = this;
//setup obj.childobj...method
var names = methodNames[i].split(".");
for(var n=0;n<names.length-1;n++){
var name = names[n];
if(obj[name]){
obj = obj[name];
}else{
obj[name] = new Object();
obj = obj[name];
}
}
var name = names[names.length-1];
if(obj[name]){
}else{
var mth = new mod.XMLRPCMethod(this._url, methodNames[i], this._user, this._password);
obj[name] = mth;
this._methods.push(mth);
}
}
}
/**
Sets username and password for HTTP Authentication for all methods of this service.
@param user The user name.
@param pass The password.
*/
publ._setAuthentication = function(user, pass){
this._user = user;
this._password = pass;
for(var i=0;i<this._methods.length;i++){
this._methods[i].setAuthentication(user, pass);
}
}
/**
Initiate XML-RPC introspection to retrieve methodnames from the server
and add them to the server proxy.
*/
publ._introspect = function(){
this._addMethodNames(["system.listMethods","system.methodHelp", "system.methodSignature"]);
var m = this.system.listMethods();
this._addMethodNames(m);
}
///The url of the service to resemble.
publ._url;
///The user used for HTTP authentication.
publ._user;
///The password used for HTTP authentication.
publ._password;
///All methods.
publ._methods=new Array();
})
///@deprecated Use ServiceProxy instead.
mod.ServerProxy= mod.ServiceProxy;
/**
XML-RPC representation of a string.
All '&' and '<' are replaced with the '&' and '<'.
@return A string containing the String's representation in XML.
*/
String.prototype.toXmlRpc = function(){
return "<string>" + this.replace(/&/g, "&").replace(/</g, "<") + "</string>";
}
/**
XML-RPC representation of a number.
@return A string containing the Number's representation in XML.
*/
Number.prototype.toXmlRpc = function(){
if(this == parseInt(this)){
return "<int>" + this + "</int>";
}else if(this == parseFloat(this)){
return "<double>" + this + "</double>";
}else{
return false.toXmlRpc();
}
}
/**
XML-RPC representation of a boolean.
@return A string containing the Boolean's representation in XML.
*/
Boolean.prototype.toXmlRpc = function(){
if(this == true) {
return "<boolean>1</boolean>";
}else{
return "<boolean>0</boolean>";
}
}
/**
XML-RPC representation of a date(iso 8601).
@return A string containing the Date's representation in XML.
*/
Date.prototype.toXmlRpc = function(){
var padd=function(s, p){
s=p+s
return s.substring(s.length - p.length)
}
var y = padd(this.getUTCFullYear(), "0000");
var m = padd(this.getUTCMonth() + 1, "00");
var d = padd(this.getUTCDate(), "00");
var h = padd(this.getUTCHours(), "00");
var min = padd(this.getUTCMinutes(), "00");
var s = padd(this.getUTCSeconds(), "00");
var isodate = y + m + d + "T" + h + ":" + min + ":" + s
return "<dateTime.iso8601>" + isodate + "</dateTime.iso8601>";
}
/**
XML-RPC representation of an array.
Each entry in the array is a value in the XML-RPC.
@return A string containing the Array's representation in XML.
*/
Array.prototype.toXmlRpc = function(){
var retstr = "<array><data>";
for(var i=0;i<this.length;i++){
retstr += "<value>" + mod.marshall(this[i]) + "</value>";
}
return retstr + "</data></array>";
}
mod.test = function(){
print("creating ServiceProxy object using introspection for method construction...\n");
var s = new mod.ServiceProxy("http://localhost/testx.py");
print("%s created\n".format(s));
print("creating and marshalling test data:\n");
var o = [1.234, 5, {a:"Hello & < ", b:new Date()}];
print(mod.marshall(o));
print("\ncalling echo() on remote service...\n");
var r = s.echo(o);
print("service returned data(marshalled again):\n")
print(mod.marshall(r));
}
})
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -