Skip to content

Commit

Permalink
Support V4 Bulk Imports (#9)
Browse files Browse the repository at this point in the history
  • Loading branch information
daigotanaka authored Jan 18, 2024
1 parent 385a15f commit 040f9d3
Show file tree
Hide file tree
Showing 4 changed files with 108 additions and 5 deletions.
7 changes: 3 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,15 +1,14 @@
# PyPardotSF

PyPardotSF is yet another fork of PyPardot/PyPardot4. The main driver for the
fork is to address
[Pardot's authentication change](https://help.salesforce.com/articleView?id=000353746) in Sprint 2021 to use
fork is to address Pardot's authentication change in Spring 2021 to use
Salesforce OAuth.
(As seen on [PyPardot4 Issue #46](https://github.com/mneedham91/PyPardot4/issues/46))

Another new features of PyPardotSF includes:

- Support both Versions 3 & 4 of Pardot API
- Support Version 3 [Import API (for Prospects)](https://developer.pardot.com/kb/api-version-3/import/)
- Support Version 3 & 4 [Import API (for Prospects)](https://developer.salesforce.com/docs/marketing/pardot/guide/import-v4.html)

This is a working prototype and the code is currently being cleaned up and
more detailed documentation is underway.
Expand Down Expand Up @@ -86,7 +85,7 @@ p = PardotAPI(
p.prospects.read_by_email(email="[email protected]")
```

## Bulk Prospect Import (Ver 3 API only)
## Bulk Prospect Import

```
file_name = "data.csv"
Expand Down
2 changes: 2 additions & 0 deletions pypardot/objects/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
from .emailclicks import EmailClicks
from .emailtemplates import EmailTemplates
from .forms import Forms
from .importapi import Import
from .lifecyclehistories import LifecycleHistories
from .lifecyclestages import LifecycleStages
from .lists import Lists
Expand Down Expand Up @@ -32,6 +33,7 @@ def load_objects(client):
client.emails = Emails(client)
client.emailtemplates = EmailTemplates(client)
client.forms = Forms(client)
client.importapi = Import(client)
client.lifecyclehistories = LifecycleHistories(client)
client.lifecyclestages = LifecycleStages(client)
client.listmemberships = ListMemberships(client)
Expand Down
102 changes: 102 additions & 0 deletions pypardot/objects/importapi.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,102 @@
import json

class Import(object):
"""
A class to query and use Pardot Import API.
Import API reference: https://developer.salesforce.com/docs/marketing/pardot/guide/import-v4.html
"""

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

def create(self, file_name=None, **kwargs):
"""Creates a new asynchronous import.
A single part with the name "importFile" should contain the CSV file for the batch. The file should contain a header row.
Prarams(as importInput if file_name is not null):
{
"operation": "Upsert",
"object": "Prospect",
"state": "Ready"
}
"""
if not file_name:
headers = {"Content-Type": "application/json"}
response = self._post(path='/do/create', json=kwargs, headers=headers)
return response

with open(file_name, "rb") as f:
files = {"importFile": f}
params = {"importInput": json.dumps(kwargs)}
response = self._post(path='/do/create',
params=params,
files=files)
return response

def add_batch(self, id, file_name, **kwargs):
"""Allows adding batches of data to an existing import when in the "Open" state.
A single part with the name "importFile" should contain the CSV file for the batch. The file should contain a header row.
"""
with open(file_name, "rb") as f:
files = {"importFile": f}
response = self._post(path='/do/batch/id/{id}'.format(id=id),
params=kwargs,
files=files)

return response

def update(self, id=None, **kwargs):
"""Used to submit the import by changing the state to "Ready". After this step, no more batches of data can be added, and processing of the import begins.
"""
headers = {"Content-Type": "application/json"}
response = self._patch(path='/do/update/id/{id}'.format(id=id),
json=kwargs, headers=headers)
return response

def read(self, id=None, **kwargs):
"""Returns the current state of the import. If processing is complete, the output provides a path to the results of the operation along with any statistics about the operation.
"""
response = self._get(path='/do/read/id/{id}'.format(id=id), params=kwargs)
return response

def query(self, **kwargs):
"""Used by administrators to retrieve a list of imports and their status. A user must have the “Admin > Imports > View” ability to execute this endpoint.
"""
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['import'] = []
elif result['total_results'] == 1:
result['import'] = [result['import']]

return result

def download_errors(self, **kwargs):
"""Download errors associated with the specified import (after it is complete).
"""
response = self._get(path='/do/downloadErrors/id/{id}'.format(id=id), params=kwargs)
return response

def _get(self, object_name='import', 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='import', path=None, params=None,
headers=None, json=None, files=None):
"""POST requests for the Account object."""
response = self.client.post(object_name=object_name, path=path,
params=params, headers=headers,
json=json, files=files)
return response

def _patch(self, object_name='import', path=None, params=None,
headers=None, json=None, files=None):
"""POST requests for the Account object."""
response = self.client.patch(object_name=object_name, path=path,
params=params, headers=headers,
json=json, files=files)
return response
2 changes: 1 addition & 1 deletion pypardot/objects_v3/importapi.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
class Import(object):
"""
A class to query and use Pardot Import API.
Import API reference: https://developer.pardot.com/kb/api-version-3/import/
Import API reference: https://developer.salesforce.com/docs/marketing/pardot/guide/import-v3.html
"""

def __init__(self, client):
Expand Down

0 comments on commit 040f9d3

Please sign in to comment.