Skip to content

Commit 3c3b9b9

Browse files
authored
chore(kno-1566) add tenant methods (#11)
1 parent cc1b4b6 commit 3c3b9b9

File tree

12 files changed

+311
-50
lines changed

12 files changed

+311
-50
lines changed

.flake8

+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
[flake8]
2+
ignore = F401,F403
3+
max-line-length = 127

.github/workflows/python-app.yml

+39
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
# This workflow will install Python dependencies, run tests and lint with a single version of Python
2+
# For more information see: https://help.github.com/actions/language-and-framework-guides/using-python-with-github-actions
3+
4+
name: Python application
5+
6+
on:
7+
push:
8+
branches: [ "main" ]
9+
pull_request:
10+
branches: [ "main" ]
11+
12+
permissions:
13+
contents: read
14+
15+
jobs:
16+
build:
17+
18+
runs-on: ubuntu-latest
19+
20+
steps:
21+
- uses: actions/checkout@v3
22+
- name: Set up Python 3.10
23+
uses: actions/setup-python@v3
24+
with:
25+
python-version: "3.10"
26+
- name: Install dependencies
27+
run: |
28+
python -m pip install --upgrade pip
29+
pip install requests flake8 pytest
30+
if [ -f requirements.txt ]; then pip install -r requirements.txt; fi
31+
- name: Lint with flake8
32+
run: |
33+
# stop the build if there are Python syntax errors or undefined names
34+
flake8 . --count --select=E9,F63,F7,F82 --show-source --statistics
35+
# exit-zero treats all errors as warnings. The GitHub editor is 127 chars wide
36+
flake8 . --count --exit-zero --max-complexity=10 --max-line-length=127 --statistics
37+
- name: Test with pytest
38+
run: |
39+
python -m pytest

knockapi/client.py

+21-3
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,11 @@ def objects(self):
6565
from .resources import Objects
6666
return Objects(self)
6767

68+
@property
69+
def tenants(self):
70+
from .resources import Tenants
71+
return Tenants(self)
72+
6873
@property
6974
def bulk_operations(self):
7075
from .resources import BulkOperations
@@ -76,7 +81,14 @@ def messages(self):
7681
return Messages(self)
7782

7883
# Defined at the top level here for convienience
79-
def notify(self, key, actor, recipients, data={}, cancellation_key=None, tenant=None):
84+
def notify(
85+
self,
86+
key,
87+
actor,
88+
recipients,
89+
data={},
90+
cancellation_key=None,
91+
tenant=None):
8092
"""
8193
Triggers a workflow.
8294
@@ -92,12 +104,18 @@ def notify(self, key, actor, recipients, data={}, cancellation_key=None, tenant=
92104
93105
data (dict): Any data to be passed to the notify call.
94106
95-
tenant (str): An optional identifier for the tenant object that the notifications belong to.
107+
tenant (str): An optional identifier for the tenant that the notifications belong to.
96108
97109
cancellation_key (str): A key used to cancel this notify.
98110
99111
Returns:
100112
dict: Response from Knock.
101113
"""
102114
# Note: this is essentially a delegated method
103-
return self.workflows.trigger(key, actor, recipients, data=data, cancellation_key=cancellation_key, tenant=tenant)
115+
return self.workflows.trigger(
116+
key,
117+
actor,
118+
recipients,
119+
data=data,
120+
cancellation_key=cancellation_key,
121+
tenant=tenant)

knockapi/resources/bulk_operations.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -15,4 +15,4 @@ def get(self, id):
1515
dict: A Knock BulkOperation
1616
"""
1717
endpoint = '/bulk_operations/{}'.format(id)
18-
return self.client.request('get', endpoint)
18+
return self.client.request('get', endpoint)

knockapi/resources/messages.py

+1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
from .service import Service
22

3+
34
class Messages(Service):
45
def list(self, options=None):
56
"""

knockapi/resources/objects.py

+56-14
Original file line numberDiff line numberDiff line change
@@ -113,7 +113,9 @@ def set_channel_data(self, collection, id, channel_id, channel_data):
113113
"""
114114
endpoint = '/objects/{}/{}/channel_data/{}'.format(
115115
collection, id, channel_id)
116-
return self.client.request('put', endpoint, payload={'data': channel_data})
116+
return self.client.request(
117+
'put', endpoint, payload={
118+
'data': channel_data})
117119

118120
def unset_channel_data(self, collection, id, channel_id):
119121
"""
@@ -187,7 +189,14 @@ def get_preferences(self, collection, id, options={}):
187189

188190
return self.client.request('get', endpoint)
189191

190-
def set_preferences(self, collection, id, channel_types=None, categories=None, workflows=None, options={}):
192+
def set_preferences(
193+
self,
194+
collection,
195+
id,
196+
channel_types=None,
197+
categories=None,
198+
workflows=None,
199+
options={}):
191200
"""
192201
Sets the preference set
193202
@@ -215,7 +224,8 @@ def set_preferences(self, collection, id, channel_types=None, categories=None, w
215224

216225
return self.client.request('put', endpoint, payload=params)
217226

218-
def set_channel_types_preferences(self, collection, id, preferences, options={}):
227+
def set_channel_types_preferences(
228+
self, collection, id, preferences, options={}):
219229
"""
220230
Sets the channel type preferences
221231
@@ -235,15 +245,21 @@ def set_channel_types_preferences(self, collection, id, preferences, options={})
235245

236246
return self.client.request('put', endpoint, payload=preferences)
237247

238-
def set_channel_type_preferences(self, collection, id, channel_type, setting, options={}):
248+
def set_channel_type_preferences(
249+
self,
250+
collection,
251+
id,
252+
channel_type,
253+
setting,
254+
options={}):
239255
"""
240256
Sets the channel type preference
241257
242258
Args:
243259
collection (str): The collection the object belongs to
244260
id (str): The id of the object in the collection
245261
channel_type (str): The channel_type to set
246-
setting (boolean): The preference setting
262+
setting (boolean): The preference setting
247263
options (dict): A dictionary of options
248264
249265
Returns:
@@ -254,9 +270,16 @@ def set_channel_type_preferences(self, collection, id, channel_type, setting, op
254270
endpoint = '/objects/{}/{}/preferences/{}/channel_types/{}'.format(
255271
collection, id, preference_set_id, channel_type)
256272

257-
return self.client.request('put', endpoint, payload={'subscribed': setting})
273+
return self.client.request(
274+
'put', endpoint, payload={
275+
'subscribed': setting})
258276

259-
def set_workflows_preferences(self, collection, id, preferences, options={}):
277+
def set_workflows_preferences(
278+
self,
279+
collection,
280+
id,
281+
preferences,
282+
options={}):
260283
"""
261284
Sets the workflow preferences
262285
@@ -276,15 +299,21 @@ def set_workflows_preferences(self, collection, id, preferences, options={}):
276299

277300
return self.client.request('put', endpoint, payload=preferences)
278301

279-
def set_workflow_preferences(self, collection, id, key, setting, options={}):
302+
def set_workflow_preferences(
303+
self,
304+
collection,
305+
id,
306+
key,
307+
setting,
308+
options={}):
280309
"""
281310
Sets the workflow preferences
282311
283312
Args:
284313
collection (str): The collection the object belongs to
285314
id (str): The id of the object in the collection
286315
key (str): The workflow key
287-
setting (boolean or dict): The preference setting
316+
setting (boolean or dict): The preference setting
288317
options (dict): A dictionary of options
289318
290319
Returns:
@@ -295,11 +324,17 @@ def set_workflow_preferences(self, collection, id, key, setting, options={}):
295324
endpoint = '/objects/{}/{}/preferences/{}/workflows/{}'.format(
296325
collection, id, preference_set_id, key)
297326

298-
params = setting if type(setting) is dict else {'subscribed': setting}
327+
params = setting if isinstance(setting, dict) else {
328+
'subscribed': setting}
299329

300330
return self.client.request('put', endpoint, payload=params)
301331

302-
def set_categories_preferences(self, collection, id, preferences, options={}):
332+
def set_categories_preferences(
333+
self,
334+
collection,
335+
id,
336+
preferences,
337+
options={}):
303338
"""
304339
Sets the categories preferences
305340
@@ -319,15 +354,21 @@ def set_categories_preferences(self, collection, id, preferences, options={}):
319354

320355
return self.client.request('put', endpoint, payload=preferences)
321356

322-
def set_category_preferences(self, collection, id, key, setting, options={}):
357+
def set_category_preferences(
358+
self,
359+
collection,
360+
id,
361+
key,
362+
setting,
363+
options={}):
323364
"""
324365
Sets the category preferences
325366
326367
Args:
327368
collection (str): The collection the object belongs to
328369
id (str): The id of the object in the collection
329370
key (str): The category key
330-
setting (boolean or dict): The preference setting
371+
setting (boolean or dict): The preference setting
331372
options (dict): A dictionary of options
332373
333374
Returns:
@@ -338,6 +379,7 @@ def set_category_preferences(self, collection, id, key, setting, options={}):
338379
endpoint = '/objects/{}/{}/preferences/{}/categories/{}'.format(
339380
collection, id, preference_set_id, key)
340381

341-
params = setting if type(setting) is dict else {'subscribed': setting}
382+
params = setting if isinstance(setting, dict) else {
383+
'subscribed': setting}
342384

343385
return self.client.request('put', endpoint, payload=params)

knockapi/resources/preferences.py

+63-18
Original file line numberDiff line numberDiff line change
@@ -1,39 +1,84 @@
11
from .service import Service
22
from warnings import warn
33

4+
45
class Preferences(Service):
56
def get_all(self, user_id):
6-
warn("This method is deprecated. Use users.get_all_preferences instead.", DeprecationWarning, stacklevel=2)
7+
warn(
8+
"This method is deprecated. Use users.get_all_preferences instead.",
9+
DeprecationWarning,
10+
stacklevel=2)
711
return self.client.users.get_all_preferences(user_id)
812

913
def get(self, user_id, options={}):
10-
warn("This method is deprecated. Use users.get_preferences instead.", DeprecationWarning, stacklevel=2)
14+
warn(
15+
"This method is deprecated. Use users.get_preferences instead.",
16+
DeprecationWarning,
17+
stacklevel=2)
1118
return self.client.users.get_preferences(user_id, options)
1219

13-
def update(self, user_id, channel_types=None, categories=None, workflows=None, options={}):
14-
warn("This method is deprecated. Use users.set_preferences instead.", DeprecationWarning, stacklevel=2)
15-
return self.client.users.set_preferences(user_id, channel_types=channel_types, categories=categories, workflows=workflows, options=options)
20+
def update(
21+
self,
22+
user_id,
23+
channel_types=None,
24+
categories=None,
25+
workflows=None,
26+
options={}):
27+
warn(
28+
"This method is deprecated. Use users.set_preferences instead.",
29+
DeprecationWarning,
30+
stacklevel=2)
31+
return self.client.users.set_preferences(
32+
user_id,
33+
channel_types=channel_types,
34+
categories=categories,
35+
workflows=workflows,
36+
options=options)
1637

1738
def set_channel_types(self, user_id, preferences, options={}):
18-
warn("This method is deprecated. Use users.set_channel_types_preferences instead.", DeprecationWarning, stacklevel=2)
19-
return self.client.users.set_channel_types_preferences(user_id, preferences, options=options)
39+
warn(
40+
"This method is deprecated. Use users.set_channel_types_preferences instead.",
41+
DeprecationWarning,
42+
stacklevel=2)
43+
return self.client.users.set_channel_types_preferences(
44+
user_id, preferences, options=options)
2045

2146
def set_channel_type(self, user_id, channel_type, setting, options={}):
22-
warn("This method is deprecated. Use users.set_channel_type_preferences instead.", DeprecationWarning, stacklevel=2)
23-
return self.client.users.set_channel_types_preferences(user_id, channel_type, setting, options=options)
47+
warn(
48+
"This method is deprecated. Use users.set_channel_type_preferences instead.",
49+
DeprecationWarning,
50+
stacklevel=2)
51+
return self.client.users.set_channel_types_preferences(
52+
user_id, channel_type, setting, options=options)
2453

2554
def set_workflows(self, user_id, preferences, options={}):
26-
warn("This method is deprecated. Use users.set_workflows_preferences instead.", DeprecationWarning, stacklevel=2)
27-
return self.client.users.set_workflows_preferences(user_id, preferences, options=options)
55+
warn(
56+
"This method is deprecated. Use users.set_workflows_preferences instead.",
57+
DeprecationWarning,
58+
stacklevel=2)
59+
return self.client.users.set_workflows_preferences(
60+
user_id, preferences, options=options)
2861

2962
def set_workflow(self, user_id, key, setting, options={}):
30-
warn("This method is deprecated. Use users.set_workflow_preferences instead.", DeprecationWarning, stacklevel=2)
31-
return self.client.users.set_workflow_preferences(user_id, key, setting, setting, options=options)
63+
warn(
64+
"This method is deprecated. Use users.set_workflow_preferences instead.",
65+
DeprecationWarning,
66+
stacklevel=2)
67+
return self.client.users.set_workflow_preferences(
68+
user_id, key, setting, setting, options=options)
3269

3370
def set_categories(self, user_id, preferences, options={}):
34-
warn("This method is deprecated. Use users.set_categories_preferences instead.", DeprecationWarning, stacklevel=2)
35-
return self.client.users.set_categories_preferences(user_id, preferences, options=options)
36-
71+
warn(
72+
"This method is deprecated. Use users.set_categories_preferences instead.",
73+
DeprecationWarning,
74+
stacklevel=2)
75+
return self.client.users.set_categories_preferences(
76+
user_id, preferences, options=options)
77+
3778
def set_category(self, user_id, key, setting, options={}):
38-
warn("This method is deprecated. Use users.set_category_preferences instead.", DeprecationWarning, stacklevel=2)
39-
return self.client.users.set_category_preferences(user_id, key, setting, setting, options=options)
79+
warn(
80+
"This method is deprecated. Use users.set_category_preferences instead.",
81+
DeprecationWarning,
82+
stacklevel=2)
83+
return self.client.users.set_category_preferences(
84+
user_id, key, setting, setting, options=options)

0 commit comments

Comments
 (0)