Skip to content

Commit b8663cb

Browse files
authored
feat(kno-3534): add support for schedules endpoints (#21)
* feat(kno-3534): add support for schedules endpoints * feat(kno-3534): add missing scheduled_at param * chore: code review tweaks
1 parent 910ab99 commit b8663cb

File tree

3 files changed

+158
-0
lines changed

3 files changed

+158
-0
lines changed

knockapi/resources/objects.py

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -157,6 +157,29 @@ def get_messages(self, collection, id, options=None):
157157

158158
return self.client.request('get', endpoint, payload=options)
159159

160+
##
161+
# Schedules
162+
##
163+
164+
def get_schedules(self, collection, id, options=None):
165+
"""
166+
Get an objects's schedules
167+
168+
Args:
169+
collection (str): The collection the object belongs to
170+
id (str): The id of the object in the collection
171+
options (dict): An optional set of filtering options to pass to the query. These are:
172+
- page_size: specify size of the page to be returned by the api. (max limit: 50)
173+
- after: after cursor for pagination
174+
- before: before cursor for pagination
175+
- tenant: tenant_id to filter schedules with
176+
177+
Returns:
178+
dict: Paginated Schedule response.
179+
"""
180+
endpoint = '/objects/{}/{}/schedules'.format(collection, id)
181+
return self.client.request('get', endpoint, payload=options)
182+
160183
##
161184
# Preferences
162185
##

knockapi/resources/users.py

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -403,3 +403,25 @@ def get_messages(self, id, options=None):
403403
"""
404404
endpoint = '/users/{}/messages'.format(id)
405405
return self.client.request('get', endpoint, payload=options)
406+
407+
##
408+
# Schedules
409+
##
410+
411+
def get_schedules(self, id, options=None):
412+
"""
413+
Get user's schedules
414+
415+
Args:
416+
id (str): The user ID
417+
options (dict): An optional set of filtering options to pass to the query. These are:
418+
- page_size: specify size of the page to be returned by the api. (max limit: 50)
419+
- after: after cursor for pagination
420+
- before: before cursor for pagination
421+
- tenant: tenant_id to filter schedules with
422+
423+
Returns:
424+
dict: Paginated Schedule response.
425+
"""
426+
endpoint = '/users/{}/schedules'.format(id)
427+
return self.client.request('get', endpoint, payload=options)

knockapi/resources/workflows.py

Lines changed: 113 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import datetime
12
from .service import Service
23

34

@@ -69,3 +70,115 @@ def cancel(self, key, cancellation_key, recipients=None):
6970
}
7071

7172
return self.client.request("post", endpoint, payload=params)
73+
74+
def create_schedules(
75+
self,
76+
key,
77+
recipients,
78+
repeats,
79+
data={},
80+
scheduled_at=datetime.datetime.now(),
81+
actor=None,
82+
tenant=None):
83+
"""
84+
Creates schedules for recipients.
85+
86+
Args:
87+
key (str): The key of the workflow to invoke.
88+
89+
actor (str | dict[str, Any]): An optional reference for who/what performed the action. This can be A) a user
90+
id, B) an object reference, or C) a dictionary with data to identify a user or object.
91+
92+
recipients (list[str | dict[str, Any]]): A list of recipients that should be notified. This can be a list of
93+
A) user ids, B) object references, C) dictionaries with data to identify a user or object, or D) a
94+
combination thereof.
95+
96+
data (dict): Any data to be passed to the scheduled trigger call.
97+
98+
scheduled_at (datetime): Date when the schedule must start
99+
100+
tenant (str): An optional identifier for the tenant object that the notifications belong to.
101+
102+
Returns:
103+
list[dict]: list of created schedules
104+
"""
105+
endpoint = '/schedules'
106+
107+
params = {
108+
'workflow': key,
109+
'recipients': recipients,
110+
'repeats': repeats,
111+
'actor': actor,
112+
'data': data,
113+
'tenant': tenant,
114+
'scheduled_at': scheduled_at.isoformat()
115+
}
116+
117+
return self.client.request("post", endpoint, payload=params)
118+
119+
def update_schedules(
120+
self,
121+
schedule_ids,
122+
schedule_attrs):
123+
"""
124+
Updates schedules with given attributes.
125+
126+
Args:
127+
schedule_ids (list[str]): the ids of the schedules to be updated (max 100)
128+
129+
schedule_attrs (dict): Schedule attributes to be updated, these can be: repeats, actor, data and tenant.
130+
131+
Returns:
132+
list[dict]: list of updated schedules
133+
"""
134+
endpoint = '/schedules'
135+
136+
schedule_attrs['schedule_ids'] = schedule_ids
137+
138+
return self.client.request("put", endpoint, payload=schedule_attrs)
139+
140+
def list_schedules(
141+
self,
142+
key,
143+
options = {}):
144+
"""
145+
List workflow schedules
146+
147+
Args:
148+
key (string): workflow key
149+
150+
options (dict): Supports the following optional arguments:
151+
- page_size: specify size of the page to be returned by the api. (max limit: 50)
152+
- after: after cursor for pagination
153+
- before: before cursor for pagination
154+
- tenant: tenant_id to filter schedules with
155+
- recipients: list of recipients to filter schedules with
156+
157+
Returns:
158+
dict: list of updated schedules
159+
"""
160+
161+
endpoint = '/schedules'
162+
options['workflow'] = key
163+
164+
return self.client.request('get', endpoint, payload=options)
165+
166+
def delete_schedules(
167+
self,
168+
schedule_ids):
169+
"""
170+
Delete schedules.
171+
172+
Args:
173+
schedule_ids (list[str]): the ids of the schedules to be updated (max 100)
174+
175+
Returns:
176+
list[dict]: list of updated schedules
177+
"""
178+
endpoint = '/schedules'
179+
180+
params = {
181+
'schedule_ids': schedule_ids,
182+
}
183+
184+
return self.client.request("delete", endpoint, payload=params)

0 commit comments

Comments
 (0)