Ronco
Ronco

Reputation: 109

Access Salesforce data using python

Whats the most simple way to access SF data using python?

I need it for read only purposes.

I have tried using BeatBox but it seems to be not compatible with 3.3.

Upvotes: 2

Views: 2270

Answers (2)

user2571090
user2571090

Reputation: 31

the salesforce api is built using a W3C standard called SOAP, so rather than a dedicated salesforce library, you might try piping your WSDL files into a generic SOAP library. SUDS is a mature, actively maintained library, and i've just done a successful salesforce metadata api call with it, and that looks something like

from suds.client import Client


_SF_PRODUCT_VERSION = (0, 0, 0)
USER_AGENT_STR = '/'.join(['Salesforce',
                           _SF_PRODUCT_NAME,
                           '.'.join(str(x) for x in _SF_PRODUCT_VERSION)])

def login(username, password, token):
        WSDL_URL = 'file://' + path.abspath(_ENTERPRISE_WSDL_FILE)
    suds_client = Client(WSDL_URL)
    suds_client.set_options(headers = {
            'User-Agent': USER_AGENT_STR,
            })
    login_result = suds_client.service.login(username, password+token)
    return login_result

login_result = login(SF_USERNAME, SF_PASSWORD, SF_TOKEN)
suds_client = Client(url=METADATA_WSDL_URL, location=login_result.metadataServerUrl)
session_header = suds_client.factory.create('SessionHeader')
session_header.sessionId = login_result['sessionId']
suds_client.set_options(
    headers = {
        'User-Agent': USER_AGENT_STR,
        },
    soapheaders = {
        'SessionHeader': session_header,
        })
describe_metadata_result = suds_client.service.describeMetadata(29.0)
print "describe metadata result"
print describe_metadata_result

(some names have been changed to protect the innocent.)

so it's not as simple as it could be, but with a little extra time to build on top of this very mature SOAP library, it's probably going to be more robust than some dedicated salesforce libraries. for one thing, adding

logging.basicConfig(level=logging.INFO)
logging.getLogger('suds.client').setLevel(logging.DEBUG)

before an api call will print the xml requests and responses as they get sent over the wire, which can be really useful.

there is a dedicated library called the python-salesforce-toolkit built on SUDS, but neither it nor the version of SUDS that it's built on are maintained anymore.

Upvotes: 3

John Sheehan
John Sheehan

Reputation: 78124

Two libraries to try:

RestForce

simple-salesforce

Upvotes: 6

Related Questions