Skip to content
Merged
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
30 changes: 15 additions & 15 deletions customerio/track.py
Original file line number Diff line number Diff line change
Expand Up @@ -86,14 +86,14 @@ def get_device_query_string(self, customer_id):

def identify(self, id, **kwargs):
"""Identify a single customer by their unique id, and optionally add attributes."""
if not id:
if id is None or id == "":
raise CustomerIOException("id cannot be blank in identify")
url = self.get_customer_query_string(id)
return self.send_request("PUT", url, kwargs)

def track(self, customer_id, name, data=None, id=None, timestamp=None):
"""Track an event for a given customer_id."""
if not customer_id:
if customer_id is None or customer_id == "":
raise CustomerIOException("customer_id cannot be blank in track")
url = self.get_event_query_string(customer_id)
post_data = self._build_event(name, data, id=id, timestamp=timestamp)
Expand All @@ -103,14 +103,14 @@ def track_anonymous(self, anonymous_id, name, data=None, id=None, timestamp=None
"""Track an event for a given anonymous_id."""
url = self.get_events_query_string()
post_data = self._build_event(name, data, id=id, timestamp=timestamp)
if anonymous_id:
if anonymous_id is not None and anonymous_id != "":
post_data["anonymous_id"] = anonymous_id

return self.send_request("POST", url, post_data)

def pageview(self, customer_id, page, **data):
"""Track a pageview for a given customer_id."""
if not customer_id:
if customer_id is None or customer_id == "":
raise CustomerIOException("customer_id cannot be blank in pageview")
url = self.get_event_query_string(customer_id)
post_data = {
Expand All @@ -122,7 +122,7 @@ def pageview(self, customer_id, page, **data):

def backfill(self, customer_id, name, timestamp, **data):
"""Backfill an event (track with timestamp) for a given customer_id."""
if not customer_id:
if customer_id is None or customer_id == "":
raise CustomerIOException("customer_id cannot be blank in backfill")

url = self.get_event_query_string(customer_id)
Expand Down Expand Up @@ -166,21 +166,21 @@ def _build_event(self, name, data=None, id=None, timestamp=None):

def delete(self, customer_id):
"""Delete a customer profile."""
if not customer_id:
if customer_id is None or customer_id == "":
raise CustomerIOException("customer_id cannot be blank in delete")

url = self.get_customer_query_string(customer_id)
return self.send_request("DELETE", url, {})

def add_device(self, customer_id, device_id, platform, **data):
"""Add a device to a customer profile."""
if not customer_id:
if customer_id is None or customer_id == "":
raise CustomerIOException("customer_id cannot be blank in add_device")

if not device_id:
if device_id is None or device_id == "":
raise CustomerIOException("device_id cannot be blank in add_device")

if not platform:
if platform is None or platform == "":
raise CustomerIOException("platform cannot be blank in add_device")

data.update(
Expand All @@ -195,18 +195,18 @@ def add_device(self, customer_id, device_id, platform, **data):

def delete_device(self, customer_id, device_id):
"""Delete a device from a customer profile."""
if not customer_id:
if customer_id is None or customer_id == "":
raise CustomerIOException("customer_id cannot be blank in delete_device")

if not device_id:
if device_id is None or device_id == "":
raise CustomerIOException("device_id cannot be blank in delete_device")

url = self.get_device_query_string(customer_id)
delete_url = f"{url}/{self._url_encode(device_id)}"
return self.send_request("DELETE", delete_url, {})

def suppress(self, customer_id):
if not customer_id:
if customer_id is None or customer_id == "":
raise CustomerIOException("customer_id cannot be blank in suppress")

return self.send_request(
Expand All @@ -216,7 +216,7 @@ def suppress(self, customer_id):
)

def unsuppress(self, customer_id):
if not customer_id:
if customer_id is None or customer_id == "":
raise CustomerIOException("customer_id cannot be blank in unsuppress")

return self.send_request(
Expand All @@ -236,10 +236,10 @@ def merge_customers(self, primary_id_type, primary_id, secondary_id_type, second
if not self.is_valid_id_type(secondary_id_type):
raise CustomerIOException("invalid secondary id type")

if not primary_id:
if primary_id is None or primary_id == "":
raise CustomerIOException("primary customer_id cannot be blank")

if not secondary_id:
if secondary_id is None or secondary_id == "":
raise CustomerIOException("secondary customer_id cannot be blank")

url = f"{self.base_url}/merge_customers"
Expand Down
50 changes: 50 additions & 0 deletions tests/test_customerio.py
Original file line number Diff line number Diff line change
Expand Up @@ -672,6 +672,56 @@ def test_merge_customers_call(self):
secondary_id="",
)

def test_identify_with_zero_id(self):
self.cio.http.hooks = dict(
response=partial(
self._check_request,
rq={
"method": "PUT",
"url_suffix": "/customers/0",
"body": {"name": "john"},
},
)
)

self.cio.identify(id=0, name="john")

def test_track_with_zero_customer_id(self):
self.cio.http.hooks = dict(
response=partial(
self._check_request,
rq={
"method": "POST",
"url_suffix": "/customers/0/events",
"body": {"data": {}, "name": "login"},
},
)
)

self.cio.track(customer_id=0, name="login")

def test_identify_with_none_raises(self):
with self.assertRaises(CustomerIOException):
self.cio.identify(id=None, name="john")

def test_identify_with_empty_string_raises(self):
with self.assertRaises(CustomerIOException):
self.cio.identify(id="", name="john")

def test_delete_with_zero_customer_id(self):
self.cio.http.hooks = dict(
response=partial(
self._check_request,
rq={
"method": "DELETE",
"url_suffix": "/customers/0",
"body": {},
},
)
)

self.cio.delete(customer_id=0)

def test_batch_call(self):
self.cio.http.hooks = dict(
response=partial(
Expand Down