nomad_client_rs/api/
service.rs1use reqwest::Method;
2
3use crate::api::service::models::{NamespacedService, ServiceParams};
4use crate::models::ServiceRegistration;
5use crate::{ClientError, NomadClient};
6
7impl NomadClient {
8 pub async fn service_get(
9 &self,
10 service_name: &str,
11 params: &ServiceParams,
12 ) -> Result<Vec<ServiceRegistration>, ClientError> {
13 let req = self
14 .request(Method::GET, &format!("/service/{}", service_name))
15 .query(params);
16
17 self.send::<Vec<ServiceRegistration>>(req).await
18 }
19
20 pub async fn service_list(
21 &self,
22 params: &ServiceParams,
23 ) -> Result<Vec<NamespacedService>, ClientError> {
24 let req = self.request(Method::GET, "/services").query(params);
25
26 self.send::<Vec<NamespacedService>>(req).await
27 }
28}
29
30pub mod models {
31 use serde::{Deserialize, Serialize};
32
33 use crate::models::ServiceRegistration;
34
35 #[derive(Clone, Debug, Default, Deserialize, Serialize)]
36 #[serde(rename_all = "PascalCase")]
37 pub struct NamespacedService {
38 pub namespace: Option<String>,
39 pub services: Vec<ServiceRegistration>,
40 }
41
42 #[derive(Debug, Serialize)]
43 #[serde(rename_all = "camelCase")]
44 pub struct ServiceParams {
45 pub namespace: Option<String>,
46 }
47
48 impl Default for ServiceParams {
49 fn default() -> Self {
50 ServiceParams {
51 namespace: Some("default".to_string()),
52 }
53 }
54 }
55}
56
57#[cfg(test)]
58mod tests {
59 use crate::api::service::models::ServiceParams;
60 use crate::NomadClient;
61
62 #[tokio::test]
63 #[ignore]
64 async fn test() {
65 let client = NomadClient::default();
66 let result = client.service_list(&ServiceParams::default()).await;
67
68 println!("{result:#?}")
69 }
70
71 #[tokio::test]
72 #[ignore]
73 async fn test2() {
74 let client = NomadClient::default();
75 let result = client
76 .service_get("nats-leaf-node", &ServiceParams::default())
77 .await;
78
79 println!("{result:#?}")
80 }
81}