Skip to main content

uptrakit_openapi_client/
system_services.rs

1use crate::Result;
2use crate::UptrakitClient;
3use crate::types_impl::batch_actions::{BatchActionRequest, BatchActionResponse};
4use crate::types_impl::pagination::PaginatedResponse;
5use crate::types_impl::system_services::{
6    ListSystemServicesQuery, SystemServiceResponse, UpdateSystemServiceRequest,
7};
8use uuid::Uuid;
9
10impl UptrakitClient {
11    /// List system services with optional filters and pagination.
12    pub async fn list_system_services(
13        &self,
14        query: &ListSystemServicesQuery,
15    ) -> Result<PaginatedResponse<SystemServiceResponse>> {
16        self.get_with_query(crate::paths::system_services::BASE, query)
17            .await
18    }
19
20    /// Fetch all system services matching the given filters across all pages.
21    ///
22    /// Automatically iterates through every page at [`MAX_PER_PAGE`] items per
23    /// request. The `page` and `per_page` fields of `query` are ignored; use
24    /// [`list_system_services`] for manual pagination control.
25    ///
26    /// [`MAX_PER_PAGE`]: uptrakit_web_api_types::pagination::MAX_PER_PAGE
27    /// [`list_system_services`]: Self::list_system_services
28    pub async fn list_all_system_services(
29        &self,
30        query: &ListSystemServicesQuery,
31    ) -> Result<Vec<SystemServiceResponse>> {
32        self.fetch_all_pages(crate::paths::system_services::BASE, query)
33            .await
34    }
35
36    /// Get a single system service by ID.
37    pub async fn get_system_service(&self, id: &Uuid) -> Result<SystemServiceResponse> {
38        self.get(&crate::paths::system_services::by_id(id)).await
39    }
40
41    /// Approve a pending system service.
42    pub async fn approve_system_service(&self, id: &Uuid) -> Result<SystemServiceResponse> {
43        self.post_empty(&crate::paths::system_services::approve(id))
44            .await
45    }
46
47    /// Reject a pending system service.
48    pub async fn reject_system_service(&self, id: &Uuid) -> Result<SystemServiceResponse> {
49        self.post_empty(&crate::paths::system_services::reject(id))
50            .await
51    }
52
53    /// Update a system service's configurable settings (e.g. ping interval).
54    pub async fn update_system_service(
55        &self,
56        id: &Uuid,
57        req: &UpdateSystemServiceRequest,
58    ) -> Result<SystemServiceResponse> {
59        self.put_json(&crate::paths::system_services::by_id(id), req)
60            .await
61    }
62
63    /// Deactivate (remove) a system service.
64    pub async fn remove_system_service(&self, id: &Uuid) -> Result<()> {
65        self.delete(&crate::paths::system_services::by_id(id)).await
66    }
67
68    /// Perform a batch action on multiple system services.
69    ///
70    /// Supported actions: `approve`, `reject`, `deactivate`.
71    pub async fn batch_system_services(
72        &self,
73        req: &BatchActionRequest,
74    ) -> Result<BatchActionResponse> {
75        self.post_json(crate::paths::system_services::BATCH, req)
76            .await
77    }
78}
79
80#[cfg(test)]
81mod tests {
82    use crate::shared_types_impl::ServiceStatus;
83    use crate::types_impl::system_services::{ListSystemServicesQuery, UpdateSystemServiceRequest};
84
85    #[test]
86    fn list_system_services_query_serialization_with_all_fields() {
87        let query = ListSystemServicesQuery {
88            capability: Some("update_tracking".to_string()),
89            status: Some(ServiceStatus::Approved),
90            page: Some(2),
91            per_page: Some(50),
92        };
93        let qs = serde_urlencoded::to_string(&query).expect("serialize");
94        assert!(qs.contains("capability=update_tracking"));
95        assert!(qs.contains("status=approved"));
96        assert!(qs.contains("page=2"));
97        assert!(qs.contains("per_page=50"));
98    }
99
100    #[test]
101    fn list_system_services_query_serialization_skips_none() {
102        let query = ListSystemServicesQuery {
103            capability: None,
104            status: None,
105            page: None,
106            per_page: None,
107        };
108        let qs = serde_urlencoded::to_string(&query).expect("serialize");
109        assert!(qs.is_empty());
110    }
111
112    #[test]
113    fn update_system_service_request_cert_lifetime_hours_round_trip() {
114        let req = UpdateSystemServiceRequest {
115            ping_interval_seconds: None,
116            cert_lifetime_hours: Some(48),
117        };
118        let json = serde_json::to_string(&req).expect("serialize");
119        assert!(json.contains(r#""cert_lifetime_hours":48"#));
120        let parsed: UpdateSystemServiceRequest = serde_json::from_str(&json).expect("deserialize");
121        assert_eq!(parsed.cert_lifetime_hours, Some(48));
122    }
123
124    #[test]
125    fn update_system_service_request_omits_none_fields() {
126        let req = UpdateSystemServiceRequest {
127            ping_interval_seconds: None,
128            cert_lifetime_hours: None,
129        };
130        let json = serde_json::to_string(&req).expect("serialize");
131        assert!(!json.contains("ping_interval_seconds"));
132        assert!(!json.contains("cert_lifetime_hours"));
133    }
134}