Scio: client code generator

Generating client modules from WSDL files

Use the command-line script scio_generate_client to generate client code from a WSDL file:

$ scio_generate_client path/to/service.wsdl > service_client.py

Generating client code from a dynamic client

It’s possible to directly generate a static client class from a dynamic client class (in fact, this is how all of Scio’s code generation works internally).

>>> import urllib2
>>> from scio import client, gen
>>> code = gen.gen(client.Client(
...     urllib2.urlopen('http://lyricwiki.org/server.php?wsdl')))
>>> code[:100]
u'#\n# WARNING: this module is autogenerated. Do not edit this file!\n#\nfrom scio import client, static\n'

To use the generated client code directly, exec it into a namespace.

>>> ns = {}
>>> exec code in ns
>>> ns['Client']
<class 'Client'>
>>> lyrics = ns['Client']()
>>> artist, albums = lyrics.service.getArtist('Wilco')
>>> artist
u'Wilco'

There’s little to be gained by executing the generated code directly, though. In practice, it is more useful to output the generated code into a module and import and use that module as normal.

>>> with open('lyrics.py', 'w') as fh:
...    fh.write(code)
...
>>> import lyrics
>>> lyrics.Client
<class 'lyrics.Client'>

Subclassing static client classes

Generated client classes are normal Python class and can be subclassed and tweaked in all of the normal ways. See Scio: custom client subclass example for a Scio-specific example of subclassing: the same methods used there will work with a generated client.

API Reference

class scio.static.Client(transport=None)

Bases: scio.client.Client

Base class for statically-generated SOAP clients

Statically-generated SOAP client classes are all subclasses of this class.

Parameters:transport – The transport mechanism for communicating with target services. Default: urlopen().
service

Service container. Each service method defined in the WSDL file from which this client was generated will be represented as a callable attribute in this container.

type

Type container. Each type defined in the WSDL file from which this client was generated will be represented as an attribute in this container.

envelope(request)

Given an InputMessage, wrap in in a SOAP envelope and produce an Element.

handle_error(method, err)

Handle an exception raised by a method call that may be a SOAP Fault.

handle_response(method, response)

Handle a seemingly successful response.

inputClass

alias of InputMessage

methodCallClass

alias of MethodCall

methodClass

alias of Method

outputClass

alias of OutputMessage

parse_response(method, response)

Parse the response xml and return the soap body and header.

raise_if_fault(method, body)

Raise a SOAP Fault if one is found in the response body.

classmethod register(name, type_)

Register a type under the given name

send(method, request)

Send the SOAP request for the given method. Don’t call this directly (use the methods attached to a client’s service attribute instead), but do override it in a subclass to mock a service or change how a request is sent.

class scio.static.TypeRegistry

Registry of types and references

scio.gen.gen(client, template='/home/jpellerin/code/scio/.tox/docs/lib/python2.6/site-packages/scio/static_client.tpl')

Generate code for a scio.client.Client class.

Parameters:
  • client – A scio.client.Client class generated from a WSDL file.
  • template – The jinja2 template to use for code generation.
Returns:

Code string.

Example

Here is a code module generated from the lyricswiki WSDL file.