11import warrant
2- from warrant import APIResource , Subject
2+ from warrant import APIResource , Subject , Warrant
33from enum import Enum
4- from typing import Any , Dict , List , Optional , Union
4+ from typing import Any , Dict , List
55
66
77class CheckOp (str , Enum ):
88 ANY_OF = "anyOf"
99 ALL_OF = "allOf"
1010
1111
12+ def map_warrant (warrant ):
13+ if isinstance (warrant , Warrant ):
14+ subject = {
15+ "objectType" : warrant .subject .object_type ,
16+ "objectId" : warrant .subject .object_id
17+ }
18+ if warrant .subject .relation != "" :
19+ subject ["relation" ] = warrant .subject .relation
20+
21+ return {
22+ "objectType" : warrant .object_type ,
23+ "objectId" : warrant .object_id ,
24+ "relation" : warrant .relation ,
25+ "subject" : subject
26+ }
27+ else :
28+ return warrant
29+
30+
1231class Authz (APIResource ):
1332 @classmethod
14- def check (cls , object_type : str , object_id : str , relation : str , subject , context : Dict [str , Any ] = {}, opts : Dict [str , Any ] = {}) -> bool :
33+ def check (cls , object_type : str , object_id : str , relation : str , subject : Subject | Dict [ str , Any ] , context : Dict [str , Any ] = {}, opts : Dict [str , Any ] = {}) -> bool :
1534 warrantToCheck = {
1635 "objectType" : object_type ,
1736 "objectId" : object_id ,
@@ -38,10 +57,11 @@ def check(cls, object_type: str, object_id: str, relation: str, subject, context
3857 return False
3958
4059 @classmethod
41- def check_many (cls , op : CheckOp , warrants : List [Dict [str , Any ]], opts : Dict [str , Any ] = {}):
60+ def check_many (cls , op : CheckOp , warrants : List [Dict [str , Any ] | Warrant ], opts : Dict [str , Any ] = {}):
61+ mapped_warrants = list (map (map_warrant , warrants ))
4262 payload = {
4363 "op" : op ,
44- "warrants" : warrants
64+ "warrants" : mapped_warrants
4565 }
4666 json_resp = cls ._post (uri = "/v2/check" , json_payload = payload , opts = opts )
4767 code = json_resp ["code" ]
0 commit comments