Skip to main content

uptrakit_openapi_client/
hosts.rs

1use crate::Result;
2use crate::UptrakitClient;
3use crate::types_impl::batch_actions::{BatchActionRequest, BatchActionResponse};
4use crate::types_impl::hosts::{HostResponse, UpdateHostRequest};
5use crate::types_impl::pagination::{PaginatedResponse, PaginationParams};
6use uuid::Uuid;
7
8impl UptrakitClient {
9    /// List hosts with pagination.
10    pub async fn list_hosts(
11        &self,
12        params: &PaginationParams,
13    ) -> Result<PaginatedResponse<HostResponse>> {
14        self.get_with_query(crate::paths::hosts::BASE, params).await
15    }
16
17    /// Fetch all hosts across all pages.
18    ///
19    /// Automatically iterates through every page at [`MAX_PER_PAGE`] items per
20    /// request and returns the concatenated list. Use [`list_hosts`] when
21    /// manual pagination control is needed.
22    ///
23    /// [`MAX_PER_PAGE`]: uptrakit_web_api_types::pagination::MAX_PER_PAGE
24    /// [`list_hosts`]: Self::list_hosts
25    pub async fn list_all_hosts(&self) -> Result<Vec<HostResponse>> {
26        let base = PaginationParams {
27            page: None,
28            per_page: None,
29        };
30        self.fetch_all_pages(crate::paths::hosts::BASE, &base).await
31    }
32
33    /// Get a single host by ID.
34    pub async fn get_host(&self, id: &Uuid) -> Result<HostResponse> {
35        self.get(&crate::paths::hosts::by_id(id)).await
36    }
37
38    /// Update a host (e.g. change its friendly name).
39    pub async fn update_host(&self, id: &Uuid, req: &UpdateHostRequest) -> Result<HostResponse> {
40        self.put_json(&crate::paths::hosts::by_id(id), req).await
41    }
42
43    /// Deactivate (remove) a host.
44    pub async fn deactivate_host(&self, id: &Uuid) -> Result<()> {
45        self.delete(&crate::paths::hosts::by_id(id)).await
46    }
47
48    /// Perform a batch action on multiple hosts.
49    ///
50    /// Supported actions: `deactivate`.
51    pub async fn batch_hosts(&self, req: &BatchActionRequest) -> Result<BatchActionResponse> {
52        self.post_json(crate::paths::hosts::BATCH, req).await
53    }
54}
55
56#[cfg(test)]
57mod tests {
58    use crate::types_impl::hosts::UpdateHostRequest;
59    use crate::types_impl::pagination::PaginationParams;
60
61    #[test]
62    fn pagination_params_serialization_with_values() {
63        let params = PaginationParams {
64            page: Some(2),
65            per_page: Some(50),
66        };
67        let qs = serde_urlencoded::to_string(&params).expect("serialize");
68        assert!(qs.contains("page=2"));
69        assert!(qs.contains("per_page=50"));
70    }
71
72    #[test]
73    fn pagination_params_serialization_skips_none() {
74        let params = PaginationParams {
75            page: None,
76            per_page: None,
77        };
78        let qs = serde_urlencoded::to_string(&params).expect("serialize");
79        assert!(qs.is_empty());
80    }
81
82    #[test]
83    fn update_host_request_serialization() {
84        let req = UpdateHostRequest {
85            friendly_name: Some("Production Server".to_string()),
86        };
87        let json = serde_json::to_value(&req).expect("serialize");
88        assert_eq!(json["friendly_name"], "Production Server");
89    }
90
91    #[test]
92    fn update_host_request_serialization_none() {
93        let req = UpdateHostRequest {
94            friendly_name: None,
95        };
96        let json = serde_json::to_value(&req).expect("serialize");
97        assert!(json["friendly_name"].is_null());
98    }
99}