1+ # NOTE: This is a simple example of how to use retries. There are certain things like TLS, etc. that are not included here for brevity.
2+
3+ from authzed .api .v1 import (
4+ Client ,
5+ ObjectReference ,
6+ Relationship ,
7+ RelationshipUpdate ,
8+ SubjectReference ,
9+ WriteRelationshipsRequest ,
10+ )
11+ from grpcutil import insecure_bearer_token_credentials
12+ import json
13+
14+
15+ service_config_json = json .dumps (
16+ {
17+ "methodConfig" : [
18+ {
19+ #NOTE: This retry will only apply to the WriteRelationships method, to apply retry to all methods, put [{}] in the "name" field
20+ "name" : [{"service" : "authzed.api.v1.PermissionsService" , "method" : "WriteRelationships" }],
21+ "retryPolicy" : {
22+ "maxAttempts" : 3 ,
23+ "initialBackoff" : "1s" ,
24+ "maxBackoff" : "4s" ,
25+ "backoffMultiplier" : 2.0 ,
26+ "retryableStatusCodes" : ['UNAVAILABLE' , 'RESOURCE_EXHAUSTED' , 'DEADLINE_EXCEEDED' , 'ABORTED' ],
27+ },
28+ }
29+ ]
30+ }
31+ )
32+ options = []
33+ options .append (("grpc.service_config" , service_config_json ))
34+
35+ # NOTE: it's also recommended to implement a retry policy for the client connection, however that is omitted here for brevity
36+ client = Client (
37+ "localhost:50051" ,
38+ insecure_bearer_token_credentials ("abc123" ),
39+ options = options ,
40+ )
41+
42+ resp = client .WriteRelationships (
43+ WriteRelationshipsRequest (
44+ updates = [
45+ RelationshipUpdate (
46+ operation = RelationshipUpdate .Operation .OPERATION_TOUCH ,
47+ relationship = Relationship (
48+ resource = ObjectReference (object_type = "document" , object_id = "1" ),
49+ relation = "owner" ,
50+ subject = SubjectReference (
51+ object = ObjectReference (
52+ object_type = "user" ,
53+ object_id = "tom" ,
54+ )
55+ ),
56+ ),
57+ ),
58+ ]
59+ )
60+ )
61+
62+ print (resp .written_at .token )
0 commit comments