Scio: Client

Client API

class scio.Client(wsdl_fp, transport=None, service_class=None, type_class=None, reduce_callback=None)

WSDL client container class.

Given an open wsdl file-like object, instantiating a Client builds SOAP types and service calls using a Factory, and provides access to them via the instance’s type and service attributes.

Parameters:
  • wsdl_fp – A file-like object containing the wsdl to use to construct the client types and methods.
  • transport – The transport mechanism for communicating with target services. Default: urlopen().
  • service_class – A class that will contain services. An instance of this class will be available as client.service.
  • type_class – A class that will contain types. An instance of this class will be available as client.type.
  • reduce_callback

    REQUIRED TO SUPPORT PICKLING! If you want to be able to pickle and unpickle scio types, you must supply a function that can, given a class name, return a bare instance of the type. This function must be defined at the top level of a module and must be marked as __safe_for_unpickle__. The reduce callback must have a signature compatible with the typical definition:

    def reviver(classname, proto=object, args=()):
        return proto.__new__(getattr(
            some_client.type, classname), *args)
    

    The proto parameter will only be used for a number of basic types, including int and arrays (list).

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.

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.

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.

SOAP Types

Constructing SOAP types

>>> import scio
>>> from lxml import etree
>>> from urllib2 import urlopen
>>> wsdl = open('tests/support/adwords_trafficestimatorservice.wsdl', 'r')
>>> adwords = scio.Client(wsdl)

When calling SOAP methods or constructing SOAP type instances, parameters may be passed as keyword arguments.

>>> cpg = adwords.type.CampaignRequest(
...     networkTargeting=adwords.type.NetworkTarget(
...     networkTypes=["GoogleSearch"]))

Attributes of SOAP type instances may be set directly, even deeply. Intermediary types will be auto-vivified. List types may be specified as lists, or built up into lists by multiple assignments.

>>> cpg.geoTargeting.cityTargets.cities = 'Houston'
>>> cpg.geoTargeting.cityTargets.cities = 'Ontario'
>>> cpg.geoTargeting.countryTargets.countries = ['USA', 'Canada']

The repr() of a SOAP type instance is useful.

>>> cpg
CampaignRequest(geoTargeting=GeoTarget(cityTargets=CityTargets(cities=[u'Houston', u'Ontario']), countryTargets=CountryTargets(countries=[u'USA', u'Canada'])), networkTargeting=NetworkTarget(networkTypes=[NetworkType.GoogleSearch]))

Serializing a SOAP type instance into xml is easy. Outside of the normal call context, you may have to provide an xml tag name.

>>> etree.tostring(cpg.toxml(tag='CampaignRequest'))
'<CampaignRequest xmlns="https://adwords.google.com/api/adwords/v13"><geoTargeting><cityTargets><cities>Houston</cities><cities>Ontario</cities></cityTargets><countryTargets><countries>USA</countries><countries>Canada</countries></countryTargets></geoTargeting><networkTargeting><networkTypes>GoogleSearch</networkTypes></networkTargeting></CampaignRequest>'

You can introspect the available types.

>>> dir(adwords.type)
['AdGroupEstimate', 'AdGroupRequest', 'ApiError', 'ApiException', 'CampaignEstimate', 'CampaignRequest', 'Circle', 'CityTargets', 'CountryTargets', 'GeoTarget', 'KeywordEstimate', 'KeywordRequest', 'KeywordTraffic', 'KeywordTrafficRequest', 'KeywordType', 'LanguageTarget', 'MetroTargets', 'NetworkTarget', 'NetworkType', 'ProximityTargets', 'RegionTargets', '__class__', '__delattr__', '__dict__', '__doc__', '__format__', '__getattribute__', '__hash__', '__init__', '__module__', '__new__', '__reduce__', '__reduce_ex__', '__repr__', '__setattr__', '__sizeof__', '__str__', '__subclasshook__', '__weakref__', '_client', 'applicationToken', 'checkKeywordTraffic', 'checkKeywordTrafficResponse', 'clientCustomerId', 'clientEmail', 'developerToken', 'email', 'estimateAdGroupList', 'estimateAdGroupListResponse', 'estimateCampaignList', 'estimateCampaignListResponse', 'estimateKeywordList', 'estimateKeywordListResponse', 'operations', 'password', 'requestId', 'responseTime', 'units', 'useragent']

Method Calls

The service attribute of the client instance contains all of the method calls defined in the wsdl file. To call a SOAP method, call the appropriate method under service with the proper arguments.

>>> url = 'http://lyricwiki.org/server.php?wsdl'
>>> lyrics = scio.Client(urlopen(url))

Methods return one or more SOAP type instances.

>>> artist, albums = lyrics.service.getArtist('Wilco')
>>> artist
u'Wilco'

That looks like a plain unicode string, but it’s really a SOAP type.

>>> type(artist)
<class 'scio.client.StringType'>

SOAP types may be complex, and include lists, other types, etc.

>>> am = albums[0]
>>> am.album
u'A.M.'
>>> am.songs
[u'I Must Be High', u'Casino Queen', u'Box Full Of Letters', u"Shouldn't Be Ashamed", u'Pick Up The Change', u'I Thought I Held You', u"That's Not The Issue", u"It's Just That Simple", u"Should've Been In Love", u'Passenger Side', u'Dash 7', u'Blue Eyed Soul', u'Too Far Apart']

You can introspect the available services.

>>> dir(lyrics.service)
['__class__', '__delattr__', '__dict__', '__doc__', '__format__', '__getattribute__', '__hash__', '__init__', '__module__', '__new__', '__reduce__', '__reduce_ex__', '__repr__', '__setattr__', '__sizeof__', '__str__', '__subclasshook__', '__weakref__', '_client', '_methods', 'checkSongExists', 'getAlbum', 'getArtist', 'getHometown', 'getSOTD', 'getSong', 'getSongResult', 'method_class', 'postAlbum', 'postArtist', 'postSong', 'postSong_flags', 'searchAlbums', 'searchArtists', 'searchSongs']

Table Of Contents

Previous topic

Scio: Quickstart

Next topic

Scio: API and Internals

This Page