1- use std:: sync:: Arc ;
1+ use std:: { sync:: Arc , time :: Duration } ;
22
3+ use futures_util:: StreamExt ;
4+ use pyo3:: { Bound , PyAny , Python } ;
35use tokio:: sync:: RwLock ;
46
7+ use crate :: { exceptions:: rust_err:: NatsrpyResult , utils:: natsrpy_future} ;
8+
59type NatsPullConsumer =
610 async_nats:: jetstream:: consumer:: Consumer < async_nats:: jetstream:: consumer:: pull:: Config > ;
711
@@ -20,13 +24,68 @@ impl PullConsumer {
2024 }
2125}
2226
23- #[ pyo3:: pyclass]
24- pub struct PullMessageIterator {
25- inner : Arc < RwLock < async_nats:: jetstream:: consumer:: pull:: Batch > > ,
26- }
27-
28- #[ pyo3:: pymethods]
29- impl PullConsumer { }
30-
3127#[ pyo3:: pymethods]
32- impl PullMessageIterator { }
28+ impl PullConsumer {
29+ #[ pyo3( signature=(
30+ max_messages=None ,
31+ group=None ,
32+ priority=None ,
33+ max_bytes=None ,
34+ heartbeat=None ,
35+ expires=None ,
36+ min_pending=None ,
37+ min_ack_pending=None ,
38+ ) ) ]
39+ pub fn fetch < ' py > (
40+ & self ,
41+ py : Python < ' py > ,
42+ max_messages : Option < usize > ,
43+ group : Option < String > ,
44+ priority : Option < usize > ,
45+ max_bytes : Option < usize > ,
46+ heartbeat : Option < Duration > ,
47+ expires : Option < Duration > ,
48+ min_pending : Option < usize > ,
49+ min_ack_pending : Option < usize > ,
50+ ) -> NatsrpyResult < Bound < ' py , PyAny > > {
51+ let ctx = self . consumer . clone ( ) ;
52+ #[ allow( clippy:: significant_drop_tightening) ]
53+ natsrpy_future ( py, async move {
54+ // Because we borrow created value
55+ // later for modifications.
56+ let consumer = ctx. read ( ) . await ;
57+ let mut fetch_builder = consumer. fetch ( ) ;
58+ if let Some ( max_messages) = max_messages {
59+ fetch_builder = fetch_builder. max_messages ( max_messages) ;
60+ }
61+ if let Some ( group) = group {
62+ fetch_builder = fetch_builder. group ( group) ;
63+ }
64+ if let Some ( priority) = priority {
65+ fetch_builder = fetch_builder. priority ( priority) ;
66+ }
67+ if let Some ( max_bytes) = max_bytes {
68+ fetch_builder = fetch_builder. max_bytes ( max_bytes) ;
69+ }
70+ if let Some ( heartbeat) = heartbeat {
71+ fetch_builder = fetch_builder. heartbeat ( heartbeat) ;
72+ }
73+ if let Some ( expires) = expires {
74+ fetch_builder = fetch_builder. expires ( expires) ;
75+ }
76+ if let Some ( min_pending) = min_pending {
77+ fetch_builder = fetch_builder. min_pending ( min_pending) ;
78+ }
79+ if let Some ( min_ack_pending) = min_ack_pending {
80+ fetch_builder = fetch_builder. min_ack_pending ( min_ack_pending) ;
81+ }
82+ let mut messages = fetch_builder. messages ( ) . await ?;
83+ let mut ret_messages = Vec :: new ( ) ;
84+ while let Some ( msg) = messages. next ( ) . await {
85+ let raw_msg = msg?;
86+ ret_messages. push ( crate :: js:: message:: JetStreamMessage :: from ( raw_msg) ) ;
87+ }
88+ Ok ( ret_messages)
89+ } )
90+ }
91+ }
0 commit comments