Skip to content

Commit 9fe3869

Browse files
committed
changed Salesforce authentication to the JWT flow, added test mode to return a subset of the contacts
1 parent 106254f commit 9fe3869

File tree

1 file changed

+23
-10
lines changed

1 file changed

+23
-10
lines changed
Lines changed: 23 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,16 @@
11
import os
22

3-
from sqlalchemy.orm import sessionmaker
3+
import structlog
44
from simple_salesforce import Salesforce
5+
from sqlalchemy.orm import sessionmaker
6+
57
from config import engine
68
from models import SalesForceContacts
79

8-
import structlog
910
logger = structlog.get_logger()
1011

12+
TEST_MODE = os.getenv("TEST_MODE") # if not present, has value None
13+
1114
def store_contacts_all():
1215
Session = sessionmaker(engine)
1316
with Session() as session:
@@ -16,12 +19,21 @@ def store_contacts_all():
1619
session.execute("TRUNCATE TABLE salesforcecontacts")
1720

1821
logger.debug("retrieving the latest salesforce contacts data")
19-
sf = Salesforce(domain=os.getenv('SALESFORCE_DOMAIN'), password=os.getenv('SALESFORCE_PW'), username=os.getenv('SALESFORCE_USERNAME'), security_token=os.getenv('SALESFORCE_SECURITY_TOKEN'))
22+
23+
if not os.path.exists('bin/connected-app-secrets.pem'):
24+
logger.warn("missing salesforce jwt private key, skipping data pull")
25+
return
26+
27+
sf = Salesforce(username=os.getenv('SALESFORCE_USERNAME'), consumer_key=os.getenv('SALESFORCE_CONSUMER_KEY'),
28+
privatekey_file='bin/connected-app-secrets.pem')
2029
results = sf.query("SELECT Contact_ID_18__c, FirstName, LastName, Contact.Account.Name, MailingCountry, MailingStreet, MailingCity, MailingState, MailingPostalCode, Phone, MobilePhone, Email FROM Contact")
21-
logger.debug("Query returned %d Salesforce contact records", len(results['records']) )
22-
30+
logger.debug("%d total Salesforce contact records", results['totalSize'])
31+
if TEST_MODE:
32+
logger.debug("running in test mode so only downloading first page of Salesforce contacts")
33+
2334
done = False
2435
while not done:
36+
logger.debug("Query returned %d Salesforce contact records", len(results['records']))
2537
for row in results['records']:
2638
account_name = row['Account']['Name'] if row['Account'] is not None else None
2739
contact = SalesForceContacts(contact_id=row['Contact_ID_18__c'],
@@ -35,10 +47,11 @@ def store_contacts_all():
3547
mailing_zip_postal_code=row['MailingPostalCode'],
3648
phone=row['Phone'],
3749
mobile=row['MobilePhone'],
38-
email=['Email'])
50+
email=row['Email'])
3951
session.add(contact)
40-
done = results['done']
41-
if not done:
42-
results = sf.query_more(results['nextRecordsUrl'])
52+
# if in test mode only return first page of results
53+
done = results['done'] if not TEST_MODE else True
54+
if not done:
55+
results = sf.query_more(results['nextRecordsUrl'], True)
4356
session.commit()
44-
logger.debug("finished downloading latest salesforce contacts data")
57+
logger.debug("finished downloading latest salesforce contacts data")

0 commit comments

Comments
 (0)