-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathjetstream.rs
More file actions
131 lines (120 loc) · 3.67 KB
/
jetstream.rs
File metadata and controls
131 lines (120 loc) · 3.67 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
use std::{ops::Deref, sync::Arc};
use async_nats::{Subject, client::traits::Publisher, connection::State};
use pyo3::{
Bound, PyAny, Python, pyclass, pymethods,
types::{PyBytes, PyBytesMethods, PyDict},
};
use tokio::sync::RwLock;
use crate::{
exceptions::rust_err::{NatsrpyError, NatsrpyResult},
js::kv::{KVConfig, KeyValue},
utils::{headers::NatsrpyHeadermapExt, natsrpy_future},
};
#[pyclass]
pub struct JetStream {
ctx: Arc<RwLock<async_nats::jetstream::Context>>,
}
impl JetStream {
#[must_use]
pub fn new(ctx: async_nats::jetstream::Context) -> Self {
Self {
ctx: Arc::new(RwLock::new(ctx)),
}
}
}
#[pymethods]
impl JetStream {
#[pyo3(signature = (
subject,
payload,
*,
headers=None,
reply=None,
err_on_disconnect = false
))]
pub fn publish<'py>(
&self,
py: Python<'py>,
subject: String,
payload: &Bound<PyBytes>,
headers: Option<Bound<PyDict>>,
reply: Option<String>,
err_on_disconnect: bool,
) -> NatsrpyResult<Bound<'py, PyAny>> {
let ctx = self.ctx.clone();
let data = bytes::Bytes::from(payload.as_bytes().to_vec());
let headermap = headers
.map(async_nats::HeaderMap::from_pydict)
.transpose()?;
natsrpy_future(py, async move {
if err_on_disconnect
&& ctx.read().await.client().connection_state() == State::Disconnected
{
return Err(NatsrpyError::Disconnected);
}
ctx.read()
.await
.publish_message(async_nats::message::OutboundMessage {
subject: Subject::from(subject),
payload: data,
headers: headermap,
reply: reply.map(Subject::from),
})
.await?;
Ok(())
})
}
pub fn create_kv<'py>(
&self,
py: Python<'py>,
config: &Bound<'py, KVConfig>,
) -> NatsrpyResult<Bound<'py, PyAny>> {
let ctx = self.ctx.clone();
let config = config.borrow().deref().clone().try_into()?;
natsrpy_future(py, async move {
let js = ctx.read().await;
Ok(KeyValue::new(js.create_key_value(config).await?))
})
}
pub fn get_kv<'py>(&self, py: Python<'py>, bucket: String) -> NatsrpyResult<Bound<'py, PyAny>> {
let ctx = self.ctx.clone();
natsrpy_future(py, async move {
let js = ctx.read().await;
Ok(KeyValue::new(js.get_key_value(bucket).await?))
})
}
pub fn update_kv<'py>(
&self,
py: Python<'py>,
config: &Bound<'py, KVConfig>,
) -> NatsrpyResult<Bound<'py, PyAny>> {
let ctx = self.ctx.clone();
let config = config.borrow().deref().clone().try_into()?;
natsrpy_future(py, async move {
let js = ctx.read().await;
Ok(KeyValue::new(js.update_key_value(config).await?))
})
}
pub fn delete_kv<'py>(
&self,
py: Python<'py>,
bucket: String,
) -> NatsrpyResult<Bound<'py, PyAny>> {
let ctx = self.ctx.clone();
natsrpy_future(py, async move {
let js = ctx.read().await;
Ok(js.delete_key_value(bucket).await?.success)
})
}
pub fn get_consumer<'py>(
&self,
py: Python<'py>,
bucket: String,
) -> NatsrpyResult<Bound<'py, PyAny>> {
let ctx = self.ctx.clone();
natsrpy_future(py, async move {
let js = ctx.read().await;
Ok(KeyValue::new(js.get_key_value(bucket).await?))
})
}
}