22
33from __future__ import annotations
44
5- from typing import TYPE_CHECKING , List , overload
5+ from typing import TYPE_CHECKING , List , Optional , overload
66
77from requests .sessions import Session as Session
88
@@ -67,18 +67,23 @@ def count(self) -> int:
6767 return len (self .find ())
6868
6969 @overload
70- def create (self , * , principal_guid : str , principal_type : str , role : str ) -> Permission : ...
70+ def create (self , / , * , principal_guid : str , principal_type : str , role : str ) -> Permission : ...
7171
7272 @overload
73- def create (self , * args : User | Group , role : str ) -> list [Permission ]: ...
74-
75- def create (self , * args : User | Group , ** kwargs ) -> Permission | list [Permission ]:
73+ def create (self , principal : User | Group , / , * , role : str ) -> Permission : ...
74+
75+ def create (
76+ self ,
77+ principal : Optional [User | Group ] = None ,
78+ / ,
79+ ** kwargs ,
80+ ) -> Permission :
7681 """Create a permission.
7782
7883 Parameters
7984 ----------
80- *args : User | Group
81- The principal users or groups to add.
85+ principal : User | Group
86+ The principal user or group to add.
8287 role : str
8388 The principal role. Currently only `"viewer"` and `"owner"` are supported.
8489 principal_guid : str
@@ -90,9 +95,8 @@ def create(self, *args: User | Group, **kwargs) -> Permission | list[Permission]
9095
9196 Returns
9297 -------
93- Permission | List[Permission]
94- Returns a `Permission` when the kwargs: `principal_guid` and `principal_type` are used.
95- Returns a `list[Permission]` when `*args` are used.
98+ Permission
99+ The created permission.
96100
97101 Examples
98102 --------
@@ -107,18 +111,19 @@ def create(self, *args: User | Group, **kwargs) -> Permission | list[Permission]
107111
108112 # Example groups and users
109113 groups = client.groups.find(prefix="GROUP_NAME_PREFIX_HERE")
114+ group = groups[0]
110115 user = client.users.get("USER_GUID_HERE")
111- users = [user]
112-
113- # Add many group and user permissions with the same role
114- content_item.permissions.create(*groups, *users, role=role)
116+ users_and_groups = [user, *groups]
115117
116118 # Add a group permission
117- group = groups[0]
118119 content_item.permissions.create(group, role=role)
119120 # Add a user permission
120121 content_item.permissions.create(user, role=role)
121122
123+ # Add many group and user permissions with the same role
124+ for principal in users_and_groups:
125+ content_item.permissions.create(principal, role=role)
126+
122127 # Add a group permission manually
123128 content_item.permissions.create(
124129 principal_guid=group["guid"],
@@ -136,35 +141,26 @@ def create(self, *args: User | Group, **kwargs) -> Permission | list[Permission]
136141 content_item.permissions.find()
137142 ```
138143 """
139- if len ( args ) > 0 :
144+ if principal is not None :
140145 # Avoid circular imports
141146 from .groups import Group
142147 from .users import User
143148
144- for arg in args :
145- if not isinstance (arg , (User , Group )):
146- raise TypeError (f"Invalid argument type: { type (arg )} " )
149+ if isinstance (principal , User ):
150+ principal_type = "user"
151+ elif isinstance (principal , Group ):
152+ principal_type = "group"
153+ else :
154+ raise TypeError (f"Invalid argument type: { type (principal ).__name__ } " )
155+
147156 if "principal_guid" in kwargs :
148- raise ValueError ("'principal_guid' can not be defined with `*args ` present." )
157+ raise ValueError ("'principal_guid' can not be defined with `principal ` present." )
149158 if "principal_type" in kwargs :
150- raise ValueError ("'principal_guid' can not be defined with `*args` present." )
151-
152- perms : list [Permission ] = []
153- for arg in args :
154- if isinstance (arg , User ):
155- principal_type = "user"
156- elif isinstance (arg , Group ):
157- principal_type = "group"
158- else :
159- raise TypeError (f"Invalid argument type: { type (arg )} " )
160-
161- perm = self .create (
162- principal_guid = arg ["guid" ],
163- principal_type = principal_type ,
164- role = kwargs ["role" ],
165- )
166- perms .append (perm )
167- return perms
159+ raise ValueError ("'principal_guid' can not be defined with `principal` present." )
160+
161+ # Set the corresponding kwargs
162+ kwargs ["principal_guid" ] = principal ["guid" ]
163+ kwargs ["principal_type" ] = principal_type
168164
169165 path = f"v1/content/{ self .content_guid } /permissions"
170166 url = self .params .url + path
0 commit comments