def get_conn(self): """ Initialize a pardot instance. :param email: The email address associated with the account. :type string: string :param password: The password associated with the account. :type string: string :param user_key: The issued user_key associated with the account. :type user_key: string :param api_version: The version of the Pardot API that the account uses. This is set on the account level within Pardot. Possible values include: - 3 - 4 :type api_version: integer """ if self.pardot: return self.pardot self.connection = self.get_connection(self.conn_id) self.extras = self.connection.extra_dejson pardot = PardotAPI(email=self.extras['email'], password=self.extras['password'], user_key=self.extras['user_key'], version=self.extras['api_version']) pardot.authenticate() self.pardot = pardot return pardot
def pardot_login(): p = PardotAPI( email=PARDOT_EMAIL, password=PARDOT_PASS, user_key=PARDOT_API_KEY ) p.authenticate() return p
def setUp(self): self.pardot = PardotAPI(email=PARDOT_USER, password=PARDOT_PASSWORD, user_key=PARDOT_USER_KEY) self.pardot.authenticate() self.email_address = '*****@*****.**' self.first_name = 'Pickles' self.last_name = 'Zardnif' # Make sure there isn't an existing prospect in the test account. try: self.pardot.prospects.delete_by_email(email=self.email_address) except PardotAPIError as e: # Error code 4 is raised if the prospect doesn't exist. if e.err_code != 4: raise e
class MyBaseTestCase(unittest.TestCase): @classmethod def setUpClass(cls): """On inherited classes, run our `setUp` method""" # Inspired via http://stackoverflow.com/questions/1323455/python-unit-test-with-base-and-sub-class/17696807#17696807 if cls is not MyBaseTestCase and cls.setUp is not MyBaseTestCase.setUp: orig_setUp = cls.setUp def setUpOverride(self, *args, **kwargs): MyBaseTestCase.setUp(self) return orig_setUp(self, *args, **kwargs) cls.setUp = setUpOverride def setUp(self): self.pardot = PardotAPI(email=PARDOT_USER, password=PARDOT_PASSWORD, user_key=PARDOT_USER_KEY) self.pardot.authenticate() def tearDown(self): pass def init_object_data(self, field_map): data = {} for k, v in field_map.items(): if k in set(['id', 'created_at', 'updated_at']): continue else: if v['datatype'] == 'string': data[k] = '{} {}'.format(TEST_STRING_PREFIX, k.capitalize()) elif v['datatype'] == 'boolean': data[k] = TEST_BOOLEAN_VALUE elif v['datatype'] == 'integer' and 'fk' in v: data[k] = 1 else: raise Exception('unrecognized datatype {}'.format( v['datatype'])) return data
def setUp(self, delete=False): self.pardot = PardotAPI(email=PARDOT_USER, password=PARDOT_PASSWORD, user_key=PARDOT_USER_KEY, version=3) self.pardot.authenticate() self.sample_record = { 'email': '*****@*****.**', 'first_name': 'Pickles', 'last_name': 'Zardnif' } # Make sure there isn't an existing prospect in the test account. if delete: try: self.pardot.prospects.delete_by_email( email=self.sample_record['email']) except PardotAPIError as e: # Error code 4 is raised if the prospect doesn't exist. if e.err_code != 4: raise e
def get_client(config=None): global CLIENT if CLIENT is None: if not config: raise KeyError("Need to set config") sf_consumer_key = config["consumer_key"] sf_consumer_secret = config["consumer_secret"] sf_refresh_token = config["refresh_token"] business_unit_id = config["business_unit_id"] version = config.get("version", 3) CLIENT = PardotAPI(sf_consumer_key=sf_consumer_key, sf_consumer_secret=sf_consumer_secret, sf_refresh_token=sf_refresh_token, business_unit_id=business_unit_id, version=version) return CLIENT
class TestProspects(unittest.TestCase): def setUp(self): self.pardot = PardotAPI(email=PARDOT_USER, password=PARDOT_PASSWORD, user_key=PARDOT_USER_KEY) self.pardot.authenticate() self.email_address = '*****@*****.**' self.first_name = 'Pickles' self.last_name = 'Zardnif' # Make sure there isn't an existing prospect in the test account. try: self.pardot.prospects.delete_by_email(email=self.email_address) except PardotAPIError as e: # Error code 4 is raised if the prospect doesn't exist. if e.err_code != 4: raise e def tearDown(self): try: self.pardot.prospects.delete_by_email(email=self.email_address) except PardotAPIError as e: # Error code 4 is raised if the prospect doesn't exist. if e.err_code != 4: raise e def _check_prospect(self, prospect): self.assertEquals(self.email_address, prospect['email']) self.assertEquals(self.first_name, prospect['first_name']) self.assertEquals(self.last_name, prospect['last_name']) def test_create_and_read(self): with self.assertRaises(PardotAPIArgumentError): results = self.pardot.prospects.create_by_email( first_name=self.first_name, last_name=self.last_name) results = self.pardot.prospects.create_by_email( email=self.email_address, first_name=self.first_name, last_name=self.last_name) prospect = results['prospect'] self._check_prospect(prospect) with self.assertRaises(PardotAPIArgumentError): results = self.pardot.prospects.read_by_id(email='*****@*****.**') results = self.pardot.prospects.read_by_id(id=prospect['id']) self._check_prospect(results['prospect']) with self.assertRaises(PardotAPIArgumentError): results = self.pardot.prospects.read_by_email(id='test') results = self.pardot.prospects.read_by_email(email=prospect['email']) self._check_prospect(results['prospect']) def test_update(self): results = self.pardot.prospects.create_by_email( email=self.email_address, first_name=self.first_name, last_name='McGee') prospect = results['prospect'] self.assertEquals('McGee', prospect['last_name']) with self.assertRaises(PardotAPIArgumentError): self.pardot.prospects.update_by_email(id=prospect['id'], last_name='Ferdle') self.pardot.prospects.update_by_email(email=self.email_address, last_name='Ferdle') results = self.pardot.prospects.read_by_id(prospect['id']) self.assertEquals('Ferdle', results['prospect']['last_name']) with self.assertRaises(PardotAPIArgumentError): self.pardot.prospects.update_by_id(email=self.email_address, last_name='Ferdle') self.pardot.prospects.update_by_id(id=prospect['id'], last_name='Klampett') results = self.pardot.prospects.read_by_id(prospect['id']) self.assertEquals('Klampett', results['prospect']['last_name']) def test_upsert(self): with self.assertRaises(PardotAPIArgumentError): results = self.pardot.prospects.upsert_by_email( first_name=self.first_name, last_name=self.last_name) results = self.pardot.prospects.upsert_by_email( email=self.email_address, first_name=self.first_name, last_name=self.last_name) prospect = results['prospect'] self._check_prospect(prospect) with self.assertRaises(PardotAPIArgumentError): self.pardot.prospects.upsert_by_id(email=prospect['id'], last_name='Ferdle') self.pardot.prospects.upsert_by_email(email=self.email_address, first_name=self.first_name, last_name='Ferdle') results = self.pardot.prospects.read_by_email(prospect['email']) self.assertEquals('Ferdle', results['prospect']['last_name']) self.assertEquals(prospect['id'], results['prospect']['id']) def test_delete(self): results = self.pardot.prospects.create_by_email( email=self.email_address, first_name=self.first_name, last_name=self.last_name) prospect = results['prospect'] with self.assertRaises(PardotAPIArgumentError): self.pardot.prospects.delete_by_email(id=prospect['id']) with self.assertRaises(PardotAPIArgumentError): self.pardot.prospects.delete_by_id(email=prospect['email']) results = self.pardot.prospects.read_by_id(prospect['id']) self.pardot.prospects.delete_by_id(id=prospect['id']) with self.assertRaises(PardotAPIError): results = self.pardot.prospects.read_by_id(prospect['id'])
class TestImportApi(unittest.TestCase): def setUp(self, delete=False): self.pardot = PardotAPI(email=PARDOT_USER, password=PARDOT_PASSWORD, user_key=PARDOT_USER_KEY, version=3) self.pardot.authenticate() self.sample_record = { 'email': '*****@*****.**', 'first_name': 'Pickles', 'last_name': 'Zardnif' } # Make sure there isn't an existing prospect in the test account. if delete: try: self.pardot.prospects.delete_by_email( email=self.sample_record['email']) except PardotAPIError as e: # Error code 4 is raised if the prospect doesn't exist. if e.err_code != 4: raise e def tearDown(self, delete=False): if delete: try: self.pardot.prospects.delete_by_email( email=self.sample_record['email']) except PardotAPIError as e: # Error code 4 is raised if the prospect doesn't exist. if e.err_code != 4: raise e os.unlink(self.tempfile.name) assert not os.path.exists(self.tempfile.name) self.tempfile = None def _write_csv(self): self.tempfile = NamedTemporaryFile(mode='w', delete=False) field_names = self.sample_record.keys() writer = csv.DictWriter(self.tempfile, fieldnames=field_names) writer.writeheader() writer.writerow(self.sample_record) self.tempfile.close() return self.tempfile.name def _print_csv(self): print(self.tempfile.name + ':') with open(self.tempfile.name) as f: print(f.read()) def test_create_and_read(self): file_name = self._write_csv() # self._print_csv() results = self.pardot.importapi.create(operation='Upsert', object='Prospect') batch_id = results['id'] results = self.pardot.importapi.add_batch(id=batch_id, file_name=file_name) results = self.pardot.importapi.update(id=batch_id, state='Ready') assert results['state'] == 'Waiting' def test_create(self): file_name = self._write_csv() # self._print_csv() results = self.pardot.importapi.create(file_name=file_name, operation='Upsert', object='Prospect') batch_id = results['id'] results = self.pardot.importapi.update(id=batch_id, state='Ready') assert results['state'] == 'Waiting'
def _auth_pardot_client(self): return PardotAPI(email=self.email, password=self.password, user_key=self.user_key)
import sys from importlib import reload reload(sys) from pypardot.client import PardotAPI visitor_id = sys.argv[1] email = sys.argv[2] password = sys.argv[3] key = sys.argv[4] list_id = '98751' api_version = 3 p = PardotAPI( email=email, password=password, user_key=key, version=api_version ) p.authenticate() visitor = p.visitors.read(visitor_id)['visitor'] prospect_id = visitor['prospect']['id'] is_a_member = p.listmemberships.read(list_id, prospect_id) response = "Already a member" if not is_a_member: response = p.listmemberships.create(list_id, prospect_id) print(response)
class TestProspects(unittest.TestCase): def setUp(self): self.pardot = PardotAPI(email=PARDOT_USER, password=PARDOT_PASSWORD, user_key=PARDOT_USER_KEY) self.pardot.authenticate() self.email_address = '*****@*****.**' self.first_name = 'Pickles' self.last_name = 'Zardnif' # Make sure there isn't an existing prospect in the test account. try: self.pardot.prospects.delete_by_email(email=self.email_address) except PardotAPIError as e: # Error code 4 is raised if the prospect doesn't exist. if e.err_code != 4: raise e def tearDown(self): try: self.pardot.prospects.delete_by_email(email=self.email_address) except PardotAPIError as e: # Error code 4 is raised if the prospect doesn't exist. if e.err_code != 4: raise e def _check_prospect(self, prospect): self.assertEquals(self.email_address, prospect['email']) self.assertEquals(self.first_name, prospect['first_name']) self.assertEquals(self.last_name, prospect['last_name']) def test_create_and_read(self): with self.assertRaises(PardotAPIArgumentError): results = self.pardot.prospects.create_by_email(first_name=self.first_name, last_name=self.last_name) results = self.pardot.prospects.create_by_email(email=self.email_address, first_name=self.first_name, last_name=self.last_name) prospect = results['prospect'] self._check_prospect(prospect) with self.assertRaises(PardotAPIArgumentError): results = self.pardot.prospects.read_by_id(email='*****@*****.**') results = self.pardot.prospects.read_by_id(id=prospect['id']) self._check_prospect(results['prospect']) with self.assertRaises(PardotAPIArgumentError): results = self.pardot.prospects.read_by_email(id='test') results = self.pardot.prospects.read_by_email(email=prospect['email']) self._check_prospect(results['prospect']) def test_update(self): results = self.pardot.prospects.create_by_email(email=self.email_address, first_name=self.first_name, last_name='McGee') prospect = results['prospect'] self.assertEquals('McGee', prospect['last_name']) with self.assertRaises(PardotAPIArgumentError): self.pardot.prospects.update_by_email(id=prospect['id'], last_name='Ferdle') self.pardot.prospects.update_by_email(email=self.email_address, last_name='Ferdle') results = self.pardot.prospects.read_by_id(prospect['id']) self.assertEquals('Ferdle', results['prospect']['last_name']) with self.assertRaises(PardotAPIArgumentError): self.pardot.prospects.update_by_id(email=self.email_address, last_name='Ferdle') self.pardot.prospects.update_by_id(id=prospect['id'], last_name='Klampett') results = self.pardot.prospects.read_by_id(prospect['id']) self.assertEquals('Klampett', results['prospect']['last_name']) def test_upsert(self): with self.assertRaises(PardotAPIArgumentError): results = self.pardot.prospects.upsert_by_email(first_name=self.first_name, last_name=self.last_name) results = self.pardot.prospects.upsert_by_email(email=self.email_address, first_name=self.first_name, last_name=self.last_name) prospect = results['prospect'] self._check_prospect(prospect) with self.assertRaises(PardotAPIArgumentError): self.pardot.prospects.upsert_by_id(email=prospect['id'], last_name='Ferdle') self.pardot.prospects.upsert_by_email(email=self.email_address, first_name=self.first_name, last_name='Ferdle') results = self.pardot.prospects.read_by_email(prospect['email']) self.assertEquals('Ferdle', results['prospect']['last_name']) self.assertEquals(prospect['id'], results['prospect']['id']) def test_delete(self): results = self.pardot.prospects.create_by_email(email=self.email_address, first_name=self.first_name, last_name=self.last_name) prospect = results['prospect'] with self.assertRaises(PardotAPIArgumentError): self.pardot.prospects.delete_by_email(id=prospect['id']) with self.assertRaises(PardotAPIArgumentError): self.pardot.prospects.delete_by_id(email=prospect['email']) results = self.pardot.prospects.read_by_id(prospect['id']) self.pardot.prospects.delete_by_id(id=prospect['id']) with self.assertRaises(PardotAPIError): results = self.pardot.prospects.read_by_id(prospect['id'])
def setUp(self): self.pardot = PardotAPI(email=PARDOT_USER, password=PARDOT_PASSWORD, user_key=PARDOT_USER_KEY) self.pardot.authenticate()