Skip to content

Commit 46be50a

Browse files
authored
feat(kno-3746): add subscription endpoints support (#22)
* feat(kno-3746): add missing listing endpoints * feat(kno-3746): add subscription endpoints support * chore: remove incorrect comments * chore: code review tweaks
1 parent 07682dd commit 46be50a

File tree

2 files changed

+107
-0
lines changed

2 files changed

+107
-0
lines changed

lib/knock/resources/objects.ex

Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,20 @@ defmodule Knock.Objects do
1212
"""
1313
@type ref :: %{id: :string, collection: :string}
1414

15+
@doc """
16+
Returns paginated list of objects for a collection
17+
18+
# Available optional parameters:
19+
#
20+
# - page_size: specify size of the page to be returned by the api. (max limit: 50)
21+
# - after: after cursor for pagination
22+
# - before: before cursor for pagination
23+
"""
24+
@spec list(Client.t(), String.t(), Keyword.t()) :: Api.response()
25+
def list(client, collection, options \\ []) do
26+
Api.get(client, "/objects/#{collection}", query: options)
27+
end
28+
1529
@doc """
1630
Builds an object reference, which can be used in workflow trigger calls.
1731
"""
@@ -138,6 +152,67 @@ defmodule Knock.Objects do
138152
Api.get(client, "/objects/#{collection}/#{id}/schedules", query: options)
139153
end
140154

155+
##
156+
# Subscriptions
157+
##
158+
159+
@doc """
160+
Returns paginated subscriptions for the given object
161+
162+
# Available optional parameters:
163+
#
164+
# - page_size: specify size of the page to be returned by the api. (max limit: 50)
165+
# - after: after cursor for pagination
166+
# - before: before cursor for pagination
167+
"""
168+
@spec list_subscriptions(Client.t(), String.t(), String.t(), Keyword.t()) :: Api.response()
169+
def list_subscriptions(client, collection, id, options \\ []) do
170+
Api.get(client, "/objects/#{collection}/#{id}/subscriptions", query: options)
171+
end
172+
173+
@doc """
174+
Returns paginated subscriptions for the given object as recipient
175+
176+
# Available optional parameters:
177+
#
178+
# - page_size: specify size of the page to be returned by the api. (max limit: 50)
179+
# - after: after cursor for pagination
180+
# - before: before cursor for pagination
181+
"""
182+
@spec get_subscriptions(Client.t(), String.t(), String.t(), Keyword.t()) :: Api.response()
183+
def get_subscriptions(client, collection, id, options \\ []) do
184+
options = Keyword.put(options, :mode, "recipient")
185+
Api.get(client, "/objects/#{collection}/#{id}/subscriptions", query: options)
186+
end
187+
188+
@doc """
189+
Adds subscriptions for all recipients passed as arguments
190+
191+
Expected properties:
192+
- recipients: list of recipients to create subscriptions for
193+
- properties: data to be stored at the subscription level for each recipient
194+
"""
195+
@spec add_subscriptions(Client.t(), String.t(), String.t(), map()) :: Api.response()
196+
def add_subscriptions(client, collection, id, params) do
197+
Api.post(client, "/objects/#{collection}/#{id}/subscriptions", params)
198+
end
199+
200+
@doc """
201+
Delete subscriptions for recipients passed as arguments
202+
203+
Expected properties:
204+
- recipients: list of recipients to create subscriptions for
205+
"""
206+
@spec delete_subscriptions(Client.t(), String.t(), String.t(), [String.t() | map()]) ::
207+
Api.response()
208+
def delete_subscriptions(client, collection, id, params) do
209+
recipients = Map.get(params, :recipients)
210+
211+
Api.delete(client, "/objects/#{collection}/#{id}/subscriptions",
212+
body: %{recipients: recipients}
213+
)
214+
end
215+
141216
##
142217
# Preferences
143218
##

lib/knock/resources/users.ex

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,20 @@ defmodule Knock.Users do
99

1010
@default_preference_set_id "default"
1111

12+
@doc """
13+
Returns paginated list of users
14+
15+
# Available optional parameters:
16+
#
17+
# - page_size: specify size of the page to be returned by the api. (max limit: 50)
18+
# - after: after cursor for pagination
19+
# - before: before cursor for pagination
20+
"""
21+
@spec list(Client.t(), Keyword.t()) :: Api.response()
22+
def list(client, options \\ []) do
23+
Api.get(client, "/users", query: options)
24+
end
25+
1226
@doc """
1327
Returns information about the user from the `user_id` given.
1428
"""
@@ -310,6 +324,24 @@ defmodule Knock.Users do
310324
Api.get(client, "/users/#{id}/schedules", query: options)
311325
end
312326

327+
##
328+
# Subscriptions
329+
##
330+
331+
@doc """
332+
Returns paginated subscriptions for the given user
333+
334+
# Available optional parameters:
335+
#
336+
# - page_size: specify size of the page to be returned by the api. (max limit: 50)
337+
# - after: after cursor for pagination
338+
# - before: before cursor for pagination
339+
"""
340+
@spec get_subscriptions(Client.t(), String.t(), Keyword.t()) :: Api.response()
341+
def get_subscriptions(client, id, options \\ []) do
342+
Api.get(client, "/users/#{id}/subscriptions", query: options)
343+
end
344+
313345
defp build_setting_param(setting) when is_map(setting), do: setting
314346
defp build_setting_param(setting), do: %{subscribed: setting}
315347
end

0 commit comments

Comments
 (0)