openapi_rs/api/v1/rdp_go/
internal_rdp_go_execute_script.rs1use crate::common::define::{
2 AsyncResponseFn, BaseRequest, BaseResponse, HttpBuilder, HttpFn, RequestFn,
3};
4use bytes::Bytes;
5use reqwest::header::{HeaderMap, HeaderName, HeaderValue};
6use reqwest::{Method, Response};
7use serde::{Deserialize, Serialize};
8use std::collections::HashMap;
9
10#[derive(Debug, Default, Clone, Serialize, Deserialize)]
11#[serde(default)]
12pub struct InternalRdpGoExecuteScriptRequest {
13 #[serde(rename = "PrivateIP")]
14 pub private_ip: Option<String>,
15 #[serde(rename = "x-ys-request-id")]
16 pub request_id: Option<String>,
17 #[serde(rename = "ScriptRunner")]
18 pub script_runner: Option<String>,
19 #[serde(rename = "ScriptContentEncoded")]
20 pub script_content_encoded: Option<String>,
21 #[serde(rename = "WaitTillEnd")]
22 pub wait_till_end: Option<bool>,
23}
24
25impl InternalRdpGoExecuteScriptRequest {
26 pub fn new() -> Self {
27 Self::default()
28 }
29 pub fn with_private_ip(mut self, private_ip: String) -> Self {
30 self.private_ip = Some(private_ip);
31 self
32 }
33 pub fn with_request_id(mut self, request_id: String) -> Self {
34 self.request_id = Some(request_id);
35 self
36 }
37 pub fn with_script_runner(mut self, script_runner: String) -> Self {
38 self.script_runner = Some(script_runner);
39 self
40 }
41 pub fn with_script_content_encoded(mut self, script_content_encoded: String) -> Self {
42 self.script_content_encoded = Some(script_content_encoded);
43 self
44 }
45 pub fn with_wait_till_end(mut self, wait_till_end: bool) -> Self {
46 self.wait_till_end = Some(wait_till_end);
47 self
48 }
49}
50
51#[derive(Debug, Default, Clone, Serialize, Deserialize)]
52#[serde(default)]
53pub struct InternalRdpGoExecuteScriptResponse {
54 #[serde(rename = "ExitCode")]
55 pub exit_code: Option<isize>,
56 #[serde(rename = "Stdout")]
57 pub stdout: Option<String>,
58 #[serde(rename = "Stderr")]
59 pub stderr: Option<String>,
60}
61
62impl HttpBuilder for InternalRdpGoExecuteScriptRequest {
63 type Response = BaseResponse<InternalRdpGoExecuteScriptResponse>;
64 fn builder(self) -> HttpFn<Self::Response> {
65 Box::new(move || {
66 let request_fn: RequestFn = Box::new(move || {
67 let mut headers = HeaderMap::new();
68 if let Some(ref request_id) = self.request_id {
69 headers.insert(
70 HeaderName::from_bytes("x-ys-request-id".as_bytes()).unwrap(),
71 HeaderValue::from_str(request_id).unwrap(),
72 );
73 }
74 let mut queries = HashMap::new();
75 if let Some(ref private_ip) = self.private_ip {
76 queries.insert("PrivateIP".to_string(), private_ip.clone());
77 }
78 BaseRequest {
79 method: Method::POST,
80 uri: "/internal/execScript".to_string(),
81 headers,
82 queries: Some(queries),
83 body: Bytes::from(serde_json::to_vec(&self).unwrap()),
84 ..Default::default()
85 }
86 });
87 let response_fn: AsyncResponseFn<Self::Response> =
88 Box::new(|response: Response| Box::pin(async move { Ok(response.json().await?) }));
89 (request_fn, response_fn)
90 })
91 }
92}
93
94#[cfg(test)]
95mod tests {
96 use super::*;
97 use crate::common::client::OpenApiClient;
98 use crate::common::config::OpenApiConfig;
99 use tracing::info;
100
101 #[tokio::test]
102 async fn test_internal_rdp_go_execute_script() -> anyhow::Result<()> {
103 tracing_subscriber::fmt::init();
104 dotenvy::dotenv()?;
105 let config = OpenApiConfig::new().load_from_env()?;
106 let mut client = OpenApiClient::new(config);
107
108 let http_fn = InternalRdpGoExecuteScriptRequest::new()
109 .with_private_ip("123".to_string())
110 .builder();
111 let response = client.send(http_fn).await?;
112 info!("response: {:#?}", response);
113
114 Ok(())
115 }
116}