1+ use std:: ops:: Deref ;
12use std:: sync:: Arc ;
3+ use std:: time:: Duration ;
24
35use async_nats:: Subject ;
46use async_nats:: client:: traits:: Publisher ;
57use async_nats:: connection:: State ;
6- use pyo3:: Py ;
78use pyo3:: types:: { PyBytesMethods , PyDict } ;
89use pyo3:: { Bound , PyAny , Python , pyclass, pymethods, types:: PyBytes } ;
910use tokio:: sync:: RwLock ;
@@ -13,6 +14,44 @@ use crate::jetstream::kv::KeyValue;
1314use crate :: utils:: headers:: NatsrpyHeadermapExt ;
1415use crate :: { exceptions:: rust_err:: NatsrpyResult , utils:: natsrpy_future} ;
1516
17+ #[ pyclass]
18+ pub enum StorageType {
19+ File ,
20+ Memory ,
21+ }
22+
23+ impl From < & StorageType > for async_nats:: jetstream:: stream:: StorageType {
24+ fn from ( value : & StorageType ) -> Self {
25+ match value {
26+ StorageType :: File => Self :: File ,
27+ StorageType :: Memory => Self :: Memory ,
28+ }
29+ }
30+ }
31+
32+ #[ pyclass]
33+ pub struct Republish {
34+ pub source : String ,
35+ pub destination : String ,
36+ pub headers_only : bool ,
37+ }
38+ impl From < & Republish > for async_nats:: jetstream:: stream:: Republish {
39+ fn from ( value : & Republish ) -> Self {
40+ Self {
41+ source : value. source . clone ( ) ,
42+ destination : value. destination . clone ( ) ,
43+ headers_only : value. headers_only . clone ( ) ,
44+ }
45+ }
46+ }
47+
48+ #[ pyclass]
49+ pub struct Source {
50+ pub name : String ,
51+ pub filter_subject : Option < String > ,
52+ pub external : bool ,
53+ }
54+
1655#[ pyclass]
1756pub struct JetStream {
1857 ctx : Arc < RwLock < async_nats:: jetstream:: Context > > ,
@@ -29,11 +68,11 @@ impl JetStream {
2968#[ pymethods]
3069impl JetStream {
3170 #[ pyo3( signature = (
32- subject,
33- payload,
34- * ,
35- headers=None ,
36- reply=None ,
71+ subject,
72+ payload,
73+ * ,
74+ headers=None ,
75+ reply=None ,
3776 err_on_disconnect = false
3877 ) ) ]
3978 pub fn publish < ' a > (
@@ -70,22 +109,58 @@ impl JetStream {
70109 & self ,
71110 py : Python < ' a > ,
72111 bucket : String ,
112+ description : Option < String > ,
113+ max_value_size : Option < i32 > ,
114+ history : Option < i64 > ,
115+ max_age : Option < f32 > ,
116+ max_bytes : Option < i64 > ,
117+ storage : Option < Bound < ' a , StorageType > > ,
118+ num_replicas : Option < usize > ,
119+ republish : Option < Bound < ' a , Republish > > ,
120+ mirror : Option < Bound < ' a , Source > > ,
121+ // sources: Option<Vec<Source>>,
122+ mirror_direct : Option < bool > ,
123+ compression : Option < bool > ,
124+ // placement: Option<Place>,
125+ limit_markers : Option < f32 > ,
73126 ) -> NatsrpyResult < Bound < ' a , PyAny > > {
74127 let ctx = self . ctx . clone ( ) ;
128+ let storage = storage. map ( |val| val. borrow ( ) . deref ( ) . into ( ) ) ;
129+ let republish = republish. map ( |val| val. borrow ( ) . deref ( ) . into ( ) ) ;
75130 natsrpy_future ( py, async move {
76131 let js = ctx. read ( ) . await ;
77- Ok ( KeyValue :: new ( js. create_key_value ( async_nats:: jetstream:: kv:: Config {
78- bucket : bucket,
79- // todo!("Add other config options")
132+ let mut config = async_nats:: jetstream:: kv:: Config {
133+ bucket : bucket. clone ( ) ,
80134 ..Default :: default ( )
81- } ) . await ?) )
135+ } ;
136+ description
137+ . into_iter ( )
138+ . for_each ( |descr| config. description = descr) ;
139+ max_value_size
140+ . into_iter ( )
141+ . for_each ( |val| config. max_value_size = val) ;
142+ history. into_iter ( ) . for_each ( |val| config. history = val) ;
143+ max_age
144+ . into_iter ( )
145+ . for_each ( |val| config. max_age = Duration :: from_secs_f32 ( val) ) ;
146+ max_bytes. into_iter ( ) . for_each ( |val| config. max_bytes = val) ;
147+ num_replicas
148+ . into_iter ( )
149+ . for_each ( |val| config. num_replicas = val) ;
150+ mirror_direct
151+ . into_iter ( )
152+ . for_each ( |val| config. mirror_direct = val) ;
153+ compression
154+ . into_iter ( )
155+ . for_each ( |val| config. compression = val) ;
156+ storage. into_iter ( ) . for_each ( |val| config. storage = val) ;
157+ config. republish = republish;
158+ config. limit_markers = limit_markers. map ( Duration :: from_secs_f32) ;
159+
160+ Ok ( KeyValue :: new ( js. create_key_value ( config) . await ?) )
82161 } )
83162 }
84- pub fn get_kv < ' a > (
85- & self ,
86- py : Python < ' a > ,
87- bucket : String ,
88- ) -> NatsrpyResult < Bound < ' a , PyAny > > {
163+ pub fn get_kv < ' a > ( & self , py : Python < ' a > , bucket : String ) -> NatsrpyResult < Bound < ' a , PyAny > > {
89164 let ctx = self . ctx . clone ( ) ;
90165 natsrpy_future ( py, async move {
91166 let js = ctx. read ( ) . await ;
0 commit comments