Skip to content
This repository was archived by the owner on Sep 6, 2025. It is now read-only.

Commit 4555a09

Browse files
authored
Merge pull request #14 from hellofresh/added-bulk-endpoint
added bulk endpoint support
2 parents 83d59ae + dda21a0 commit 4555a09

1 file changed

Lines changed: 63 additions & 7 deletions

File tree

crossengage/client.py

Lines changed: 63 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,8 @@ class CrossengageClient(object):
4343
API_VERSION = '1'
4444

4545
USER_ENDPOINT = '/users/'
46-
EVENTS_ENDPOINT = '/events'
46+
EVENTS_ENDPOINT = '/events/'
47+
BULK_ENDPOINT = '/users/batch'
4748

4849
REQUEST_GET = 'get'
4950
REQUEST_PUT = 'put'
@@ -62,7 +63,11 @@ def __init__(self, client_token):
6263
self.client_token = client_token
6364
self.requests = requests
6465
self.request_url = ''
65-
self.headers = {}
66+
self.headers = {
67+
'X-XNG-AuthToken': self.client_token,
68+
'X-XNG-ApiVersion': self.API_VERSION,
69+
'Content-Type': 'application/json',
70+
}
6671

6772
def update_user(self, user):
6873
# type: (dict) -> dict
@@ -192,13 +197,64 @@ def send_events(self, events, email=None, external_id=None, business_unit=None):
192197

193198
return self.__create_request(payload, self.REQUEST_POST)
194199

195-
def __create_request(self, payload, request_type):
196-
self.headers = {
197-
'X-XNG-AuthToken': self.client_token,
198-
'X-XNG-ApiVersion': self.API_VERSION,
199-
'Content-Type': 'application/json',
200+
def batch_process(self, delete_list=[], update_list=[]):
201+
"""
202+
Delete or Update up to 1000 users in batch.
203+
:param delete_list: user that should get deleted
204+
:param update_list: user that should get updated
205+
:return: integer status_code, json dict response
206+
{
207+
"updated": [
208+
{
209+
"id": "fb85fe50-a528-11e7-abc4-cec278b6b50a",
210+
"xngId": "088818b3-445e-41a6-a7e1-cf86c8cdfbe4",
211+
"success": false,
212+
"errors": [
213+
{
214+
"field": "id",
215+
"type": "NOT_NULL"
216+
},
217+
{
218+
"field": "email",
219+
"type": "WRONG_FORMAT"
220+
}
221+
]
222+
}
223+
],
224+
"deleted": [
225+
{
226+
"id": "78ad0e3e-19e6-4ec1-84a7-b2c860c05387",
227+
"xngId": "ae86796f-8aca-4f65-a5dc-dea9a269f2a5",
228+
"success": false,
229+
"errors": [
230+
{
231+
"field": "id",
232+
"type": "NOT_NULL"
233+
},
234+
{
235+
"field": "email",
236+
"type": "WRONG_FORMAT"
237+
}
238+
]
239+
}
240+
]
241+
}
242+
"""
243+
self.request_url = self.API_URL + self.BULK_ENDPOINT
244+
payload = {
245+
'updated': update_list,
246+
'deleted': delete_list,
200247
}
201248

249+
r = self.requests.post(
250+
self.request_url,
251+
data=json.dumps(payload),
252+
headers=self.headers
253+
)
254+
255+
return r.status_code, r.json()
256+
257+
def __create_request(self, payload, request_type):
202258
r = '{}'
203259
try:
204260
if request_type == self.REQUEST_PUT:

0 commit comments

Comments
 (0)