1
+ use std:: sync:: Arc ;
2
+
3
+ use actor_core_client:: { self as actor_core_rs, CreateOptions , GetOptions , GetWithIdOptions } ;
4
+ use pyo3:: prelude:: * ;
5
+
6
+ use crate :: util:: { try_opts_from_kwds, PyKwdArgs } ;
7
+
8
+ use super :: handle:: ActorHandle ;
9
+
10
+ #[ pyclass( name = "AsyncClient" ) ]
11
+ pub struct Client {
12
+ client : Arc < actor_core_rs:: Client > ,
13
+ }
14
+
15
+ #[ pymethods]
16
+ impl Client {
17
+ #[ new]
18
+ #[ pyo3( signature=(
19
+ endpoint,
20
+ transport_kind="websocket" ,
21
+ encoding_kind="json"
22
+ ) ) ]
23
+ fn py_new (
24
+ endpoint : & str ,
25
+ transport_kind : & str ,
26
+ encoding_kind : & str ,
27
+ ) -> PyResult < Self > {
28
+ let transport_kind = try_transport_kind_from_str ( & transport_kind) ?;
29
+ let encoding_kind = try_encoding_kind_from_str ( & encoding_kind) ?;
30
+ let client = actor_core_rs:: Client :: new (
31
+ endpoint. to_string ( ) ,
32
+ transport_kind,
33
+ encoding_kind
34
+ ) ;
35
+
36
+ Ok ( Client {
37
+ client : Arc :: new ( client)
38
+ } )
39
+ }
40
+
41
+ #[ pyo3( signature = ( name, * * kwds) ) ]
42
+ fn get < ' a > ( & self , py : Python < ' a > , name : & str , kwds : Option < PyKwdArgs > ) -> PyResult < Bound < ' a , PyAny > > {
43
+ let opts = try_opts_from_kwds :: < GetOptions > ( kwds) ?;
44
+ let name = name. to_string ( ) ;
45
+ let client = self . client . clone ( ) ;
46
+
47
+ pyo3_async_runtimes:: tokio:: future_into_py ( py, async move {
48
+ let handle = client. get ( & name, opts) . await ;
49
+
50
+ match handle {
51
+ Ok ( handle) => Ok ( ActorHandle {
52
+ handle
53
+ } ) ,
54
+ Err ( e) => Err ( py_runtime_err ! (
55
+ "Failed to get actor: {}" ,
56
+ e
57
+ ) )
58
+ }
59
+ } )
60
+ }
61
+
62
+ #[ pyo3( signature = ( id, * * kwds) ) ]
63
+ fn get_with_id < ' a > ( & self , py : Python < ' a > , id : & str , kwds : Option < PyKwdArgs > ) -> PyResult < Bound < ' a , PyAny > > {
64
+ let opts = try_opts_from_kwds :: < GetWithIdOptions > ( kwds) ?;
65
+ let id = id. to_string ( ) ;
66
+ let client = self . client . clone ( ) ;
67
+
68
+ pyo3_async_runtimes:: tokio:: future_into_py ( py, async move {
69
+ let handle = client. get_with_id ( & id, opts) . await ;
70
+
71
+ match handle {
72
+ Ok ( handle) => Ok ( ActorHandle {
73
+ handle
74
+ } ) ,
75
+ Err ( e) => Err ( py_runtime_err ! (
76
+ "Failed to get actor: {}" ,
77
+ e
78
+ ) )
79
+ }
80
+ } )
81
+ }
82
+
83
+ #[ pyo3( signature = ( name, * * kwds) ) ]
84
+ fn create < ' a > ( & self , py : Python < ' a > , name : & str , kwds : Option < PyKwdArgs > ) -> PyResult < Bound < ' a , PyAny > > {
85
+ let opts = try_opts_from_kwds :: < CreateOptions > ( kwds) ?;
86
+ let name = name. to_string ( ) ;
87
+ let client = self . client . clone ( ) ;
88
+
89
+ pyo3_async_runtimes:: tokio:: future_into_py ( py, async move {
90
+ let handle = client. create ( & name, opts) . await ;
91
+
92
+ match handle {
93
+ Ok ( handle) => Ok ( ActorHandle {
94
+ handle
95
+ } ) ,
96
+ Err ( e) => Err ( py_runtime_err ! (
97
+ "Failed to get actor: {}" ,
98
+ e
99
+ ) )
100
+ }
101
+ } )
102
+ }
103
+ }
104
+
105
+ fn try_transport_kind_from_str (
106
+ transport_kind : & str
107
+ ) -> PyResult < actor_core_rs:: TransportKind > {
108
+ match transport_kind {
109
+ "websocket" => Ok ( actor_core_rs:: TransportKind :: WebSocket ) ,
110
+ "sse" => Ok ( actor_core_rs:: TransportKind :: Sse ) ,
111
+ _ => Err ( py_value_err ! (
112
+ "Invalid transport kind: {}" ,
113
+ transport_kind
114
+ ) ) ,
115
+ }
116
+ }
117
+
118
+ fn try_encoding_kind_from_str (
119
+ encoding_kind : & str
120
+ ) -> PyResult < actor_core_rs:: EncodingKind > {
121
+ match encoding_kind {
122
+ "json" => Ok ( actor_core_rs:: EncodingKind :: Json ) ,
123
+ "cbor" => Ok ( actor_core_rs:: EncodingKind :: Cbor ) ,
124
+ _ => Err ( py_value_err ! (
125
+ "Invalid encoding kind: {}" ,
126
+ encoding_kind
127
+ ) ) ,
128
+ }
129
+ }
0 commit comments