24
24
from dapr .conf import settings
25
25
26
26
# Actor factory Callable type hint.
27
- ACTOR_FACTORY_CALLBACK = Callable [[ActorInterface , str , str ], ' ActorProxy' ]
27
+ ACTOR_FACTORY_CALLBACK = Callable [[ActorInterface , str , str ], " ActorProxy" ]
28
28
29
29
30
30
class ActorFactoryBase (ABC ):
31
31
@abstractmethod
32
32
def create (
33
- self , actor_type : str , actor_id : ActorId ,
34
- actor_interface : Optional [Type [ActorInterface ]] = None ) -> 'ActorProxy' :
33
+ self ,
34
+ actor_type : str ,
35
+ actor_id : ActorId ,
36
+ actor_interface : Optional [Type [ActorInterface ]] = None ,
37
+ ) -> "ActorProxy" :
35
38
...
36
39
37
40
@@ -44,32 +47,36 @@ class ActorProxyFactory(ActorFactoryBase):
44
47
"""
45
48
46
49
def __init__ (
47
- self ,
48
- message_serializer = DefaultJSONSerializer (),
49
- http_timeout_seconds : int = settings .DAPR_HTTP_TIMEOUT_SECONDS ):
50
+ self ,
51
+ message_serializer = DefaultJSONSerializer (),
52
+ http_timeout_seconds : int = settings .DAPR_HTTP_TIMEOUT_SECONDS ,
53
+ ):
50
54
# TODO: support serializer for state store later
51
55
self ._dapr_client = DaprActorHttpClient (message_serializer , timeout = http_timeout_seconds )
52
56
self ._message_serializer = message_serializer
53
57
54
58
def create (
55
- self , actor_type : str , actor_id : ActorId ,
56
- actor_interface : Optional [Type [ActorInterface ]] = None ) -> 'ActorProxy' :
59
+ self ,
60
+ actor_type : str ,
61
+ actor_id : ActorId ,
62
+ actor_interface : Optional [Type [ActorInterface ]] = None ,
63
+ ) -> "ActorProxy" :
57
64
return ActorProxy (
58
- self ._dapr_client , actor_type , actor_id ,
59
- actor_interface , self . _message_serializer )
65
+ self ._dapr_client , actor_type , actor_id , actor_interface , self . _message_serializer
66
+ )
60
67
61
68
62
69
class CallableProxy :
63
70
def __init__ (
64
- self , proxy : ' ActorProxy' , attr_call_type : Dict [str , Any ],
65
- message_serializer : Serializer ):
71
+ self , proxy : " ActorProxy" , attr_call_type : Dict [str , Any ], message_serializer : Serializer
72
+ ):
66
73
self ._proxy = proxy
67
74
self ._attr_call_type = attr_call_type
68
75
self ._message_serializer = message_serializer
69
76
70
77
async def __call__ (self , * args , ** kwargs ) -> Any :
71
78
if len (args ) > 1 :
72
- raise ValueError (' does not support multiple arguments' )
79
+ raise ValueError (" does not support multiple arguments" )
73
80
74
81
bytes_data = None
75
82
if len (args ) > 0 :
@@ -78,9 +85,9 @@ async def __call__(self, *args, **kwargs) -> Any:
78
85
else :
79
86
bytes_data = self ._message_serializer .serialize (args [0 ])
80
87
81
- rtnval = await self ._proxy .invoke_method (self ._attr_call_type [' actor_method' ], bytes_data )
88
+ rtnval = await self ._proxy .invoke_method (self ._attr_call_type [" actor_method" ], bytes_data )
82
89
83
- return self ._message_serializer .deserialize (rtnval , self ._attr_call_type [' return_types' ])
90
+ return self ._message_serializer .deserialize (rtnval , self ._attr_call_type [" return_types" ])
84
91
85
92
86
93
class ActorProxy :
@@ -94,11 +101,13 @@ class ActorProxy:
94
101
_default_proxy_factory = ActorProxyFactory ()
95
102
96
103
def __init__ (
97
- self , client : DaprActorClientBase ,
98
- actor_type : str ,
99
- actor_id : ActorId ,
100
- actor_interface : Optional [Type [ActorInterface ]],
101
- message_serializer : Serializer ):
104
+ self ,
105
+ client : DaprActorClientBase ,
106
+ actor_type : str ,
107
+ actor_id : ActorId ,
108
+ actor_interface : Optional [Type [ActorInterface ]],
109
+ message_serializer : Serializer ,
110
+ ):
102
111
self ._dapr_client = client
103
112
self ._actor_id = actor_id
104
113
self ._actor_type = actor_type
@@ -120,10 +129,12 @@ def actor_type(self) -> str:
120
129
121
130
@classmethod
122
131
def create (
123
- cls ,
124
- actor_type : str , actor_id : ActorId ,
125
- actor_interface : Optional [Type [ActorInterface ]] = None ,
126
- actor_proxy_factory : Optional [ActorFactoryBase ] = None ) -> 'ActorProxy' :
132
+ cls ,
133
+ actor_type : str ,
134
+ actor_id : ActorId ,
135
+ actor_interface : Optional [Type [ActorInterface ]] = None ,
136
+ actor_proxy_factory : Optional [ActorFactoryBase ] = None ,
137
+ ) -> "ActorProxy" :
127
138
"""Creates ActorProxy client to call actor.
128
139
129
140
Args:
@@ -157,10 +168,11 @@ async def invoke_method(self, method: str, raw_body: Optional[bytes] = None) ->
157
168
"""
158
169
159
170
if raw_body is not None and not isinstance (raw_body , bytes ):
160
- raise ValueError (f' raw_body { type (raw_body )} is not bytes type' )
171
+ raise ValueError (f" raw_body { type (raw_body )} is not bytes type" )
161
172
162
173
return await self ._dapr_client .invoke_method (
163
- self ._actor_type , str (self ._actor_id ), method , raw_body )
174
+ self ._actor_type , str (self ._actor_id ), method , raw_body
175
+ )
164
176
165
177
def __getattr__ (self , name : str ) -> CallableProxy :
166
178
"""Enables RPC style actor method invocation.
@@ -177,17 +189,18 @@ def __getattr__(self, name: str) -> CallableProxy:
177
189
AttributeError: method is not defined in Actor interface.
178
190
"""
179
191
if not self ._actor_interface :
180
- raise ValueError (' actor_interface is not set. use invoke method.' )
192
+ raise ValueError (" actor_interface is not set. use invoke method." )
181
193
182
194
if name not in self ._dispatchable_attr :
183
195
get_dispatchable_attrs_from_interface (self ._actor_interface , self ._dispatchable_attr )
184
196
185
197
attr_call_type = self ._dispatchable_attr .get (name )
186
198
if attr_call_type is None :
187
- raise AttributeError (f' { self ._actor_interface .__class__ } has no attribute { name } ' )
199
+ raise AttributeError (f" { self ._actor_interface .__class__ } has no attribute { name } " )
188
200
189
201
if name not in self ._callable_proxies :
190
202
self ._callable_proxies [name ] = CallableProxy (
191
- self , attr_call_type , self ._message_serializer )
203
+ self , attr_call_type , self ._message_serializer
204
+ )
192
205
193
206
return self ._callable_proxies [name ]
0 commit comments