Fork me on GitHub

Beatbox - Access the Salesforce.com API from Python

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, the main parts of the 20.0 API is currently supported. 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])
Updating your chatter status
sf = beatbox._tPartnerNS
svc = beatbox.Client()
loginResult = svc.login(username, password)
user = { 'type' : 'User',
		 'id'   : str(loginResult[sf.userId]),
		 'currentStatus' : newStatus }
r = svc.update(user)
if (str(r[sf.success]) == 'false'):
	print "error updating status:" + str(r[sf.errors][sf.statusCode]) + ":" + str(rr[sf.errors][sf.message])
else:
	print "success!"

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 samples, demo.py, export.py and soql2atom.py. I've only run it on Python 2.6 on a Mac so far, but I think it should be good for any recent version of 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.

Beatbox is available on Github.