|
| 1 | +from .service import Service |
| 2 | + |
| 3 | +default_set_id = "default" |
| 4 | + |
| 5 | + |
| 6 | +class Preferences(Service): |
| 7 | + def get_all(self, user_id): |
| 8 | + """ |
| 9 | + Get a users full set of preferences |
| 10 | +
|
| 11 | + Args: |
| 12 | + user_id: The users ID |
| 13 | +
|
| 14 | + Returns: |
| 15 | + dict: User response from Knock. |
| 16 | + """ |
| 17 | + endpoint = '/users/{}/preferences'.format(user_id) |
| 18 | + return self.client.request('get', endpoint) |
| 19 | + |
| 20 | + def get(self, user_id, options={}): |
| 21 | + """ |
| 22 | + Get a users preference set |
| 23 | +
|
| 24 | + Args: |
| 25 | + user_id (str): The users ID |
| 26 | + options (dict): |
| 27 | + preference_set (str): The preference set to retrieve (defaults to "default") |
| 28 | +
|
| 29 | + Returns: |
| 30 | + dict: User response from Knock. |
| 31 | + """ |
| 32 | + preference_set_id = options.get('preference_set', default_set_id) |
| 33 | + endpoint = '/users/{}/preferences/{}'.format( |
| 34 | + user_id, preference_set_id) |
| 35 | + |
| 36 | + return self.client.request('get', endpoint) |
| 37 | + |
| 38 | + def update(self, user_id, channel_types=None, categories=None, workflows=None, options={}): |
| 39 | + """ |
| 40 | + Sets the preference set for the user |
| 41 | +
|
| 42 | + Args: |
| 43 | + user_id (str): The users ID |
| 44 | + channel_types (dict): A dictionary of channel type preferences |
| 45 | + categories (dict): A dictionary of category preferences |
| 46 | + workflows (dict): A dictionary of workflow preferences |
| 47 | + options (dict): A dictionary of options |
| 48 | +
|
| 49 | + Returns: |
| 50 | + dict: User response from Knock. |
| 51 | + """ |
| 52 | + preference_set_id = options.get('preference_set', default_set_id) |
| 53 | + |
| 54 | + endpoint = '/users/{}/preferences/{}'.format( |
| 55 | + user_id, preference_set_id) |
| 56 | + |
| 57 | + params = { |
| 58 | + 'channel_types': channel_types, |
| 59 | + 'categories': categories, |
| 60 | + 'workflows': workflows |
| 61 | + } |
| 62 | + |
| 63 | + return self.client.request('put', endpoint, payload=params) |
| 64 | + |
| 65 | + def set_channel_types(self, user_id, preferences, options={}): |
| 66 | + """ |
| 67 | + Sets the channel type preferences for the user |
| 68 | +
|
| 69 | + Args: |
| 70 | + user_id (str): The users ID |
| 71 | + preferences (dict): A dictionary of channel type preferences |
| 72 | + options (dict): A dictionary of options |
| 73 | +
|
| 74 | + Returns: |
| 75 | + dict: User response from Knock. |
| 76 | + """ |
| 77 | + preference_set_id = options.get('preference_set', default_set_id) |
| 78 | + |
| 79 | + endpoint = '/users/{}/preferences/{}/channel_types'.format( |
| 80 | + user_id, preference_set_id) |
| 81 | + |
| 82 | + return self.client.request('put', endpoint, payload=preferences) |
| 83 | + |
| 84 | + def set_channel_type(self, user_id, channel_type, setting, options={}): |
| 85 | + """ |
| 86 | + Sets the channel type preference for the user |
| 87 | +
|
| 88 | + Args: |
| 89 | + user_id (str): The users ID |
| 90 | + channel_type (str): The channel_type to set |
| 91 | + setting (boolean): The preference setting |
| 92 | + options (dict): A dictionary of options |
| 93 | +
|
| 94 | + Returns: |
| 95 | + dict: User response from Knock. |
| 96 | + """ |
| 97 | + preference_set_id = options.get('preference_set', default_set_id) |
| 98 | + |
| 99 | + endpoint = '/users/{}/preferences/{}/channel_types/{}'.format( |
| 100 | + user_id, preference_set_id, channel_type) |
| 101 | + |
| 102 | + return self.client.request('put', endpoint, payload={'subscribed': setting}) |
| 103 | + |
| 104 | + def set_workflows(self, user_id, preferences, options={}): |
| 105 | + """ |
| 106 | + Sets the workflow preferences for the user |
| 107 | +
|
| 108 | + Args: |
| 109 | + user_id (str): The users ID |
| 110 | + preferences (dict): A dictionary of workflow preferences |
| 111 | + options (dict): A dictionary of options |
| 112 | +
|
| 113 | + Returns: |
| 114 | + dict: User response from Knock. |
| 115 | + """ |
| 116 | + preference_set_id = options.get('preference_set', default_set_id) |
| 117 | + |
| 118 | + endpoint = '/users/{}/preferences/{}/workflows'.format( |
| 119 | + user_id, preference_set_id) |
| 120 | + |
| 121 | + return self.client.request('put', endpoint, payload=preferences) |
| 122 | + |
| 123 | + def set_workflow(self, user_id, key, setting, options={}): |
| 124 | + """ |
| 125 | + Sets the workflow preferences for the user |
| 126 | +
|
| 127 | + Args: |
| 128 | + user_id (str): The users ID |
| 129 | + key (str): The workflow key |
| 130 | + setting (boolean or dict): The preference setting |
| 131 | + options (dict): A dictionary of options |
| 132 | +
|
| 133 | + Returns: |
| 134 | + dict: User response from Knock. |
| 135 | + """ |
| 136 | + preference_set_id = options.get('preference_set', default_set_id) |
| 137 | + |
| 138 | + endpoint = '/users/{}/preferences/{}/workflows/{}'.format( |
| 139 | + user_id, preference_set_id, key) |
| 140 | + |
| 141 | + return self.client.request('put', endpoint, payload={'subscribed': setting}) |
| 142 | + |
| 143 | + def set_categories(self, user_id, preferences, options={}): |
| 144 | + """ |
| 145 | + Sets the categories preferences for the user |
| 146 | +
|
| 147 | + Args: |
| 148 | + user_id (str): The users ID |
| 149 | + preferences (dict): A dictionary of category preferences |
| 150 | + options (dict): A dictionary of options |
| 151 | +
|
| 152 | + Returns: |
| 153 | + dict: User response from Knock. |
| 154 | + """ |
| 155 | + preference_set_id = options.get('preference_set', default_set_id) |
| 156 | + |
| 157 | + endpoint = '/users/{}/preferences/{}/categories'.format( |
| 158 | + user_id, preference_set_id) |
| 159 | + |
| 160 | + return self.client.request('put', endpoint, payload=preferences) |
| 161 | + |
| 162 | + def set_category(self, user_id, key, setting, options={}): |
| 163 | + """ |
| 164 | + Sets the category preferences for the user |
| 165 | +
|
| 166 | + Args: |
| 167 | + user_id (str): The users ID |
| 168 | + key (str): The category key |
| 169 | + setting (boolean or dict): The preference setting |
| 170 | + options (dict): A dictionary of options |
| 171 | +
|
| 172 | + Returns: |
| 173 | + dict: User response from Knock. |
| 174 | + """ |
| 175 | + preference_set_id = options.get('preference_set', default_set_id) |
| 176 | + |
| 177 | + endpoint = '/users/{}/preferences/{}/categories/{}'.format( |
| 178 | + user_id, preference_set_id, key) |
| 179 | + |
| 180 | + return self.client.request('put', endpoint, payload={'subscribed': setting}) |
0 commit comments