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

Commit 46d94e0

Browse files
Merge pull request #15 from hellofresh/feature-update-events-api
Update events api
2 parents 8c6f588 + 290de94 commit 46d94e0

6 files changed

Lines changed: 320 additions & 45 deletions

File tree

.travis.yml

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
language: python
2+
python:
3+
- "2.7"
4+
- "3.3"
5+
- "3.4"
6+
- "3.5"
7+
- "3.6"
8+
9+
install:
10+
- make build
11+
12+
script:
13+
- make test
14+
- codecov

Makefile

Lines changed: 6 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,29 +1,17 @@
1-
VENV?=env
2-
31
help:
42
@echo "Please use \`make <target>' where <target> is one of"
53
@echo " test to run unit tests"
6-
@echo " build to build the working virtual environment, and to install requirements for development"
7-
@echo " clean to remove the created virtualenv folder"
4+
@echo " build to install requirements for development"
85

9-
build: clean virtualenv requirements test-requirments
6+
build: requirements test-requirements
107

118
test: unittests
129

1310
unittests:
14-
PYTHONPATH=$(CURDIR)/src nosetests -d -w tests/unit -v
15-
16-
functionaltests:
17-
PYTHONPATH=$(CURDIR)/src nosetests -d -w tests/functional -v
18-
19-
virtualenv:
20-
virtualenv $(CURDIR)/$(VENV)
21-
22-
clean:
23-
rm -rf $(CURDIR)/$(VENV)
11+
PYTHONPATH=$(CURDIR) nosetests -d -w tests -v --with-coverage --cover-package ./crossengage
2412

2513
requirements:
26-
$(CURDIR)/$(VENV)/bin/pip install -r requirements.txt
14+
pip install -r requirements.txt
2715

28-
test-requirments:
29-
$(CURDIR)/$(VENV)/bin/pip install -r dev-requirements.txt
16+
test-requirements:
17+
pip install -r test-requirements.txt

README.md

Lines changed: 24 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,30 @@
55
</p>
66

77
# crossengage-python-client
8-
Python client for Crossengage's API
8+
[![Build Status](https://travis-ci.org/hellofresh/crossengage-python-client.svg?branch=master)](https://travis-ci.org/hellofresh/crossengage-python-client)
9+
[![codecov](https://codecov.io/gh/hellofresh/crossengage-python-client/branch/master/graph/badge.svg)](https://codecov.io/gh/hellofresh/crossengage-python-client)
10+
11+
Python client for [Crossengage's API](https://docs.crossengage.io)
12+
13+
Library supports next methods:
14+
15+
**User profile management**
16+
- `update_user(self, user)`
17+
- `delete_user(self, user)`
18+
- `delete_user_by_xng_id(self, user)`
19+
20+
**User attributes management**
21+
- `add_user_attribute(self, attribute_name, attribute_type, nested_type)`
22+
- `add_nested_user_attribute(self, parent_name, attribute_name, attribute_type)`
23+
- `list_user_attributes(self, offset, limit)`
24+
- `delete_user_attribute(self, attribute_id)`
25+
26+
**Bulk user management**
27+
- `batch_process(self, delete_list=[], update_list=[])`
28+
29+
**Events management**
30+
- `send_events(self, events, email=None, user_id=None, business_unit=None)`
931

10-
[ ![Codeship build](https://codeship.com/projects/c70724e0-f905-0133-a8ad-268d110da048/status?branch=master)](https://codeship.com/projects/151121)
1132
### Owner
1233
[Alexander Zhilyaev](mailto:azh@hellofresh.com)
1334

@@ -51,4 +72,4 @@ For more examples, check `examples.py`.
5172

5273
To run the unit tests, make sure you have the [nose](http://nose.readthedocs.org/) module instaled and run the following from the repository root directory:
5374

54-
`$ nosetests`
75+
`$ make build && make test`

crossengage/client.py

Lines changed: 13 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@
22
import logging
33

44
import requests
5-
65
from requests.exceptions import RequestException
76

87

@@ -83,6 +82,7 @@ def update_user(self, user):
8382
def update_users_bulk(self, users):
8483
# type: (list) -> dict
8584
"""
85+
Warning! Deprecated method, use batch_process instead.
8686
Create / Update User bulk.
8787
:param users: list of user dicts [(email, id, firstName, lastName, birthday, createdAt, gender)]
8888
:return: json dict response
@@ -167,19 +167,18 @@ def delete_user_attribute(self, attribute_id):
167167
payload = {}
168168
return self.__create_request(payload, self.REQUEST_DELETE)
169169

170-
def send_events(self, events, email=None, external_id=None, business_unit=None):
171-
# type: (dict), (list) -> dict
170+
def send_events(self, events, email=None, user_id=None, business_unit=None):
172171
"""
173172
Send up to 50 events for a given user.
174173
:param email: user email
175174
:param events: list of event payloads
176175
:param business_unit: businessUnit of user in crossengage
177-
:param external_id: id of user in crossengage
176+
:param user_id: id of user in your database
178177
:return: json dict response, for example: {"status_code": 200}
179178
"""
180-
self.request_url = self.API_URL + self.EVENTS_ENDPOINT
179+
self.request_url = "{}{}".format(self.API_URL, self.EVENTS_ENDPOINT)
181180

182-
if email is None and external_id is None:
181+
if email is None and user_id is None:
183182
raise ValueError('email or external_id required for sending events')
184183

185184
payload = {
@@ -189,8 +188,8 @@ def send_events(self, events, email=None, external_id=None, business_unit=None):
189188
if email is not None:
190189
payload['email'] = email
191190

192-
if external_id is not None:
193-
payload['externalId'] = external_id
191+
if user_id is not None:
192+
payload['id'] = user_id
194193

195194
if business_unit is not None:
196195
payload['businessUnit'] = business_unit
@@ -283,16 +282,19 @@ def __create_request(self, payload, request_type):
283282

284283
except RequestException as e:
285284
# handle all requests HTTP exceptions
286-
response = {'success': False, 'errors': {'connection_error': e.message}}
285+
response = {'success': False, 'errors': {'connection_error': str(e)}}
287286
except Exception as e:
288287
# handle all exceptions which can be on API side
289-
response = {'success': False, 'errors': {'client_error': e.message + '. Response: ' + r.text}}
288+
response = {'success': False, 'errors': {'client_error': str(e)}}
290289

291290
if 'status_code' not in response:
292291
response['status_code'] = 0
293292

294293
if response['status_code'] == 500:
295294
response['success'] = False
296-
response['errors'] = {'server_error': response['message']}
295+
response['errors'] = {'server_error': 'error on crossengage side'}
296+
297+
if response['status_code'] > 202:
298+
response['success'] = False
297299

298300
return response
Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
11
unittest2
22
mock
3-
nose
3+
nose
4+
codecov

0 commit comments

Comments
 (0)