A WebService meta object that can serve requests for SOAP, XML-RPC, JSON-RPC, JSON-MYNA, and Ext.Direct
This object can be used directly to implement web rpc services or as a wrapper around existing objects. For SOAP, this attempts to provide the simplest usable implementation. For the simpler XML-RPC JSON-RPC, and Ext.Direct specifications, this object aims to provide a complete implementation.
This is a Myna specific protocol that aims to be the simplest implementation of WebService. Requests are made via GET or POST. A parameter “method” must be passed to indicate which function on this WebService to execute. All other GET or POST parameters are assumed to be named function parameters. The response body will be a JSON encoding of the function result without any extra metadata.
| Myna. WebService | A WebService meta object that can serve requests for SOAP, XML-RPC, JSON-RPC, JSON-MYNA, and Ext.Direct |
| Functions | |
| WebService | Constructor function for WebService class |
| generateQueryType | Static function that takes a columns definition and returns a type that represents the Myna.Query.result type for the supplied columns that is suitible for use in the “returns” section of a web service function definition |
| handleRequest | Parses a request and returns the appropriate data; |
| getAuthUser | Attempts to find and return the Myna.Permissions.User record for the user indicated in <WebService.authUserId> |
| setAuthUserId | sets <WebService.authUserId> and calls $cookie.setAuthUserId |
| clearAuthUserId | sets WebService.authUserId to null and calls $cookie.clearAuthUserId |
| executeFunctionHandler | INTERNAL handles the execution of web service function. |
| printHelp | prints an HTML response to the requester with documentation of this WebService |
| printWsdl | prints a SOAP WSDL document representing this web service |
| printExtApi | prints a JSON Ext.Direct API representing this web service. |
| executeJsonRpcPost | parses the supplied JSON-RPC request data, executes that requested funtion, and prints a JSON-RPC response text |
| executeJsonMyna | parses the supplied JSON-MYNA request data, executes that requested funtion, and prints a JSON response text |
| executeXmlRpc | parses the supplied XML-RPC request xml, executes that requested funtion, and prints a XML-RPC response text |
| executeSoap | parses the supplied SOAP request XML, executes that requested funtion, and prints a SOAP response envelope |
| executeExtRoute | parses the supplied EXT-ROUTE requests, executes that requested functions, and prints the appropriate response |
Myna.WebService = function( spec )
Constructor function for WebService class
Web services are defined by creating a file with a “.ws” extension containing an object literal as defined in “Spec Definition” below. This file will be handled internally like this:
new Myna.WebService(<.ws file contents>).handleRequest($req)
| spec | object that describes a web service, or a MynaPath to a file that contains the WebService spec, or a Myna.File object that points to a file that contains the WebService spec. See below for spec object definition |
| name | A name for this set of services. Like a class name |
| desc | A string describing this service set |
| authFunction | Optional default null Function that handles authentication before each function request. This function will be called with a single parameter, an object with these properties: |
| beforeHandler | Optional default null A function to execute after authFunction but before processing the function handler. This function is called with three parameters: |
| afterHandler | Optional default null A function to execute after processing the function handler. This function is called with four parameters: |
| functions | An object where each property is a function name and the value is an object representing a function definition as described below |
| desc | Optional default null A string describing this function |
| params | An array of Parameter Definitions. See below |
| handler | Function to execute. |
| scope | Optional default: WebService instance Object to use as “this” when executing the handler. |
| returns | A representation of the type of data to return. If this is a string, it should be one of these type names: “string,numeric,date,date_time”. If this is an array object, then the first item should be either a string type name, another array, or an object. If this is an object, each property should be set equal to either a string type name, another object, or an array. |
| name | name of the parameter |
| type | Type of the parameter. Currently, the only available types are “string,numeric,date,date_time” |
| desc | Optional default null description of the parameter Returns: Reference to WebService instance |
//MyService.ws
{
name:"MyService",
desc:"A thing that does stuff",
myFunction:function(arg1,arg2){
// arbitrary functions and properties can be defined on the spec object.
// in this case "myFunction" can be accessed as "this.myFunction" in
// authFunction, beforeHandler, afterHandler or in the handler function
// requested
},
authFunction:function(authData){
// authData contans:
// * username - supplied username
// * password - supplied password
// * user - <Myna.Permissions.User> object
// associated with this cookie. To
// associate a user with this call
// <$cookie.setAuthUserId>
// * functionName - name of function called
// * functionDef - reference to the function
// definition called
// returning true allows the cal, and false requests a login. Generally
// you want to return false if the username or password is invalid, and
// throw an exception if
return true;
},
beforeHandler:function(name,def,params){
// name - the name of the function handler
// def - a reference to the function definition
// params - an object of the function parameters torequest.
//
// This is called after authFunction but before the handler function.
// This function can manipulate the "params" object before the handler is
// called, or can throw an error to abort processing
},
afterHandler:function(name,def,params,result){
// name - the name of the function handler
// def - a reference to the function definition
// params - an object of the function parameters to the request.
// result - result of the handler call
//
// This is called after the handler function but before out processing.
// This is generally used for logging, or manipulating the result
},
functions:{
echo:{
desc:"returns your parameters as an html table",
returns:"string",
params:[{
name:"arg_string",
type:"string",
desc:"A string params"
},{
name:"arg_number",
type:"numeric",
desc:"A numeric param"
},{
name:"arg_date",
type:"date",
desc:"A date param"
}],
handler:function(params){
return Myna.dump(params)
}
},
get_spec:{
desc:"returns the spec object as JSON string",
returns:"string",
params:[],
handler:function(){
//you can access the spec as "this"
return this.toJson()
}
},
get_bob:{
desc:"Always returns bob. Example of a complex return type",
returns:{
name:"string",
age:"numeric",
children:[{
name:"string",
age:"numeric"
}],
favorite_colors:["string"]
},
params:[],
handler:function(){
return {
name:"Bob Dobb",
age:"44",
children:[{
name:"julie",
age:9
},{
name:"sam",
age:13
}],
favorite_colors:["yellow","green","blue"]
}
}
}
}
}
Myna.WebService.generateQueryType=function( columns )
Static function that takes a columns definition and returns a type that represents the Myna.Query.result type for the supplied columns that is suitible for use in the “returns” section of a web service function definition
| columns | An array of objects in the form of [{colname:type}] that represents the columns in the returned query. “Type” should be one of string, numeric, date, date_time |
Myna.WebService.prototype.handleRequest=function()
Parses a request and returns the appropriate data;
This function examines $req.data for possible rpc calls.
//creates a global MyServiceAPI variable containing the Ext.Direct API
<script src="http://myhost.com/myservice.ws?ext-api&scriptvar=MyServiceAPI"></script>
Myna.WebService.prototype.getAuthUser=function()
Attempts to find and return the Myna.Permissions.User record for the user indicated in <WebService.authUserId>
Myna.WebService.prototype.setAuthUserId=function( user_id )
sets <WebService.authUserId> and calls $cookie.setAuthUserId
| user_id | user_id of user to register |
Myna.WebService.prototype.clearAuthUserId=function()
sets WebService.authUserId to null and calls $cookie.clearAuthUserId
| user_id | user_id of user to register |
Myna.WebService.prototype.executeFunctionHandler=function( functionName, functionDef, paramArray )
INTERNAL handles the execution of web service function. Used by service specific function handlers
| functionName | name of method being executed |
| functionDef | reference to the function definition in the spec |
| paramArray | array of parameters to the method |
Myna.WebService.prototype.printHelp=function()
prints an HTML response to the requester with documentation of this WebService
Myna.WebService.prototype.printWsdl=function()
prints a SOAP WSDL document representing this web service
Myna.WebService.prototype.printExtApi=function( req )
prints a JSON Ext.Direct API representing this web service.
if $req.data.ns is defined, this namespace is will be defined in the API
if $req.data.scriptvar is defined, a JavaScript response is generated with the Ext.Direct API set equal to the value of $req.data.scriptvar. If $req.data.ns is also defined, then scriptvar will be created in that namespace
if $req.data.callback is defined, a JavaScript response is generated that calls callback function with the Ext.Direct API as the only parameter
// adds a Ext.Direct provider via script include with callback
<script type="text/javascript" src="myservice.ws?ext-api&callback=Ext.Direct.addProvider"></script>
// adds a Ext.Direct provider via AJAX callback
<script>
Ext.Ajax.request({
url:"http://myhost.com/myservice.ws?ext-api",
success:function(response){
Ext.Direct.addProvider(Ext.decode(response.responseText));
}
})
MyService.someFunction(arg1, arg1,function(response){
alert(response);
})
</script>
// adds a Ext.Direct provider in the MyNS namespace via AJAX callback
<script>
Ext.Ajax.request({
url:"http://myhost.com/myservice.ws?ext-api",
success:function(response){
Ext.Direct.addProvider(Ext.decode(response.responseText));
}
})
MyService.someFunction(arg1, arg1,function(response){
alert(response);
})
</script>
// creates a global MyServiceAPI variable containing the Ext.Direct API
// via script include
<script src="http://myhost.com/myservice.ws?ext-api&scriptvar=MyServiceAPI"></script>
<script>
Ext.Direct.addProvider(MyServiceAPI);
MyService.someFunction(arg1, arg1,function(response){
alert(response);
})
</script>
// creates a MyServiceAPI variable in the MyNS namespace containing the
// Ext.Direct API via script include
<script src="http://myhost.com/myservice.ws?ext-api&ns=MyNS&scriptvar=MyServiceAPI"></script>
<script>
Ext.Direct.addProvider(MyNS.MyServiceAPI);
MyNS.MyService.someFunction(arg1, arg1,function(response){
alert(response);
})
</script>
Myna.WebService.prototype.executeJsonRpcPost=function( dataString )
parses the supplied JSON-RPC request data, executes that requested funtion, and prints a JSON-RPC response text
| dataString | String object containing the JSON-RPC request |
Myna.WebService.prototype.executeJsonMyna=function( data )
parses the supplied JSON-MYNA request data, executes that requested funtion, and prints a JSON response text
| dataString | String object containing the JSON-RPC request |
Myna.WebService.prototype.executeXmlRpc=function( xml )
parses the supplied XML-RPC request xml, executes that requested funtion, and prints a XML-RPC response text
| xml | XML object containing the XML-RPC request |
Constructor function for WebService class
Myna.WebService = function( spec )
Static function that takes a columns definition and returns a type that represents the Myna.Query.result type for the supplied columns that is suitible for use in the “returns” section of a web service function definition
Myna.WebService.generateQueryType=function( columns )
Parses a request and returns the appropriate data;
Myna.WebService.prototype.handleRequest=function()
Attempts to find and return the Myna.Permissions.User record for the user indicated in WebService.authUserId
Myna.WebService.prototype.getAuthUser=function()
sets WebService.authUserId and calls $cookie.setAuthUserId
Myna.WebService.prototype.setAuthUserId=function( user_id )
sets a cookie that contains the supplied user_id and a timestamp
setAuthUserId:function( user_id )
sets WebService.authUserId to null and calls $cookie.clearAuthUserId
Myna.WebService.prototype.clearAuthUserId=function()
Clears the “myna_auth_cookie”.
clearAuthUserId:function( name )
INTERNAL handles the execution of web service function.
Myna.WebService.prototype.executeFunctionHandler=function( functionName, functionDef, paramArray )
prints an HTML response to the requester with documentation of this WebService
Myna.WebService.prototype.printHelp=function()
prints a SOAP WSDL document representing this web service
Myna.WebService.prototype.printWsdl=function()
prints a JSON Ext.Direct API representing this web service.
Myna.WebService.prototype.printExtApi=function( req )
parses the supplied JSON-RPC request data, executes that requested funtion, and prints a JSON-RPC response text
Myna.WebService.prototype.executeJsonRpcPost=function( dataString )
parses the supplied JSON-MYNA request data, executes that requested funtion, and prints a JSON response text
Myna.WebService.prototype.executeJsonMyna=function( data )
parses the supplied XML-RPC request xml, executes that requested funtion, and prints a XML-RPC response text
Myna.WebService.prototype.executeXmlRpc=function( xml )
parses the supplied SOAP request XML, executes that requested funtion, and prints a SOAP response envelope
Myna.WebService.prototype.executeSoap=function( xml )
parses the supplied EXT-ROUTE requests, executes that requested functions, and prints the appropriate response
Myna.WebService.prototype.executeExtRoute=function()