Extending the Python AdWords API client to understand “unbounded”
I’ve been adding some functionality to the sample AdWords API client. After using it for a couple of minutes I found that it does not understand when a list should be returned. In other words, if I call an API method that should return a list, getAdGroupList for example, and only one ad group is returned, it is returned as a single instance, not a list of length one. So now instead of:
for adgroup in service.getAdGroupList(listofAdGroupIds):
print adgroup.name
I have to do some duck typing on each return value:
adgroups = service.getAdGroupList(listofAdGroupIds)
if not hasattr(adgroups, 'sort'):
adgroups = [adgroups]
for adgroup in adgroups:
print adgroup.name
It’s more than doubled the lines of code needed just to call a simple API method. Now, I could just overload the service to turn all returned values into a list. The problem, however, is that some methods aren’t supposed to return a list, updateAdGroup, for example. To make the client understand when a list is needed I have to deconstruct the WSDL. I’ve already been loading the WSDLs to extract API method names so I just need to find where the return value was defined. For the AdWords API, it’s listed pretty far down. Here’s an look at the WSDL where it defines the getAdgroupList response element:
<element name="getAdGroupListResponse">
<complextype>
<sequence>
<element name="getAdGroupListReturn" maxOccurs="unbounded" type="impl:AdGroup"/>
</sequence>
</complextype>
</element>
In the getAdgroupListReturn element you’ll see the maxOccurs value is unbounded. This means the return value should be a list. I’m not sure why, but the SOAPpy module doesn’t seem to understand this. To fix the behavior, I first had to find out where SOAPpy was putting the *methodName*Return element in the data structure. This was pretty much trial and error with a lot of dir() commands until I found the culprit. I’m not going to post the convoluted code here but, if you’re interested, you can look through it yourself. The method name is getPluralMethods.
Ok, so I’ve gotten a list of method names that need to return a list. What now? Well, I created a method named expectsList that takes another method as an argument:
def expectsList(self, fn):
""Decorator that guarantees that the
return value of a function is a list
Args:
fn: function
Returns:
function
"""
def returnList(*args, **kwargs):
out = fn(*args, **kwargs)
#Quack, quack: duck typing
if not hasattr(out, 'reverse'):
if not hasattr(out, 'id'):
#Empty return? Return an empty list
return []
#Single return element? Return it in a list
return [out]
#Otherwise, it must already be a list
return out
return returnList
In the comments, I call this a decorator although it doesn’t technically follow the decorator syntax in the next bit of code where I wrap it around plural API methods:
plurals = self.getPluralMethods(wsdl)
for meth in wsdl.methods.keys():
methFn = getattr(service, meth)
if meth in plurals:
methFn = self.expectsList(methFn)
setattr(self, meth, methFn)
So, as you can see I’m wrapping the methods that expect a list in the expectsList function. This will make sure that all data returned from these methods is in the correct format and it’s been working so far.
Leave a Reply