Posts Tagged ‘python ZSI’

Using ZSI

Friday, July 18th, 2008

So I had some task to complete at work which involved swapping out and old XML based API for a new SOAP based API.  I had looked at ZSI in the past, was scared away and didn’t continue.  Of course the WSDL for that one was over 1MB and this one was pretty simple, so what the heck.  I found a simple example of a WSDL so I can explain how to use ZSI.

The WSDL located here is used in this simple example.  Maybe I’ll post a more complex one soonish.

First, you find your WSDL.  If you have ZSI 2.0 installed you need to run the wsdl2py script like so:

wsdl2py –url http://lyricwiki.org/server.php?wsdl

In the directory you’re in (make a new one if you are so inclined) you will notice two files, LyricWiki_services.py, LyricWiki_services_types.py.  We aren’t going to be editing either file, but we will be using them.  Now fire up your favorite editor and plug this in:

from LyricWiki_services import *
 
loc = LyricWikiLocator()
port = loc.getLyricWikiPortType()
 
request = getSongRequest()
 
request.Artist = 'Jack Johnson'
request.Song = 'If I Had Eyes'
 
response = port.getSong(request)
 
print response.Return.Lyrics

Which prints the lyrics! The basic idea here is that you have to build up your request and then send it off. For more advanced APIs you end up having methods that create new objects for you to build up. I’ll see if I can find something more advanced and post it up.