ring_client/client/wrapper/
device.rs

1use crate::client::api::device::Device;
2use crate::client::api::ApiError;
3use crate::Client;
4
5impl Client {
6    /// Retrieve a list of devices in the Ring account.
7    ///
8    /// # Examples
9    ///
10    /// ```no_run
11    /// use ring_client::Client;
12    ///
13    /// use ring_client::authentication::Credentials;
14    /// use ring_client::OperatingSystem;
15    ///
16    /// # tokio_test::block_on(async {
17    /// let client = Client::new("Home Automation", "mock-system-id", OperatingSystem::Ios);
18    ///
19    /// // For berevity, a Refresh Token is being used here. However, the client can also
20    /// // be authenticated using a username and password.
21    /// //
22    /// // See `Client::login` for more information.
23    /// let refresh_token = Credentials::RefreshToken("".to_string());
24    ///
25    /// client.login(refresh_token)
26    ///      .await
27    ///      .expect("Logging in with a valid refresh token should not fail");
28    ///
29    /// let devices = client.get_devices()
30    ///      .await
31    ///      .expect("Getting devices not fail");
32    ///
33    /// println!("{:#?}", devices);
34    /// # });
35    ///```
36    ///
37    /// # Errors
38    ///
39    /// Returns an error if the API request fails. This may occur either as the result of an API
40    /// error, or if the authentication token needs to be refreshed and it is not successful.
41    pub async fn get_devices(&self) -> Result<Vec<Device>, ApiError> {
42        self.api
43            .get_device_data(
44                &*self
45                    .refresh_tokens_if_needed()
46                    .await
47                    .map_err(ApiError::AuthenticationRefreshFailed)?,
48            )
49            .await
50            .map(|data| data.into_iter().map(Device::new).collect())
51    }
52}