Skip to content

Commit

Permalink
Add Bulk API Support to Simple Salesforce
Browse files Browse the repository at this point in the history
Adds bulk API support as mentioned in simple-salesforce#112
Closes simple-salesforce#143
By colemanja91 <colemanja91@gmail.com>
  • Loading branch information
colemanja91 authored and nickcatal committed Jun 22, 2017
1 parent 3da5304 commit e9433e6
Show file tree
Hide file tree
Showing 4 changed files with 402 additions and 4 deletions.
54 changes: 54 additions & 0 deletions README.rst
Expand Up @@ -230,6 +230,60 @@ To retrieve a list of top level description of instance metadata, user:
print x["label"]
Using Bulk
----------

You can use this library to access Bulk API functions.

Create new records:

.. code-block:: python
data = [{'LastName':'Smith','Email':'example@example.com'}, {'LastName':'Jones','Email':'test@test.com'}]
sf.bulk.Contact.insert(data)
Update existing records:

.. code-block:: python
data = [{'Id': '0000000000AAAAA', 'Email': 'examplenew@example.com'}, {'Id': '0000000000BBBBB', 'Email': 'testnew@test.com'}]
sf.bulk.Contact.update(data)
Upsert records:

.. code-block:: python
data = [{'Id': '0000000000AAAAA', 'Email': 'examplenew2@example.com'}, {'Id': '', 'Email': 'foo@foo.com'}]
sf.bulk.Contact.upsert(data, 'Id')
Query records:

.. code-block:: python
query = 'SELECT Id, Name FROM Account LIMIT 10'
sf.bulk.Account.query(query)
Delete records (soft deletion):

.. code-block:: python
data = [{'Id': '0000000000AAAAA'}]
sf.bulk.Contact.delete(data)
Hard deletion:

.. code-block:: python
data = [{'Id': '0000000000BBBBB'}]
sf.bulk.Contact.hard_delete(data)
Using Apex
----------

Expand Down
1 change: 1 addition & 0 deletions simple_salesforce/__init__.py
Expand Up @@ -5,6 +5,7 @@
Salesforce,
SalesforceAPI,
SFType,
SFBulkHandler,
SalesforceError,
SalesforceMoreThanOneRecord,
SalesforceExpiredSession,
Expand Down
15 changes: 11 additions & 4 deletions simple_salesforce/api.py
Expand Up @@ -17,6 +17,7 @@
from urllib.parse import urlparse, urljoin
from simple_salesforce.login import SalesforceLogin
from simple_salesforce.util import date_to_iso8601, SalesforceError
from simple_salesforce.bulk import SFBulkHandler

try:
from collections import OrderedDict
Expand Down Expand Up @@ -164,6 +165,9 @@ def __init__(
version=self.sf_version))
self.apex_url = ('https://{instance}/services/apexrest/'
.format(instance=self.sf_instance))
self.bulk_url = ('https://{instance}/services/async/{version}/'
.format(instance=self.sf_instance,
version=self.sf_version))

def describe(self):
"""Describes all available objects
Expand Down Expand Up @@ -201,6 +205,11 @@ def __getattr__(self, name):
if name.startswith('__'):
return super(Salesforce, self).__getattr__(name)

if name == 'bulk':
# Deal with bulk API functions
return SFBulkHandler(self.session_id, self.bulk_url, self.proxies,
self.session)

return SFType(
name, self.session_id, self.sf_instance, sf_version=self.sf_version,
proxies=self.proxies, session=self.session)
Expand Down Expand Up @@ -326,7 +335,7 @@ def query(self, query, **kwargs):
Arguments:
* query -- the SOQL query to send to Salesforce, e.g.
`SELECT Id FROM Lead WHERE Email = "waldo@somewhere.com"`
SELECT Id FROM Lead WHERE Email = "waldo@somewhere.com"
"""
url = self.base_url + 'query/'
params = {'q': query}
Expand Down Expand Up @@ -382,7 +391,7 @@ def query_all(self, query, **kwargs):
Arguments
* query -- the SOQL query to send to Salesforce, e.g.
`SELECT Id FROM Lead WHERE Email = "waldo@somewhere.com"`
SELECT Id FROM Lead WHERE Email = "waldo@somewhere.com"
"""

result = self.query(query, **kwargs)
Expand Down Expand Up @@ -736,7 +745,6 @@ def request(self, session):
_warn_request_deprecation()
self.session = session


class SalesforceAPI(Salesforce):
"""Deprecated SalesforceAPI Instance
Expand Down Expand Up @@ -771,7 +779,6 @@ def __init__(self, username, password, security_token, sandbox=False,
sandbox=sandbox,
version=sf_version)


def _exception_handler(result, name=""):
"""Exception router. Determines which error to raise for bad results"""
try:
Expand Down

0 comments on commit e9433e6

Please sign in to comment.