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

Commit d277dea

Browse files
authored
Merge pull request #22 from hellofresh/feature/out-in
Add support to opt into channels
2 parents 5b074d1 + ed02d08 commit d277dea

File tree

3 files changed

+49
-0
lines changed

3 files changed

+49
-0
lines changed

README.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,11 @@ Library supports next methods:
3434
**Events management**
3535
- `send_events(self, events, email=None, user_id=None, business_unit=None)` | v1
3636

37+
**Opt-Out Management**
38+
- `get_user_opt_out_status(self, user_id)` | v1
39+
- `update_user_opt_out_status(self, user_id, channel_name)` | v1
40+
- `update_user_opt_in_status(self, user_id, channel_name)` | v1
41+
3742
### Owner
3843
[Alexander Zhilyaev](mailto:azh@hellofresh.com)
3944

crossengage/client.py

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -381,6 +381,23 @@ def update_user_opt_out_status(self, user_id, channel_name):
381381
)
382382
return self.__create_request(payload={"optOut": True}, request_type=self.REQUEST_PUT, version="v1")
383383

384+
def update_user_opt_in_status(self, user_id, channel_name):
385+
# type: (str, str) -> dict
386+
"""
387+
Fetch User Opt-In status by id.
388+
:param user_id: User ID
389+
:param channel_name: Name of the channel to opt in user to. It has to be one of MAIL, BROWSER_NOTIFICATION,
390+
ONSITE_DISPLAY, EXIT_INTENT, PUSH_NOTIFICATION, DIRECT_MAIL or SMS
391+
:return: json dict response, for example:
392+
{
393+
"optOut": false
394+
}
395+
"""
396+
self.request_url = "{0}/{1}/{2}/{3}?channelType={4}".format(
397+
self.API_URL, self.USER_ENDPOINT, user_id, self.OPTOUT_ENDPOINT, channel_name
398+
)
399+
return self.__create_request(payload={"optOut": False}, request_type=self.REQUEST_PUT, version="v1")
400+
384401
def __create_request(self, payload, request_type, version):
385402
headers = update_dict(self.default_headers, {self.API_VERSION_HEADER: self.API_VERSIONS[version]})
386403
try:

tests/test_client.py

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -679,3 +679,30 @@ def test_update_user_opt_out_status(self):
679679
)
680680

681681
self.assertEqual(expected_response, result)
682+
683+
def test_update_user_opt_in_status(self):
684+
"""Crossengage returns status code 200"""
685+
# GIVEN
686+
expected_response = self.user.copy()
687+
expected_response.update({"status_code": codes.ok})
688+
response = Mock(status_code=codes.ok)
689+
response.json.return_value = expected_response
690+
requests = Mock()
691+
requests.get.return_value = response
692+
693+
requests = Mock()
694+
requests.put.return_value = response
695+
self.client.requests = requests
696+
697+
# WHEN
698+
result = self.client.update_user_opt_in_status(self.user['id'], self.user['channel'])
699+
700+
# THEN
701+
requests.put.assert_called_once_with(
702+
self.CROSSENGAGE_URL + 'users/1234/optout-status?channelType=' + self.user['channel'],
703+
data='{"optOut": false}',
704+
headers=self.default_headers_api_v1,
705+
timeout=30
706+
)
707+
708+
self.assertEqual(expected_response, result)

0 commit comments

Comments
 (0)