Skip to content

Commit 5bbee86

Browse files
authored
feat: object preferences and unset channel data (#9)
* feat: object preferences and unset channel data * feat(users): add unset channel data
1 parent 0542ec3 commit 5bbee86

File tree

2 files changed

+232
-8
lines changed

2 files changed

+232
-8
lines changed

knockapi/resources/objects.py

Lines changed: 216 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ def get(self, collection, id):
1919
return self.client.request('get', endpoint)
2020

2121
# NOTE: This is `set_object` as `set` is a reserved keyword
22-
def set_object(self, collection, id, data = {}):
22+
def set_object(self, collection, id, data={}):
2323
"""
2424
Returns an object in a collection with the id given.
2525
@@ -32,7 +32,7 @@ def set_object(self, collection, id, data = {}):
3232
dict: A Knock Object
3333
"""
3434
endpoint = '/objects/{}/{}'.format(collection, id)
35-
return self.client.request('put', endpoint, payload = data)
35+
return self.client.request('put', endpoint, payload=data)
3636

3737
def bulk_set(self, collection, objects):
3838
"""
@@ -45,7 +45,7 @@ def bulk_set(self, collection, objects):
4545
Returns:
4646
dict: BulkOperation from Knock
4747
"""
48-
data = { 'objects': objects }
48+
data = {'objects': objects}
4949
endpoint = '/objects/{}/bulk/set'.format(collection)
5050
return self.client.request('post', endpoint, payload=data)
5151

@@ -74,7 +74,7 @@ def bulk_delete(self, collection, object_ids):
7474
Returns:
7575
dict: BulkOperation from Knock
7676
"""
77-
data = { 'object_ids': object_ids }
77+
data = {'object_ids': object_ids}
7878
endpoint = '/objects/{}/bulk/delete'.format(collection)
7979
return self.client.request('post', endpoint, payload=data)
8080

@@ -94,7 +94,8 @@ def get_channel_data(self, collection, id, channel_id):
9494
Returns:
9595
dict: Channel data from Knock.
9696
"""
97-
endpoint = '/objects/{}/{}/channel_data/{}'.format(collection, id, channel_id)
97+
endpoint = '/objects/{}/{}/channel_data/{}'.format(
98+
collection, id, channel_id)
9899
return self.client.request('get', endpoint)
99100

100101
def set_channel_data(self, collection, id, channel_id, channel_data):
@@ -110,9 +111,26 @@ def set_channel_data(self, collection, id, channel_id, channel_data):
110111
Returns:
111112
dict: Channel data from Knock.
112113
"""
113-
endpoint = '/objects/{}/{}/channel_data/{}'.format(collection, id, channel_id)
114+
endpoint = '/objects/{}/{}/channel_data/{}'.format(
115+
collection, id, channel_id)
114116
return self.client.request('put', endpoint, payload={'data': channel_data})
115117

118+
def unset_channel_data(self, collection, id, channel_id):
119+
"""
120+
Unsets the object's channel data for the given channel id.
121+
122+
Args:
123+
collection (str): The collection the object belongs to
124+
id (str): The id of the object in the collection
125+
channel_id (str): Target channel ID
126+
127+
Returns:
128+
None: No response
129+
"""
130+
endpoint = '/objects/{}/{}/channel_data/{}'.format(
131+
collection, id, channel_id)
132+
return self.client.request('delete', endpoint)
133+
116134
##
117135
# Messages
118136
##
@@ -131,3 +149,195 @@ def get_messages(self, collection, id, options=None):
131149
"""
132150
endpoint = '/objects/{}/{}/messages'.format(collection, id)
133151
return self.client.request('get', endpoint, payload=options)
152+
153+
##
154+
# Preferences
155+
##
156+
157+
def get_all_preferences(self, collection, id):
158+
"""
159+
Get an objects full set of preferences
160+
161+
Args:
162+
collection (str): The collection the object belongs to
163+
id (str): The id of the object in the collection
164+
165+
Returns:
166+
dict: PreferenceSet response from Knock.
167+
"""
168+
endpoint = '/objects/{}/{}/preferences'.format(collection, id)
169+
return self.client.request('get', endpoint)
170+
171+
def get_preferences(self, collection, id, options={}):
172+
"""
173+
Get a preference set
174+
175+
Args:
176+
collection (str): The collection the object belongs to
177+
id (str): The id of the object in the collection
178+
options (dict):
179+
preference_set (str): The preference set to retrieve (defaults to "default")
180+
181+
Returns:
182+
dict: PreferenceSet response from Knock.
183+
"""
184+
preference_set_id = options.get('preference_set', default_set_id)
185+
endpoint = '/objects/{}/{}/preferences/{}'.format(
186+
collection, id, preference_set_id)
187+
188+
return self.client.request('get', endpoint)
189+
190+
def set_preferences(self, collection, id, channel_types=None, categories=None, workflows=None, options={}):
191+
"""
192+
Sets the preference set
193+
194+
Args:
195+
collection (str): The collection the object belongs to
196+
id (str): The id of the object in the collection
197+
channel_types (dict): A dictionary of channel type preferences
198+
categories (dict): A dictionary of category preferences
199+
workflows (dict): A dictionary of workflow preferences
200+
options (dict): A dictionary of options
201+
202+
Returns:
203+
dict: PreferenceSet response from Knock.
204+
"""
205+
preference_set_id = options.get('preference_set', default_set_id)
206+
207+
endpoint = '/objects/{}/{}/preferences/{}'.format(
208+
collection, id, preference_set_id)
209+
210+
params = {
211+
'channel_types': channel_types,
212+
'categories': categories,
213+
'workflows': workflows
214+
}
215+
216+
return self.client.request('put', endpoint, payload=params)
217+
218+
def set_channel_types_preferences(self, collection, id, preferences, options={}):
219+
"""
220+
Sets the channel type preferences
221+
222+
Args:
223+
collection (str): The collection the object belongs to
224+
id (str): The id of the object in the collection
225+
preferences (dict): A dictionary of channel type preferences
226+
options (dict): A dictionary of options
227+
228+
Returns:
229+
dict: PreferenceSet response from Knock.
230+
"""
231+
preference_set_id = options.get('preference_set', default_set_id)
232+
233+
endpoint = '/objects/{}/{}/preferences/{}/channel_types'.format(
234+
collection, id, preference_set_id)
235+
236+
return self.client.request('put', endpoint, payload=preferences)
237+
238+
def set_channel_type_preferences(self, collection, id, channel_type, setting, options={}):
239+
"""
240+
Sets the channel type preference
241+
242+
Args:
243+
collection (str): The collection the object belongs to
244+
id (str): The id of the object in the collection
245+
channel_type (str): The channel_type to set
246+
setting (boolean): The preference setting
247+
options (dict): A dictionary of options
248+
249+
Returns:
250+
dict: PreferenceSet response from Knock.
251+
"""
252+
preference_set_id = options.get('preference_set', default_set_id)
253+
254+
endpoint = '/objects/{}/{}/preferences/{}/channel_types/{}'.format(
255+
collection, id, preference_set_id, channel_type)
256+
257+
return self.client.request('put', endpoint, payload={'subscribed': setting})
258+
259+
def set_workflows_preferences(self, collection, id, preferences, options={}):
260+
"""
261+
Sets the workflow preferences
262+
263+
Args:
264+
collection (str): The collection the object belongs to
265+
id (str): The id of the object in the collection
266+
preferences (dict): A dictionary of workflow preferences
267+
options (dict): A dictionary of options
268+
269+
Returns:
270+
dict: PreferenceSet response from Knock.
271+
"""
272+
preference_set_id = options.get('preference_set', default_set_id)
273+
274+
endpoint = '/objects/{}/{}/preferences/{}/workflows'.format(
275+
collection, id, preference_set_id)
276+
277+
return self.client.request('put', endpoint, payload=preferences)
278+
279+
def set_workflow_preferences(self, collection, id, key, setting, options={}):
280+
"""
281+
Sets the workflow preferences
282+
283+
Args:
284+
collection (str): The collection the object belongs to
285+
id (str): The id of the object in the collection
286+
key (str): The workflow key
287+
setting (boolean or dict): The preference setting
288+
options (dict): A dictionary of options
289+
290+
Returns:
291+
dict: PreferenceSet response from Knock.
292+
"""
293+
preference_set_id = options.get('preference_set', default_set_id)
294+
295+
endpoint = '/objects/{}/{}/preferences/{}/workflows/{}'.format(
296+
collection, id, preference_set_id, key)
297+
298+
params = setting if type(setting) is dict else {'subscribed': setting}
299+
300+
return self.client.request('put', endpoint, payload=params)
301+
302+
def set_categories_preferences(self, collection, id, preferences, options={}):
303+
"""
304+
Sets the categories preferences
305+
306+
Args:
307+
collection (str): The collection the object belongs to
308+
id (str): The id of the object in the collection
309+
preferences (dict): A dictionary of category preferences
310+
options (dict): A dictionary of options
311+
312+
Returns:
313+
dict: PreferenceSet response from Knock.
314+
"""
315+
preference_set_id = options.get('preference_set', default_set_id)
316+
317+
endpoint = '/objects/{}/{}/preferences/{}/categories'.format(
318+
collection, id, preference_set_id)
319+
320+
return self.client.request('put', endpoint, payload=preferences)
321+
322+
def set_category_preferences(self, collection, id, key, setting, options={}):
323+
"""
324+
Sets the category preferences
325+
326+
Args:
327+
collection (str): The collection the object belongs to
328+
id (str): The id of the object in the collection
329+
key (str): The category key
330+
setting (boolean or dict): The preference setting
331+
options (dict): A dictionary of options
332+
333+
Returns:
334+
dict: PreferenceSet response from Knock.
335+
"""
336+
preference_set_id = options.get('preference_set', default_set_id)
337+
338+
endpoint = '/objects/{}/{}/preferences/{}/categories/{}'.format(
339+
collection, id, preference_set_id, key)
340+
341+
params = setting if type(setting) is dict else {'subscribed': setting}
342+
343+
return self.client.request('put', endpoint, payload=params)

knockapi/resources/users.py

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -141,6 +141,20 @@ def set_channel_data(self, id, channel_id, channel_data):
141141
endpoint = '/users/{}/channel_data/{}'.format(id, channel_id)
142142
return self.client.request('put', endpoint, payload={'data': channel_data})
143143

144+
def unset_channel_data(self, id, channel_id):
145+
"""
146+
Unsets the user's channel data for the given channel id.
147+
148+
Args:
149+
id (str): The user ID
150+
channel_id (str): Target channel ID
151+
152+
Returns:
153+
None: no response
154+
"""
155+
endpoint = '/users/{}/channel_data/{}'.format(id, channel_id)
156+
return self.client.request('delete', endpoint)
157+
144158
##
145159
# Preferences
146160
##
@@ -319,7 +333,7 @@ def set_categories_preferences(self, user_id, preferences, options={}):
319333
options (dict): A dictionary of options
320334
321335
Returns:
322-
dict: User response from Knock.
336+
dict: PreferenceSet response from Knock.
323337
"""
324338
preference_set_id = options.get('preference_set', default_set_id)
325339

@@ -339,7 +353,7 @@ def set_category_preferences(self, user_id, key, setting, options={}):
339353
options (dict): A dictionary of options
340354
341355
Returns:
342-
dict: User response from Knock.
356+
dict: PreferenceSet response from Knock.
343357
"""
344358
preference_set_id = options.get('preference_set', default_set_id)
345359

0 commit comments

Comments
 (0)