nomad_client_rs/api/
client.rs

1use reqwest::Method;
2
3use crate::api::client::models::{
4    ClientMetadataResponse, ClientReadMetadataParams, ClientUpdateMetadataRequest,
5};
6use crate::models::alloc_file_info::AllocFileInfo;
7use crate::{ClientError, NomadClient};
8
9impl NomadClient {
10    pub async fn client_list_files(
11        &self,
12        alloc_id: &str,
13        path: Option<String>,
14    ) -> Result<Vec<AllocFileInfo>, ClientError> {
15        let req = self.request(
16            Method::GET,
17            &format!(
18                "/client/fs/ls/{}?path={}",
19                alloc_id,
20                path.unwrap_or("/".into())
21            ),
22        );
23
24        self.send::<Vec<AllocFileInfo>>(req).await
25    }
26
27    pub async fn client_get_file(
28        &self,
29        alloc_id: &str,
30        file_path: &str,
31    ) -> Result<String, ClientError> {
32        let req = self.request(
33            Method::GET,
34            &format!("/client/fs/cat/{}?path={}", alloc_id, file_path),
35        );
36
37        self.send_plain(req).await
38    }
39
40    pub async fn client_get_file_info(
41        &self,
42        alloc_id: &str,
43        file_path: &str,
44    ) -> Result<AllocFileInfo, ClientError> {
45        let req = self.request(
46            Method::GET,
47            &format!("/client/fs/stat/{}?path={}", alloc_id, file_path),
48        );
49
50        self.send::<AllocFileInfo>(req).await
51    }
52
53    pub async fn client_read_metadata(
54        &self,
55        params: &ClientReadMetadataParams,
56    ) -> Result<ClientMetadataResponse, ClientError> {
57        let req = self.request(Method::GET, "/client/metadata").query(params);
58
59        self.send::<ClientMetadataResponse>(req).await
60    }
61
62    pub async fn client_update_metadata(
63        &self,
64        req: &ClientUpdateMetadataRequest,
65    ) -> Result<ClientMetadataResponse, ClientError> {
66        let req = self.request(Method::POST, "/client/metadata").json(req);
67
68        self.send::<ClientMetadataResponse>(req).await
69    }
70}
71
72pub mod models {
73    use std::collections::HashMap;
74
75    use serde::{Deserialize, Serialize};
76
77    #[derive(Debug, Deserialize)]
78    #[serde(rename_all = "PascalCase")]
79    pub struct ClientMetadataResponse {
80        pub meta: Option<HashMap<String, Option<String>>>,
81        pub dynamic: Option<HashMap<String, Option<String>>>,
82        pub r#static: Option<HashMap<String, Option<String>>>,
83    }
84
85    #[derive(Debug, Default, Serialize)]
86    #[serde(rename_all = "camelCase")]
87    pub struct ClientReadMetadataParams {
88        #[serde(rename = "node_id")]
89        pub node_id: Option<String>,
90    }
91
92    #[derive(Debug, Default, Serialize)]
93    #[serde(rename_all = "camelCase")]
94    pub struct ClientUpdateMetadataRequest {
95        pub node_id: Option<String>,
96        pub meta: HashMap<String, Option<String>>,
97    }
98}
99
100#[cfg(test)]
101mod tests {
102    // use std::collections::HashMap;
103    //
104    // use crate::api::client::models::{ClientReadMetadataParams, ClientUpdateMetadataRequest};
105    // use crate::NomadClient;
106    //
107    // #[tokio::test]
108    // pub async fn test_client_update_metadata() {
109    //     let client = NomadClient::default();
110    //
111    //     let body = ClientUpdateMetadataRequest {
112    //         meta: HashMap::from([("foo".to_string(), Some("bar".to_string()))]),
113    //         ..ClientUpdateMetadataRequest::default()
114    //     };
115    //
116    //     match client.client_update_metadata(&body).await {
117    //         Ok(resp) => {
118    //             println!("{:#?}", resp);
119    //         }
120    //         Err(e) => panic!("{:#?}", e),
121    //     }
122    // }
123    //
124    // #[tokio::test]
125    // pub async fn test_client_read_metadata() {
126    //     let client = NomadClient::default();
127    //
128    //     let params = ClientReadMetadataParams {
129    //         ..ClientReadMetadataParams::default()
130    //     };
131    //
132    //     match client.client_read_metadata(&params).await {
133    //         Ok(resp) => {
134    //             println!("{:#?}", resp);
135    //         }
136    //         Err(e) => panic!("{:#?}", e),
137    //     }
138    // }
139}