Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

separate the implementations between ver. 3 and 4 #43

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
54 changes: 9 additions & 45 deletions pypardot/client.py
Original file line number Diff line number Diff line change
@@ -1,26 +1,4 @@
import requests
from .objects.accounts import Accounts
from .objects.customfields import CustomFields
from .objects.customredirects import CustomRedirects
from .objects.dynamiccontent import DynamicContent
from .objects.emailclicks import EmailClicks
from .objects.emailtemplates import EmailTemplates
from .objects.forms import Forms
from .objects.lifecyclehistories import LifecycleHistories
from .objects.lifecyclestages import LifecycleStages
from .objects.lists import Lists
from .objects.listmemberships import ListMemberships
from .objects.emails import Emails
from .objects.prospects import Prospects
from .objects.opportunities import Opportunities
from .objects.prospectaccounts import ProspectAccounts
from .objects.tags import Tags
from .objects.tagobjects import TagObjects
from .objects.users import Users
from .objects.visits import Visits
from .objects.visitors import Visitors
from .objects.visitoractivities import VisitorActivities
from .objects.campaigns import Campaigns

from .errors import PardotAPIError

Expand All @@ -41,28 +19,14 @@ def __init__(self, email, password, user_key, version=4):
self.user_key = user_key
self.api_key = None
self.version = version
self.accounts = Accounts(self)
self.campaigns = Campaigns(self)
self.customfields = CustomFields(self)
self.customredirects = CustomRedirects(self)
self.dynamiccontent = DynamicContent(self)
self.emailclicks = EmailClicks(self)
self.emails = Emails(self)
self.emailtemplates = EmailTemplates(self)
self.forms = Forms(self)
self.lifecyclehistories = LifecycleHistories(self)
self.lifecyclestages = LifecycleStages(self)
self.listmemberships = ListMemberships(self)
self.lists = Lists(self)
self.opportunities = Opportunities(self)
self.prospects = Prospects(self)
self.prospectaccounts = ProspectAccounts(self)
self.tags = Tags(self)
self.tagobjects = TagObjects(self)
self.users = Users(self)
self.visits = Visits(self)
self.visitors = Visitors(self)
self.visitoractivities = VisitorActivities(self)
self._load_objects()

def _load_objects(self):
if self.version == 3:
from .objects_v3 import load_objects
else:
from .objects import load_objects
load_objects(self)

def post(self, object_name, path=None, params=None, retries=0):
"""
Expand Down Expand Up @@ -175,4 +139,4 @@ def _build_auth_header(self):
if not self.user_key or not self.api_key:
raise Exception('Cannot build Authorization header. user or api key is empty')
auth_string = 'Pardot api_key=%s, user_key=%s' % (self.api_key, self.user_key)
return {'Authorization': auth_string}
return {'Authorization': auth_string}
46 changes: 46 additions & 0 deletions pypardot/objects/__init__.py
Original file line number Diff line number Diff line change
@@ -1 +1,47 @@
from .accounts import Accounts
from .customfields import CustomFields
from .customredirects import CustomRedirects
from .dynamiccontent import DynamicContent
from .emailclicks import EmailClicks
from .emailtemplates import EmailTemplates
from .forms import Forms
from .lifecyclehistories import LifecycleHistories
from .lifecyclestages import LifecycleStages
from .lists import Lists
from .listmemberships import ListMemberships
from .emails import Emails
from .prospects import Prospects
from .opportunities import Opportunities
from .prospectaccounts import ProspectAccounts
from .tags import Tags
from .tagobjects import TagObjects
from .users import Users
from .visits import Visits
from .visitors import Visitors
from .visitoractivities import VisitorActivities
from .campaigns import Campaigns


def load_objects(client):
client.accounts = Accounts(client)
client.campaigns = Campaigns(client)
client.customfields = CustomFields(client)
client.customredirects = CustomRedirects(client)
client.dynamiccontent = DynamicContent(client)
client.emailclicks = EmailClicks(client)
client.emails = Emails(client)
client.emailtemplates = EmailTemplates(client)
client.forms = Forms(client)
client.lifecyclehistories = LifecycleHistories(client)
client.lifecyclestages = LifecycleStages(client)
client.listmemberships = ListMemberships(client)
client.lists = Lists(client)
client.opportunities = Opportunities(client)
client.prospects = Prospects(client)
client.prospectaccounts = ProspectAccounts(client)
client.tags = Tags(client)
client.tagobjects = TagObjects(client)
client.users = Users(client)
client.visits = Visits(client)
client.visitors = Visitors(client)
client.visitoractivities = VisitorActivities(client)
27 changes: 27 additions & 0 deletions pypardot/objects_v3/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
# This module (objects) was originally imported from Josh Geller's original implementation
# https://github.com/joshgeller/PyPardot/tree/349dde1fad561f32a425324005c4f2a0c4a23d9b/pypardot/objects
# The code in __init__.py was added to be integrated with PyPardot4 to support
# difference in the implementations between the versions 3 and 4.
from .lists import Lists
from .emails import Emails
from .prospects import Prospects
from .opportunities import Opportunities
from .accounts import Accounts
from .users import Users
from .visits import Visits
from .visitors import Visitors
from .visitoractivities import VisitorActivities
from .campaigns import Campaigns


def load_objects(client):
client.lists = Lists(client)
client.emails = Emails(client)
client.prospects = Prospects(client)
client.opportunities = Opportunities(client)
client.accounts = Accounts(client)
client.users = Users(client)
client.visits = Visits(client)
client.visitors = Visitors(client)
client.visitoractivities = VisitorActivities(client)
client.campaigns = Campaigns(client)
67 changes: 67 additions & 0 deletions pypardot/objects_v3/accounts.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
class Accounts(object):
"""
A class to query and use Pardot accounts.
Account field reference: http://developer.pardot.com/kb/api-version-3/object-field-references/#prospectAccount
"""

def __init__(self, client):
self.client = client

def query(self, **kwargs):
"""
Returns the prospect accounts matching the specified criteria parameters.
Supported search criteria: http://developer.pardot.com/kb/api-version-3/prospect-accounts/#supported-search-criteria
"""
response = self._get(path='/do/query', params=kwargs)

# Ensure result['prospectAccount'] is a list, no matter what.
result = response.get('result')
if result['total_results'] == 0:
result['prospectAccount'] = []
elif result['total_results'] == 1:
result['prospectAccount'] = [result['prospectAccount']]

return result

def create(self, **kwargs):
"""Creates a new prospect account."""
response = self._post(path='/do/create', params=kwargs)
return response

def describe(self, **kwargs):
"""
Returns the field metadata for prospect accounts, explaining what fields are available, their types, whether
they are required, and their options (for dropdowns, radio buttons, etc).
"""
response = self._get(path='/do/describe', params=kwargs)
return response

def read(self, id=None, **kwargs):
"""
Returns the data for the prospect account specified by <id>. <id> is the Pardot ID of the target prospect
account.
"""
response = self._post(path='/do/read/id/{id}'.format(id=id), params=kwargs)
return response

def update(self, id=None, **kwargs):
"""
Updates the data for the prospect account specified by <id>. <id> is the Pardot ID of the target prospect
account.
"""
response = self._post(path='/do/update/id/{id}'.format(id=id), params=kwargs)
return response

def _get(self, object_name='prospectAccount', path=None, params=None):
"""GET requests for the Account object."""
if params is None:
params = {}
response = self.client.get(object_name=object_name, path=path, params=params)
return response

def _post(self, object_name='prospectAccount', path=None, params=None):
"""POST requests for the Account object."""
if params is None:
params = {}
response = self.client.post(object_name=object_name, path=path, params=params)
return response
44 changes: 44 additions & 0 deletions pypardot/objects_v3/campaigns.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
class Campaigns(object):
"""
A class to query and use Pardot campaigns.
Campaign field reference: http://developer.pardot.com/kb/api-version-3/object-field-references/#campaign
"""

def __init__(self, client):
self.client = client

def query(self, **kwargs):
"""
Returns the campaigns matching the specified criteria parameters.
Supported search criteria: http://developer.pardot.com/kb/api-version-3/campaigns/#supported-search-criteria
"""
response = self._get(path='/do/query', params=kwargs)

# Ensure result['campaign'] is a list, no matter what.
result = response.get('result')
if result['total_results'] == 0:
result['campaign'] = []
elif result['total_results'] == 1:
result['campaign'] = [result['campaign']]

return result

def read_by_id(self, id=None, **kwargs):
"""
Returns the data for the campaign specified by <id>. <id> is the Pardot ID of the target campaign."""
response = self._post(path='/do/read/id/{id}'.format(id=id), params=kwargs)
return response

def _get(self, object_name='campaign', path=None, params=None):
"""GET requests for the Campaign object."""
if params is None:
params = {}
response = self.client.get(object_name=object_name, path=path, params=params)
return response

def _post(self, object_name='campaign', path=None, params=None):
"""POST requests for the Campaign object."""
if params is None:
params = {}
response = self.client.post(object_name=object_name, path=path, params=params)
return response
56 changes: 56 additions & 0 deletions pypardot/objects_v3/emails.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
class Emails(object):
"""
A class to query and send Pardot emails.
Email field reference: http://developer.pardot.com/kb/api-version-3/object-field-references/#email
"""

def __init__(self, client):
self.client = client

def send_to_email(self, prospect_email=None, **kwargs):
"""
Sends an email to the prospect identified by <prospect_email>.
Required parameters: (email_template_id OR (text_content, name, subject, & ((from_email & from_name) OR from_user_id)))
"""
response = self._post(
path='/do/send/prospect_email/{prospect_email}'.format(prospect_email=prospect_email),
params=kwargs)
return response

def send_to_id(self, prospect_id=None, **kwargs):
"""
Sends an email to the prospect identified by <prospect_id>.
Required parameters: (email_template_id OR (text_content, name, subject, & ((from_email & from_name) OR from_user_id)))
"""
response = self._post(
path='/do/send/prospect_id/{prospect_id}'.format(prospect_id=prospect_id), params=kwargs)
return response

def send_to_lists(self, **kwargs):
"""
Sends an email to the lists identified by <list_ids>.
Required parameters: (email_template_id OR (text_content, name, subject, & ((from_email & from_name) OR from_user_id)))
"""
kwargs['list_ids'] = kwargs.get('list_ids', None)
response = self._post(
path='/do/send/', params=kwargs)
return response

def read(self, id=None):
"""Returns the data for the email specified by <id>. <id> is the Pardot ID of the target email."""
response = self._post(path='/do/read/id/{id}'.format(id=id))
return response

def _get(self, object_name='email', path=None, params=None):
"""GET requests for the Email object."""
if params is None:
params = {}
response = self.client.get(object_name=object_name, path=path, params=params)
return response

def _post(self, object_name='email', path=None, params=None):
"""POST requests for the Email object."""
if params is None:
params = {}
response = self.client.post(object_name=object_name, path=path, params=params)
return response
45 changes: 45 additions & 0 deletions pypardot/objects_v3/lists.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
class Lists(object):
"""
A class to query and use Pardot lists.
List field reference: http://developer.pardot.com/kb/api-version-3/object-field-references/#list
"""

def __init__(self, client):
self.client = client

def query(self, **kwargs):
"""
Returns the lists matching the specified criteria parameters.
Supported search criteria: http://developer.pardot.com/kb/api-version-3/lists/#supported-search-criteria
"""
response = self._get(path='/do/query', params=kwargs)

# Ensure result['list'] is a list, no matter what.
result = response.get('result')
if result['total_results'] == 0:
result['list'] = []
elif result['total_results'] == 1:
result['list'] = [result['list']]

return result

def read(self, id=None):
"""
Returns the data for the list specified by <id>.<id> is the Pardot ID of the target list.
"""
response = self._post(path='/do/read/id/{id}'.format(id=id))
return response

def _get(self, object_name='list', path=None, params=None):
"""GET requests for the List object."""
if params is None:
params = {}
response = self.client.get(object_name=object_name, path=path, params=params)
return response

def _post(self, object_name='list', path=None, params=None):
"""POST requests for the List object."""
if params is None:
params = {}
response = self.client.post(object_name=object_name, path=path, params=params)
return response
Loading