Skip to content

Commit fbacba7

Browse files
authored
Merge pull request #8 from knocklabs/jazambuja-kno-1169-python-sdk-add-new-messages-endpoint
feat(messages): add new message endpoint support
2 parents 4386d00 + ff622dd commit fbacba7

File tree

5 files changed

+115
-4
lines changed

5 files changed

+115
-4
lines changed

knockapi/client.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ def request(self, method, endpoint, payload=None):
2020
r = requests.request(
2121
method,
2222
url,
23+
params=payload,
2324
json=payload,
2425
headers=self.headers,
2526
)
@@ -69,6 +70,11 @@ def bulk_operations(self):
6970
from .resources import BulkOperations
7071
return BulkOperations(self)
7172

73+
@property
74+
def messages(self):
75+
from .resources import Messages
76+
return Messages(self)
77+
7278
# Defined at the top level here for convienience
7379
def notify(self, key, actor, recipients, data={}, cancellation_key=None, tenant=None):
7480
"""

knockapi/resources/__init__.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,4 +2,5 @@
22
from .workflows import Workflows
33
from .preferences import Preferences
44
from .objects import Objects
5-
from .bulk_operations import BulkOperations
5+
from .bulk_operations import BulkOperations
6+
from .messages import Messages

knockapi/resources/messages.py

Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
from .service import Service
2+
3+
class Messages(Service):
4+
def list(self, options=None):
5+
"""
6+
Gets a paginated list of Message records
7+
8+
Args:
9+
options (dict): An optional set of filtering options to pass to the query
10+
11+
Returns:
12+
dict: a paginated list of Message records
13+
"""
14+
endpoint = '/messages'
15+
return self.client.request('get', endpoint, payload=options)
16+
17+
def get(self, id):
18+
"""
19+
Get a message by its id
20+
21+
Args:
22+
id: The message ID
23+
24+
Returns:
25+
dict: Message response from Knock.
26+
"""
27+
endpoint = '/messages/{}'.format(id)
28+
return self.client.request('get', endpoint)
29+
30+
def get_content(self, id):
31+
"""
32+
Get a message's content by its id
33+
34+
Args:
35+
id: The message ID
36+
37+
Returns:
38+
dict: MessageContent response from Knock.
39+
"""
40+
endpoint = '/messages/{}/content'.format(id)
41+
return self.client.request('get', endpoint)
42+
43+
def get_activities(self, id, options=None):
44+
"""
45+
Get a message's activities by its id
46+
47+
Args:
48+
id: The message ID
49+
50+
Returns:
51+
dict: paginated Activity response from Knock.
52+
"""
53+
endpoint = '/messages/{}/activities'.format(id)
54+
return self.client.request('get', endpoint, options)
55+
56+
def get_events(self, id, options=None):
57+
"""
58+
Get a message's events by its id
59+
60+
Args:
61+
id: The message ID
62+
63+
Returns:
64+
dict: paginated Event response from Knock.
65+
"""
66+
endpoint = '/messages/{}/events'.format(id)
67+
return self.client.request('get', endpoint, options)

knockapi/resources/objects.py

Lines changed: 22 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -84,7 +84,7 @@ def bulk_delete(self, collection, object_ids):
8484

8585
def get_channel_data(self, collection, id, channel_id):
8686
"""
87-
Get user's channel data for the given channel id.
87+
Get object's channel data for the given channel id.
8888
8989
Args:
9090
collection (str): The collection the object belongs to
@@ -99,7 +99,7 @@ def get_channel_data(self, collection, id, channel_id):
9999

100100
def set_channel_data(self, collection, id, channel_id, channel_data):
101101
"""
102-
Upserts user's channel data for the given channel id.
102+
Upserts object's channel data for the given channel id.
103103
104104
Args:
105105
collection (str): The collection the object belongs to
@@ -111,4 +111,23 @@ def set_channel_data(self, collection, id, channel_id, channel_data):
111111
dict: Channel data from Knock.
112112
"""
113113
endpoint = '/objects/{}/{}/channel_data/{}'.format(collection, id, channel_id)
114-
return self.client.request('put', endpoint, payload={'data': channel_data})
114+
return self.client.request('put', endpoint, payload={'data': channel_data})
115+
116+
##
117+
# Messages
118+
##
119+
120+
def get_messages(self, collection, id, options=None):
121+
"""
122+
Get object's messages
123+
124+
Args:
125+
collection (str): The collection the object belongs to
126+
id (str): The id of the object in the collection
127+
options (dict): An optional set of filtering options to pass to the query
128+
129+
Returns:
130+
dict: Paginated Message response.
131+
"""
132+
endpoint = '/objects/{}/{}/messages'.format(collection, id)
133+
return self.client.request('get', endpoint, payload=options)

knockapi/resources/users.py

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -349,3 +349,21 @@ def set_category_preferences(self, user_id, key, setting, options={}):
349349
params = setting if type(setting) is dict else {'subscribed': setting}
350350

351351
return self.client.request('put', endpoint, payload=params)
352+
353+
##
354+
# Messages
355+
##
356+
357+
def get_messages(self, id, options=None):
358+
"""
359+
Get user's messages
360+
361+
Args:
362+
id (str): The user ID
363+
options (dict): An optional set of filtering options to pass to the query
364+
365+
Returns:
366+
dict: Paginated Message response.
367+
"""
368+
endpoint = '/users/{}/messages'.format(id)
369+
return self.client.request('get', endpoint, payload=options)

0 commit comments

Comments
 (0)