4.9. Sample Client using an EndpointReference

We will try out our service first with a simple client that creates a new resource and performs a couple operations on it. Starting from the client.py that was generated by wsdl2web.py all that's needed are a few rpc calls to demonstrate WSRF resource's behavior.


#!/usr/bin/env python
############################################################################
# Automatically generated by wsdl2web.py
# See LBNLCopyright for copyright notice!
###########################################################################
from twisted.python import log
from twisted.internet import reactor

import ZSI
from pyGridWare.utility.scripts.client import GetBasicOptParser, GetPortKWArgs, SetUp
from generated.MathService.stubs import MathService as CLIENT


def main(**kw):
    locator = CLIENT.MathServiceLocator()
    port = locator.getMathPortType(**kw) (1)

    msg = port.createResource(CLIENT.CreateInputMessage()) (2)
    iport = locator.getMathPortType(endPointReference=msg.EndpointReference, **kw) (3)
    for i in range(10):
        iport.add(CLIENT.AddInputMessage(i)) (4)

    msg = iport.subtract(CLIENT.SubtractInputMessage(10))

    msg = iport.getValueRP(CLIENT.GetValueRPInputMessage())

    # Factory METHOD Just guessing here
    #response = port.create(CLIENT.CreateRequest())
    #kw['endPointReference'] = response._EndpointReference
    #iport = locator.getMathPortType(**kw)
    reactor.stop()
    print "MSG: ", msg
    print "CORRECT: ", msg == sum(range(10)) - 10


if __name__ == '__main__':
    op = GetBasicOptParser()
    (options, args) = op.parse_args()
    SetUp(options)
    kw = GetPortKWArgs(options)
    reactor.callWhenRunning(main, **kw)
    reactor.run()

Note

Copy & Paste example..

(1)
Here we obtain a reference to a MathPortType instance.
(2)
Once we have the MathPortType, we use it to invoke the createResource operation. This operation returns an endpoint reference, inside a CreateResourceResponse object. This endpoint reference includes both the instance service's URI and the new resource's identifier. In the next client we will take a peek inside the endpoint reference.
(3)
Using the instance EPR, we obtain a new MathPortType which will now refer to the instance service.
(4)
We now use the MathPortType to invoke add, subtract, and getValueRP.

run the client:


./client.py -u http://127.0.0.1:9080/wsrf/services/MathService -d 0

If all goes well, you should see the following:


MSG:  35
CORRECT:  True

If you run it again, you will get the exact same result. This is because we are creating a new resource every time we run the client.


MSG:  35
CORRECT:  True