Skip to content
Closed
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
16 changes: 16 additions & 0 deletions customerio/track.py
Original file line number Diff line number Diff line change
Expand Up @@ -264,6 +264,22 @@ def batch(self, operations):
url = f"https://{self.host}:{self.port}/api/v2/batch"
return self.send_request("POST", url, {"batch": operations})

def entity(self, **kwargs):
"""Send a single operation to the v2 entity endpoint.

This is the singular counterpart to `batch`: the keyword arguments form
one batch-style operation (e.g. type, action, identifiers, attributes).
See https://customer.io/docs/api/track/#operation/entity
"""
if not kwargs:
raise CustomerIOException("operation cannot be empty in entity")

if self.port == 443:
url = f"https://{self.host}/api/v2/entity"
else:
url = f"https://{self.host}:{self.port}/api/v2/entity"
return self.send_request("POST", url, kwargs)

def _build_session(self):
session = super()._build_session()
session.auth = (self.site_id, self.api_key)
Expand Down
30 changes: 30 additions & 0 deletions tests/test_customerio.py
Original file line number Diff line number Diff line change
Expand Up @@ -852,6 +852,36 @@ def test_batch_call(self):
with self.assertRaises(CustomerIOException):
self.cio.batch(None)

def test_entity_call(self):
self.cio.http.hooks = dict(
response=partial(
self._check_request,
rq={
"method": "POST",
"authorization": _basic_auth_str("siteid", "apikey"),
"content_type": "application/json",
"url_suffix": "/api/v2/entity",
"body": {
"type": "person",
"action": "identify",
"identifiers": {"id": "1"},
"attributes": {"name": "Alice"},
},
},
)
)

self.cio.entity(
type="person",
action="identify",
identifiers={"id": "1"},
attributes={"name": "Alice"},
)

def test_entity_empty_raises(self):
with self.assertRaises(CustomerIOException):
self.cio.entity()


if __name__ == "__main__":
unittest.main()
Loading