ros_core_rs/
client_api.rs

1use dxr::Value;
2use dxr_client::{Client, ClientBuilder, Url};
3
4pub struct ClientApi {
5    client: Client,
6}
7
8impl ClientApi {
9    /// Creates a new `ClientApi` instance with the given URI.
10    ///
11    /// # Arguments
12    ///
13    /// * `uri` - A string slice representing the URI of the client API.
14    ///
15    /// # Returns
16    ///
17    /// A new `ClientApi` instance.
18    pub fn new(uri: &str) -> Self {
19        // Parse the URI and create a new `Client` instance.
20        let url = Url::parse(uri).expect("Failed to parse client-api URL.");
21        let client = ClientBuilder::new(url.clone())
22            .user_agent("ros-core-rs-client-api")
23            .build();
24        Self { client }
25    }
26
27    /// Sends a "publisherUpdate" request to the ROS node.
28    ///
29    /// # Arguments
30    ///
31    /// * `caller_id` - A string slice representing the ID of the caller.
32    /// * `topic` - A string slice representing the name of the topic.
33    /// * `publisher_apis` - A vector of string slices representing the API URLs of the publishers.
34    ///
35    /// # Returns
36    ///
37    /// An `anyhow::Result` indicating whether the request was successful.
38    pub async fn publisher_update(
39        &self,
40        caller_id: &str,
41        topic: &str,
42        publisher_apis: &Vec<String>,
43    ) -> anyhow::Result<Value> {
44        let result = self.client.call::<_, _>("publisherUpdate", (caller_id, topic, publisher_apis)).await;
45        
46        Ok(result?)
47    }
48
49    /// Sends a "paramUpdate" request to the ROS node.
50    ///
51    /// # Arguments
52    ///
53    /// * `caller_id` - A string slice representing the ID of the caller.
54    /// * `key` - A string slice representing the name of the parameter.
55    /// * `value` - A `Value` representing the new value of the parameter.
56    ///
57    /// # Returns
58    ///
59    /// An `anyhow::Result` indicating whether the request was successful.
60    pub async fn param_update(
61        &self,
62        caller_id: &str,
63        key: &str,
64        value: &Value,
65    ) -> anyhow::Result<Value> {
66        let result = self.client.call("paramUpdate", (caller_id, key, value)).await;
67        Ok(result?)
68    }
69
70    /// Requests the node to shut down
71    ///
72    /// # Arguments
73    ///
74    /// * `caller_id` - A string slice representing the ID of the caller.
75    /// * `reason` - Reason for shutting the node down. Will likely show up in logs.
76    ///
77    /// # Returns
78    ///
79    /// An `anyhow::Result` indicating whether the request was successful.
80
81    pub async fn shutdown(
82        &self,
83        caller_id: &str,
84        reason: &str,
85    ) -> anyhow::Result<()> {
86        let result = self.client.call("shutdown", (caller_id, reason)).await;
87        Ok(result?)
88    }
89}