After generating the Site, we need to actually provide some implementation of the service's advertised functionality. Recall that in the example WSDL we defined one service, called MathService. The generation step created a service skeleton stub named MathServiceWSRF Now we will create a new class that subclasses this generated service skeleton stub and overrides all the methods prefixed with the special 4-character sequence "wsa_". One of these methods should be present for each "operation" defined in the WSDL file. Put another way, these methods represent the WSDL operations of the service. For example, "wsa_add" represents the service's "add" operation.
To understand the code you can see in these methods, we must first understand something about Web Service request and response objects.
request: When a "wsa_"-prefixed method of the generated service skeleton stub is invoked (e.g. MathServiceWSRF.wsa_add() ), it creates a new request instance and returns it. This request represents the XML message that was passed in the ps parameter ("ps" stands for "parsed SOAP", i.e. a parsed web services message). This Python object will have an attribute called typecode which is used to serialize it back into XML.
response: When a "wsa_"-prefixed method of the generated service skeleton stub returns, it creates a new response instance. This object also contains a typecode attribute, which is used to serialize it into XML.
The important thing to get out of the above is that both requests and responses are objects with the attribute "typecode". But in Python everything is an object, so they could be subclasses of integers or strings or floating-point numbers that just have an extra "typecode" attribute tagging along for the ride. In fact, with this simple service this is exactly the case, and understanding that helps understand some details of the code.
SimpleMathService.py: Save this file somewhere in your PYTHONPATH
from generated.SimpleMath.services.SimpleMath.MathService import MathServiceWSRF class StatefulWebService(MathServiceWSRF): def __init__(self, *args, **kw): MathServiceWSRF.__init__(self, *args, **kw) self.value = 0def wsa_add(self, ps, address, **kw):
request,response = MathServiceWSRF.wsa_add(self, ps, address, **kw)
self.value += request
return request,response def wsa_subtract(self, ps, address, **kw): request,response = MathServiceWSRF.wsa_subtract(self, ps, address, **kw) self.value -= request return request,response def wsa_getValueRP(self, ps, address, **kw): request,response = MathServiceWSRF.wsa_getValueRP(self, ps, address, **kw) return request,response.__class__(self.value)
![]()