Simon Fell > Its just code > Beatbox

Sunday, February 19, 2006

I've been tinkering with Python for a while, but have wanted to try my hand at something more complex that hello world with it, I ended up building Beatbox, a simple to use, minimal dependency library for accessing the Salesforce.com API. (Given the pace of the branding train at Salesforce, I've decided to stop trying to put force or sforce in any of my sforce API tools).

Rather than some thick data binding layer, most of the stuff you pass into the library is dictionaries, and most of the data you get back is in the form of a xmltramp representation of the relevant result part of the soap response (xmltramp rocks BTW) It includes all the usual sforce perf recommendations, HTTP/1.1 persistent connections, gzip compression on request/responses, batch operations, all of the 7.0 API is currently supported except for leadConvert. The login redirect and sessionId header handling is all done for you under the covers, there's still a few loose ends mainly around handling base64, and timezones on datetimes, but other than that you're good to go.

demo.py in the download has samples of calling all the methods, for example to login and then call describeGlobal and print out all the types available is simply

sf = beatbox._tPartnerNS
svc = beatbox.Client()
svc.login(username, password)
dg = svc.describeGlobal()
for t in dg[sf.types:]:
    print str(t)
making a call to query and printing the results,
sf = beatbox._tPartnerNS
svc = beatbox.Client()
svc.login(username, password)
qr = svc.query("select Id, Name from Account")
print "query size = " + str(qr[sf.size])
for rec in qr[sf.records:]:
    print str(rec[2]) + " : " + str(rec[3])
creating a new account
sf = beatbox._tPartnerNS
svc = beatbox.Client()
svc.login(username, password)
a = { 'type': 'Account',
    'Name': 'New Account',
    'Website': 'http://www.pocketsoap.com/' }
sr = svc.create(a)
if str(sr[sf.success]) == 'true':
    print "id " + str(sr[sf.id])

The download includes a copy of xmltramp (the only dependency) that has a minor bug fix in it, the beatbox.py library itself and the demo.py sample. I've only run it on python 2.4.2 on Windows so far, but I think it should be good for any 2.x python. Feedback welcome, especially from those more versed in Python than myself. I'm releasing the code under the GPL license, which matches the license for xmltramp.

Share and enjoy. beatbox_08.zip