nomad_client_rs/api/
client.rs

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
132
133
134
135
136
137
138
139
use reqwest::Method;

use crate::api::client::models::{
    ClientMetadataResponse, ClientReadMetadataParams, ClientUpdateMetadataRequest,
};
use crate::models::alloc_file_info::AllocFileInfo;
use crate::{ClientError, NomadClient};

impl NomadClient {
    pub async fn client_list_files(
        &self,
        alloc_id: &str,
        path: Option<String>,
    ) -> Result<Vec<AllocFileInfo>, ClientError> {
        let req = self.request(
            Method::GET,
            &format!(
                "/client/fs/ls/{}?path={}",
                alloc_id,
                path.unwrap_or("/".into())
            ),
        );

        self.send::<Vec<AllocFileInfo>>(req).await
    }

    pub async fn client_get_file(
        &self,
        alloc_id: &str,
        file_path: &str,
    ) -> Result<String, ClientError> {
        let req = self.request(
            Method::GET,
            &format!("/client/fs/cat/{}?path={}", alloc_id, file_path),
        );

        self.send_plain(req).await
    }

    pub async fn client_get_file_info(
        &self,
        alloc_id: &str,
        file_path: &str,
    ) -> Result<AllocFileInfo, ClientError> {
        let req = self.request(
            Method::GET,
            &format!("/client/fs/stat/{}?path={}", alloc_id, file_path),
        );

        self.send::<AllocFileInfo>(req).await
    }

    pub async fn client_read_metadata(
        &self,
        params: &ClientReadMetadataParams,
    ) -> Result<ClientMetadataResponse, ClientError> {
        let req = self.request(Method::GET, "/client/metadata").query(params);

        self.send::<ClientMetadataResponse>(req).await
    }

    pub async fn client_update_metadata(
        &self,
        req: &ClientUpdateMetadataRequest,
    ) -> Result<ClientMetadataResponse, ClientError> {
        let req = self.request(Method::POST, "/client/metadata").json(req);

        self.send::<ClientMetadataResponse>(req).await
    }
}

pub mod models {
    use std::collections::HashMap;

    use serde::{Deserialize, Serialize};

    #[derive(Debug, Deserialize)]
    #[serde(rename_all = "PascalCase")]
    pub struct ClientMetadataResponse {
        pub meta: Option<HashMap<String, Option<String>>>,
        pub dynamic: Option<HashMap<String, Option<String>>>,
        pub r#static: Option<HashMap<String, Option<String>>>,
    }

    #[derive(Debug, Default, Serialize)]
    #[serde(rename_all = "camelCase")]
    pub struct ClientReadMetadataParams {
        #[serde(rename = "node_id")]
        pub node_id: Option<String>,
    }

    #[derive(Debug, Default, Serialize)]
    #[serde(rename_all = "camelCase")]
    pub struct ClientUpdateMetadataRequest {
        pub node_id: Option<String>,
        pub meta: HashMap<String, Option<String>>,
    }
}

#[cfg(test)]
mod tests {
    // use std::collections::HashMap;
    //
    // use crate::api::client::models::{ClientReadMetadataParams, ClientUpdateMetadataRequest};
    // use crate::NomadClient;
    //
    // #[tokio::test]
    // pub async fn test_client_update_metadata() {
    //     let client = NomadClient::default();
    //
    //     let body = ClientUpdateMetadataRequest {
    //         meta: HashMap::from([("foo".to_string(), Some("bar".to_string()))]),
    //         ..ClientUpdateMetadataRequest::default()
    //     };
    //
    //     match client.client_update_metadata(&body).await {
    //         Ok(resp) => {
    //             println!("{:#?}", resp);
    //         }
    //         Err(e) => panic!("{:#?}", e),
    //     }
    // }
    //
    // #[tokio::test]
    // pub async fn test_client_read_metadata() {
    //     let client = NomadClient::default();
    //
    //     let params = ClientReadMetadataParams {
    //         ..ClientReadMetadataParams::default()
    //     };
    //
    //     match client.client_read_metadata(&params).await {
    //         Ok(resp) => {
    //             println!("{:#?}", resp);
    //         }
    //         Err(e) => panic!("{:#?}", e),
    //     }
    // }
}