Skip to main content

A Python client wrapping the Saleforce.com SOAP API

Project description

Introduction
============

This is a reluctant fork of the beatbox project originally authored by Simon
Fell, (his version locked at 0.92) later drastically changed by these guys
https://code.google.com/p/salesforce-beatbox/ (versioned at 20.0).

Renamed to `pyforce` to avoid confusion related to the fractured community
version. (https://github.com/superfell/Beatbox/issues/6) Long story short,
the python client in the fork at version 20.0 is exceptionally useful, so
going back to 0.92 would be a mistake, however the beatbox version at 20.0 is
also no longer maintained (judging by the issues). `pyforce` builds off of
the version available there, integrating bug fixes, and new features.

This module contains 2 versions of the Salesforce.com client:

XMLClient
An xmltramp wrapper that handles the xml fun.
PythonClient
Marshalls the returned objects into proper Python data types. e.g. integer
fields return integers.

Compatibility
=============

`pyforce` supports versions 16.0 through 20.0 of the Salesforce Partner Web
Services API. However, the following API calls have not been implemented at
this time:

* convertLead
* emptyRecycleBin
* invalidateSessions
* logout
* merge
* process
* queryAll
* undelete
* describeSObject
* sendEmail
* describeDataCategoryGroups
* describeDataCategoryGroupStructures

`pyforce` supports python 2.x for values of x >=6. It probably works in 2.4,
but not officially supported.

Basic Usage Examples
====================

Instantiate a Python Salesforce.com client:
>>> svc = pyforce.Client()
>>> svc.login('username', 'passwordTOKEN')

(Note that interacting with Salesforce.com via the API requires the use of a
'security token' which must be appended to the password. See sfdc docs for
details)

The pyforce client allows you to query with sfdc SOQL.

Here's an example of a query for contacts with last name 'Doe':

res = svc.query("SELECT Id, FirstName, LastName FROM Contact WHERE LastName='Doe'")
res[0]
{'LastName': 'Doe', 'type': 'Contact', 'Id': '0037000000eRf6vAAC', 'FirstName': 'John'}
res[0].Id
'0037000000eRf6vAAC'

Add a new Lead:

contact = {
'type': 'Lead',
'LastName': 'Ian',
'FirstName': 'Bentley',
'Company': '10gen'
}
res = svc.create(contact)
if not res[0]['errors']:
contact_id = res[0]['id']
else:
raise Exception('Contact creation failed {0}'.format(res[0]['errors']))

Batches work automatically (though sfdc limits the number to 200 maximum):

contacts = [
{
'type': 'Lead',
'LastName': 'Glick',
'FirstName': 'David',
'Company': 'Individual'
},
{
'type': 'Lead',
'LastName': 'Ian',
'FirstName': 'Bentley',
'Company': '10gen'
}
]
res = svc.create(contacts)

More Examples
=============

The examples folder contains the examples for the xml client. For examples on
how to use the python client see the tests directory.

Some of these other products that were built on top of beatbox can also provide
example of `pyforce` use, though this project may diverge from the beatbox api.

* `Salesforce Base Connector`_
* `Salesforce PFG Adapter`_
* `Salesforce Auth Plugin`_
* `RSVP for Salesforce`_

.. _`Salesforce Base Connector`: http://plone.org/products/salesforcebaseconnector
.. _`Salesforce PFG Adapter`: http://plone.org/products/salesforcepfgadapter
.. _`Salesforce Auth Plugin`: http://plone.org/products/salesforceauthplugin
.. _`RSVP for Salesforce`: http://plone.org/products/collective.salesforce.rsvp


Alternatives
============

David Lanstein has created a `Python Salesforce Toolkit` that is based on the
`suds` SOAP library. That project has not seen any commit since June 2011, so
it is assumed to be abandoned.

.. `Python Salesforce Toolkit`: http://code.google.com/p/salesforce-python-toolkit/

Running Tests
=============

At the fork time, all tests are integration tests that require access to a
Salesforce environment. It is my intent to change these tests to be stub
based unit tests.

From the beatbox documentation:

First, we need to add some custom fields to the Contacts object in your Salesforce instance:

* Login to your Salesforce.com instance
* Browse to Setup --> Customize --> Contacts --> Fields --> "New" button
* Add a Picklist (multi-select) labeled "Favorite Fruit", then add
* Apple
* Orange
* Pear
* Leave default of 3 lines and field name should default to "Favorite_Fruit"
* Add a Number labeled "Favorite Integer", with 18 places, 0 decimal places
* Add a Number labeled "Favorite Float", with 13 places, 5 decimal places

Create a sfconfig file in your python path with the following format::

USERNAME='your salesforce username'
PASSWORD='your salesforce passwordTOKEN'

where TOKEN is your Salesforce API login token.

Add './src' to your PYTHONPATH

Run the tests::

python src/pyforce/tests/test_xmlclient.py
python src/pyforce/tests/test_pythonClient.py


Changelog
=========

1.0 (2013-04-01)
* Rename beatbox to pyforce
* Support embedded dictionaries in python objects submitted to the python
client
* Rename writeStringElement method to writeElement for more accuracy in name

Beatbox forked as Pyforce
-------------------------

20.0 (2010-11-30)
-----------------

* Add 'encryptedstring' to the list of types marshalled as strings. Thanks
sobyone.
[davisagli]

* Update to use version 20.0 of the Salesforce.com partner WSDL by default.
[davisagli]

19.0 (2010-08-23)
-----------------

* Update marshalling of describeGlobal and describeSObjects responses to
include new properties now returned by the API. For backwards
compatibility, we set the types property of the describeGlobal response
to a list of the names of all types (which Salesforce now returns in
separate DescribeGlobalSObjectResult objects).
[davisagli]

* Update to use version 19.0 of the Salesforce.com partner WSDL by default.
Also, use the new login.salesforce.com login endpoint by default.
[davisagli]

16.1 (2010-03-11)
-----------------

* Catch and retry on exceptions from the socket library, in addition to ones
from httplib. This fixes a regression introduced in version 16.0.
[davisagli]


16.0 (2009-11-12)
-----------------

* Don't strip newlines when marshalling the values of textarea fields.
[davisagli]

* Make sure to add a field to fieldsToNull if its Python value is None.
[rhettg, davisagli]

* Fix issue where numbers of type long weren't converted to a string.
[spleeman, davisagli]

* Only catch HTTP exceptions when retrying a connection.
[spleeman, davisagli]


16.0b1 (2009-09-08)
-------------------

* Log beatbox calls at the debug level.
[davisagli]

* Fixed a string exception for compatibility with Python 2.6.
[davisagli]

* Added support for SOSL searches via the search method. Thanks to Alex Tokar
of Web Collective.
[davisagli]

* Added an optional cache for the sObject type descriptions needed for
marshalling query results into Python objects. This can avoid an extra
describeSObjects API call for each query, but means that the information
could become stale if the type metadata is modified in Salesforce.com.
The cache is off by default. Turn it on by passing
cacheTypeDescriptions=True when instantiating a Python client. The cache may
be reset by calling the flushTypeDescriptionsCache method of the Python
client.
[davisagli]

* Support a full SOQL statement as a parameter to the query method of the
Python client. The old 3-part method signature (fields, sObjectType,
conditionalExpression) should continue to work.
[davisagli]

* In the Python client, support relationship queries and other queries that may
return multiple types of objects. Object type descriptions (required for
marshalling field values into the correct Python type) are cached for the
duration of the query after the first time they are used. Thanks to
Melnychuk Taras of Quintagroup.
[davisagli]

* In the Python client, queries now return a list-like QueryRecordSet holding
a sequence of dict-like QueryRecord objects, instead of a dict containing a
list of dicts. This allows for more Pythonic access such as results[0].Id
instead of results['results'][0]['Id']. The old syntax should still work.
Thanks to Melnychuk Taras of Quintagroup.
[davisagli]

* Update to use version 16.0 of the Salesforce.com partner WSDL.
[davisagli]


0.12 (2009-05-13)
-----------------

* Use the default serverUrl value if the passed value evaluates to boolean
False.
[davisagli]

0.11 (2009-05-13)
-----------------

* Access 'created' instead of 'isCreated' in the upsert result. This closes
http://code.google.com/p/salesforce-beatbox/issues/detail?id=4
[davisagli]

10.1 (unreleased)
-----------------

0.10 (2009-05-06)
-----------------

* Added optional serverUrl parameter when creating a Client.
[davisagli]

pre 0.9.1.1
-----------

* ancient history

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

pyforce-1.0.tar.gz (32.5 kB view details)

Uploaded Source

File details

Details for the file pyforce-1.0.tar.gz.

File metadata

  • Download URL: pyforce-1.0.tar.gz
  • Upload date:
  • Size: 32.5 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No

File hashes

Hashes for pyforce-1.0.tar.gz
Algorithm Hash digest
SHA256 2cf3365445fe8b0e12fa78004276845b1c01d03f404f302cb76da6be2cb04e08
MD5 47797f4be541dc52697f956af849b310
BLAKE2b-256 0a3472b99e784b9e2951fb25da981c01f6be2e4c6c55e9d61b832eb69fd55915

See more details on using hashes here.

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page