-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathpermissions.py
139 lines (111 loc) · 3.55 KB
/
permissions.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
"""Permission resources."""
from __future__ import annotations
from typing import List, overload
from requests.sessions import Session as Session
from .resources import Resource, ResourceParameters, Resources
class Permission(Resource):
def delete(self) -> None:
"""Delete the permission."""
path = f"v1/content/{self['content_guid']}/permissions/{self['id']}"
url = self.params.url + path
self.params.session.delete(url)
@overload
def update(self, *args, role: str, **kwargs) -> None:
"""Update the permission.
Parameters
----------
role : str
The principal role.
"""
@overload
def update(self, *args, **kwargs) -> None:
"""Update the permission."""
def update(self, *args, **kwargs) -> None:
"""Update the permission."""
body = {
"principal_guid": self.get("principal_guid"),
"principal_type": self.get("principal_type"),
"role": self.get("role"),
}
body.update(dict(*args))
body.update(**kwargs)
path = f"v1/content/{self['content_guid']}/permissions/{self['id']}"
url = self.params.url + path
response = self.params.session.put(
url,
json=body,
)
super().update(**response.json())
class Permissions(Resources):
def __init__(self, params: ResourceParameters, content_guid: str) -> None:
super().__init__(params)
self.content_guid = content_guid
def count(self) -> int:
"""Count the number of permissions.
Returns
-------
int
"""
return len(self.find())
@overload
def create(self, *, principal_guid: str, principal_type: str, role: str) -> Permission:
"""Create a permission.
Parameters
----------
principal_guid : str
principal_type : str
role : str
Returns
-------
Permission
"""
@overload
def create(self, **kwargs) -> Permission:
"""Create a permission.
Returns
-------
Permission
"""
def create(self, **kwargs) -> Permission:
"""Create a permission.
Returns
-------
Permission
"""
path = f"v1/content/{self.content_guid}/permissions"
url = self.params.url + path
response = self.params.session.post(url, json=kwargs)
return Permission(params=self.params, **response.json())
def find(self, **kwargs) -> List[Permission]:
"""Find permissions.
Returns
-------
List[Permission]
"""
path = f"v1/content/{self.content_guid}/permissions"
url = self.params.url + path
response = self.params.session.get(url, json=kwargs)
results = response.json()
return [Permission(self.params, **result) for result in results]
def find_one(self, **kwargs) -> Permission | None:
"""Find a permission.
Returns
-------
Permission | None
"""
permissions = self.find(**kwargs)
return next(iter(permissions), None)
def get(self, uid: str) -> Permission:
"""Get a permission.
Parameters
----------
uid : str
The permission id.
Returns
-------
Permission
"""
path = f"v1/content/{self.content_guid}/permissions/{uid}"
url = self.params.url + path
response = self.params.session.get(url)
return Permission(self.params, **response.json())