decthings_api/client/rpc/terminal/
mod.rs1mod request;
2mod response;
3
4use crate::client::StateModification;
5
6pub use request::*;
7pub use response::*;
8
9pub struct TerminalRpc {
10 rpc: crate::client::DecthingsClientRpc,
11}
12
13impl TerminalRpc {
14 pub(crate) fn new(rpc: crate::client::DecthingsClientRpc) -> Self {
15 Self { rpc }
16 }
17
18 pub async fn launch_terminal_session(
19 &self,
20 params: LaunchTerminalSessionParams<'_>,
21 ) -> Result<
22 LaunchTerminalSessionResult,
23 crate::client::DecthingsRpcError<LaunchTerminalSessionError>,
24 > {
25 #[cfg(feature = "events")]
26 let subscribe_to_events = params.subscribe_to_events != Some(false);
27
28 #[cfg(feature = "events")]
29 let protocol = if subscribe_to_events {
30 crate::client::RpcProtocol::Ws
31 } else {
32 crate::client::RpcProtocol::Http
33 };
34
35 #[cfg(not(feature = "events"))]
36 let protocol = crate::client::RpcProtocol::Http;
37
38 let (tx, rx) = tokio::sync::oneshot::channel();
39 self.rpc
40 .raw_method_call::<_, _, &[u8]>(
41 "Terminal",
42 "launchTerminalSession",
43 params,
44 &[],
45 protocol,
46 move |x| {
47 match x {
48 Ok(val) => {
49 let res: Result<
50 super::Response<
51 LaunchTerminalSessionResult,
52 LaunchTerminalSessionError,
53 >,
54 crate::client::DecthingsRpcError<LaunchTerminalSessionError>,
55 > = serde_json::from_slice(&val.0).map_err(Into::into);
56 match res {
57 Ok(super::Response::Result(val)) => {
58 #[cfg(feature = "events")]
59 let terminal_session_id = val.terminal_session_id.clone();
60
61 tx.send(Ok(val)).ok();
62
63 #[cfg(feature = "events")]
64 if subscribe_to_events {
65 return StateModification {
66 add_events: vec![terminal_session_id],
67 remove_events: vec![],
68 };
69 }
70 }
71 Ok(super::Response::Error(val)) => {
72 tx.send(Err(crate::client::DecthingsRpcError::Rpc(val)))
73 .ok();
74 }
75 Err(e) => {
76 tx.send(Err(e)).ok();
77 }
78 }
79 }
80 Err(err) => {
81 tx.send(Err(err.into())).ok();
82 }
83 }
84 StateModification::empty()
85 },
86 )
87 .await;
88 rx.await.unwrap()
89 }
90
91 pub async fn terminate_terminal_session(
92 &self,
93 params: TerminateTerminalSessionParams<'_>,
94 ) -> Result<
95 TerminateTerminalSessionResult,
96 crate::client::DecthingsRpcError<TerminateTerminalSessionError>,
97 > {
98 #[cfg(feature = "events")]
99 let terminal_session_id_owned = params.terminal_session_id.to_owned();
100
101 let (tx, rx) = tokio::sync::oneshot::channel();
102 self.rpc
103 .raw_method_call::<_, _, &[u8]>(
104 "Terminal",
105 "terminateTerminalSession",
106 params,
107 &[],
108 crate::client::RpcProtocol::Http,
109 move |x| {
110 match x {
111 Ok(val) => {
112 let res: Result<
113 super::Response<
114 TerminateTerminalSessionResult,
115 TerminateTerminalSessionError,
116 >,
117 crate::client::DecthingsRpcError<TerminateTerminalSessionError>,
118 > = serde_json::from_slice(&val.0).map_err(Into::into);
119 match res {
120 Ok(super::Response::Result(val)) => {
121 tx.send(Ok(val)).ok();
122
123 #[cfg(feature = "events")]
124 return StateModification {
125 add_events: vec![],
126 remove_events: vec![terminal_session_id_owned],
127 };
128 }
129 Ok(super::Response::Error(val)) => {
130 tx.send(Err(crate::client::DecthingsRpcError::Rpc(val)))
131 .ok();
132 }
133 Err(e) => {
134 tx.send(Err(e)).ok();
135 }
136 }
137 }
138 Err(err) => {
139 tx.send(Err(err.into())).ok();
140 }
141 }
142 StateModification::empty()
143 },
144 )
145 .await;
146 rx.await.unwrap()
147 }
148
149 pub async fn get_terminal_sessions(
150 &self,
151 params: GetTerminalSessionsParams<'_, impl AsRef<str>>,
152 ) -> Result<GetTerminalSessionsResult, crate::client::DecthingsRpcError<GetTerminalSessionsError>>
153 {
154 let (tx, rx) = tokio::sync::oneshot::channel();
155 self.rpc
156 .raw_method_call::<_, _, &[u8]>(
157 "Terminal",
158 "getTerminalSessions",
159 params,
160 &[],
161 crate::client::RpcProtocol::Http,
162 |x| {
163 tx.send(x).ok();
164 StateModification::empty()
165 },
166 )
167 .await;
168 rx.await
169 .unwrap()
170 .map_err(crate::client::DecthingsRpcError::Request)
171 .and_then(|x| {
172 let res: super::Response<GetTerminalSessionsResult, GetTerminalSessionsError> =
173 serde_json::from_slice(&x.0)?;
174 match res {
175 super::Response::Result(val) => Ok(val),
176 super::Response::Error(val) => Err(crate::client::DecthingsRpcError::Rpc(val)),
177 }
178 })
179 }
180
181 pub async fn write_to_terminal_session(
182 &self,
183 params: WriteToTerminalSessionParams<'_, impl AsRef<[u8]>>,
184 ) -> Result<
185 WriteToTerminalSessionResult,
186 crate::client::DecthingsRpcError<WriteToTerminalSessionError>,
187 > {
188 let (tx, rx) = tokio::sync::oneshot::channel();
189 self.rpc
190 .raw_method_call(
191 "Terminal",
192 "writeToTerminalSession",
193 ¶ms,
194 &[¶ms.data],
195 crate::client::RpcProtocol::Http,
196 |x| {
197 tx.send(x).ok();
198 StateModification::empty()
199 },
200 )
201 .await;
202 rx.await
203 .unwrap()
204 .map_err(crate::client::DecthingsRpcError::Request)
205 .and_then(|x| {
206 let res: super::Response<
207 WriteToTerminalSessionResult,
208 WriteToTerminalSessionError,
209 > = serde_json::from_slice(&x.0)?;
210 match res {
211 super::Response::Result(val) => Ok(val),
212 super::Response::Error(val) => Err(crate::client::DecthingsRpcError::Rpc(val)),
213 }
214 })
215 }
216
217 pub async fn resize_terminal_session(
218 &self,
219 params: ResizeTerminalSessionParams<'_>,
220 ) -> Result<
221 ResizeTerminalSessionResult,
222 crate::client::DecthingsRpcError<ResizeTerminalSessionError>,
223 > {
224 let (tx, rx) = tokio::sync::oneshot::channel();
225 self.rpc
226 .raw_method_call::<_, _, &[u8]>(
227 "Terminal",
228 "resizeTerminalSession",
229 params,
230 &[],
231 crate::client::RpcProtocol::Http,
232 |x| {
233 tx.send(x).ok();
234 StateModification::empty()
235 },
236 )
237 .await;
238 rx.await
239 .unwrap()
240 .map_err(crate::client::DecthingsRpcError::Request)
241 .and_then(|x| {
242 let res: super::Response<ResizeTerminalSessionResult, ResizeTerminalSessionError> =
243 serde_json::from_slice(&x.0)?;
244 match res {
245 super::Response::Result(val) => Ok(val),
246 super::Response::Error(val) => Err(crate::client::DecthingsRpcError::Rpc(val)),
247 }
248 })
249 }
250
251 #[cfg(feature = "events")]
252 pub async fn subscribe_to_events(
253 &self,
254 params: TerminalSubscribeToEventsParams<'_>,
255 ) -> Result<
256 TerminalSubscribeToEventsResult,
257 crate::client::DecthingsRpcError<TerminalSubscribeToEventsError>,
258 > {
259 let (tx, rx) = tokio::sync::oneshot::channel();
260 let spawned_command_id_owned = params.terminal_session_id.to_owned();
261 self.rpc
262 .raw_method_call::<_, _, &[u8]>(
263 "Terminal",
264 "subscribeToEvents",
265 params,
266 &[],
267 crate::client::RpcProtocol::Ws,
268 move |x| {
269 match x {
270 Ok(val) => {
271 let res: Result<
272 super::Response<
273 TerminalSubscribeToEventsResult,
274 TerminalSubscribeToEventsError,
275 >,
276 crate::client::DecthingsRpcError<TerminalSubscribeToEventsError>,
277 > = serde_json::from_slice(&val.0).map_err(Into::into);
278 match res {
279 Ok(super::Response::Result(val)) => {
280 tx.send(Ok(val)).ok();
281 return StateModification {
282 add_events: vec![spawned_command_id_owned],
283 remove_events: vec![],
284 };
285 }
286 Ok(super::Response::Error(val)) => {
287 tx.send(Err(crate::client::DecthingsRpcError::Rpc(val)))
288 .ok();
289 }
290 Err(e) => {
291 tx.send(Err(e)).ok();
292 }
293 }
294 }
295 Err(err) => {
296 tx.send(Err(err.into())).ok();
297 }
298 }
299 StateModification::empty()
300 },
301 )
302 .await;
303 rx.await.unwrap()
304 }
305
306 #[cfg(feature = "events")]
307 pub async fn unsubscribe_from_events(
308 &self,
309 params: TerminalUnsubscribeFromEventsParams<'_>,
310 ) -> Result<
311 TerminalUnsubscribeFromEventsResult,
312 crate::client::DecthingsRpcError<TerminalUnsubscribeFromEventsError>,
313 > {
314 let (tx, rx) = tokio::sync::oneshot::channel();
315 let spawned_command_id_owned = params.terminal_session_id.to_owned();
316 let did_call = self
317 .rpc
318 .raw_method_call::<_, _, &[u8]>(
319 "Terminal",
320 "unsubscribeFromEvents",
321 params,
322 &[],
323 crate::client::RpcProtocol::WsIfAvailableOtherwiseNone,
324 move |x| {
325 match x {
326 Ok(val) => {
327 let res: Result<
328 super::Response<
329 TerminalUnsubscribeFromEventsResult,
330 TerminalUnsubscribeFromEventsError,
331 >,
332 crate::client::DecthingsRpcError<
333 TerminalUnsubscribeFromEventsError,
334 >,
335 > = serde_json::from_slice(&val.0).map_err(Into::into);
336 match res {
337 Ok(super::Response::Result(val)) => {
338 tx.send(Ok(val)).ok();
339 return StateModification {
340 add_events: vec![],
341 remove_events: vec![spawned_command_id_owned],
342 };
343 }
344 Ok(super::Response::Error(val)) => {
345 tx.send(Err(crate::client::DecthingsRpcError::Rpc(val)))
346 .ok();
347 }
348 Err(e) => {
349 tx.send(Err(e)).ok();
350 }
351 }
352 }
353 Err(err) => {
354 tx.send(Err(err.into())).ok();
355 }
356 }
357 StateModification::empty()
358 },
359 )
360 .await;
361 if !did_call {
362 return Err(crate::client::DecthingsRpcError::Rpc(
363 TerminalUnsubscribeFromEventsError::NotSubscribed,
364 ));
365 }
366 rx.await.unwrap()
367 }
368}