Skip to content

Commit 5d2d605

Browse files
committed
Fixed something.
1 parent 39e4813 commit 5d2d605

7 files changed

Lines changed: 48 additions & 12 deletions

File tree

python/natsrpy/__init__.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,4 +7,5 @@
77
__all__ = [
88
"Nats",
99
"Subscription",
10+
"Message",
1011
]

python/natsrpy/_inner/__init__.pyi

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,5 @@
11
from natsrpy._inner.js import JetStream
2-
3-
class Message:
4-
subject: str
5-
reply: str | None
6-
payload: bytes
2+
from natsrpy._inner.message import Message
73

84
class Subscription:
95
def __aiter__(self) -> "Subscription": ...

python/natsrpy/_inner/message.pyi

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,3 +8,6 @@ class Message:
88
status: int | None
99
description: str | None
1010
length: int
11+
12+
def __repr__(self) -> str: ...
13+
def __str__(self) -> str: ...

src/js/jetstream.rs

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -101,11 +101,27 @@ impl JetStream {
101101
})
102102
}
103103

104-
pub fn delete_kv<'py>(&self, py: Python<'py>, bucket: String) -> NatsrpyResult<Bound<'py, PyAny>> {
104+
pub fn delete_kv<'py>(
105+
&self,
106+
py: Python<'py>,
107+
bucket: String,
108+
) -> NatsrpyResult<Bound<'py, PyAny>> {
105109
let ctx = self.ctx.clone();
106110
natsrpy_future(py, async move {
107111
let js = ctx.read().await;
108112
Ok(js.delete_key_value(bucket).await?.success)
109113
})
110114
}
115+
116+
pub fn get_consumer<'py>(
117+
&self,
118+
py: Python<'py>,
119+
bucket: String,
120+
) -> NatsrpyResult<Bound<'py, PyAny>> {
121+
let ctx = self.ctx.clone();
122+
natsrpy_future(py, async move {
123+
let js = ctx.read().await;
124+
Ok(KeyValue::new(js.get_key_value(bucket).await?))
125+
})
126+
}
111127
}

src/lib.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,8 @@ pub mod _inner {
1515
use super::nats_cls::NatsCls;
1616
#[pymodule_export]
1717
use super::subscription::Subscription;
18+
#[pymodule_export]
19+
use super::message::Message;
1820

1921
#[pymodule_export]
2022
use super::js::pymod as js;

src/message.rs

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,3 +47,24 @@ impl Message {
4747
})
4848
}
4949
}
50+
51+
#[pyo3::pymethods]
52+
impl Message {
53+
pub fn __repr__(&self) -> String {
54+
self.to_string()
55+
}
56+
}
57+
58+
impl ToString for Message {
59+
fn to_string(&self) -> String {
60+
format!(
61+
r#"Message<subject="{subject}", reply={reply}, payload={payload}, headers={headers}, description={description}, length={len}>"#,
62+
subject = self.subject,
63+
reply = format!("{:?}", self.reply),
64+
payload = self.payload.to_string(),
65+
headers = self.headers.to_string(),
66+
description = format!("{:?}", self.description),
67+
len = self.length,
68+
)
69+
}
70+
}

src/utils/py.rs

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -10,12 +10,9 @@ fn _inner_init(module: &Bound<'_, PyModule>, path: &str) -> PyResult<()> {
1010
let submod_name = submod_name.extract::<String>()?;
1111
let submod = module.getattr(&submod_name)?;
1212
let modpath = format!("{path}.{submod_name}");
13-
match submod.cast::<PyModule>() {
14-
Ok(submod) => {
15-
_inner_init(&submod, &modpath)?;
16-
sys_modules.set_item(&modpath, submod)?;
17-
}
18-
_ => continue,
13+
if let Ok(submod) = submod.cast::<PyModule>() {
14+
_inner_init(&submod, &modpath)?;
15+
sys_modules.set_item(&modpath, submod)?;
1916
}
2017
}
2118
Ok(())

0 commit comments

Comments
 (0)