ring_client/client/wrapper/
location.rs

1use crate::client::api;
2use crate::client::location::Location;
3use crate::Client;
4
5impl Client {
6    /// Retrieve a list of locations 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 locations = client.get_locations()
30    ///      .await
31    ///      .expect("Getting locations should not fail");
32    ///
33    /// let location = locations
34    ///      .first()
35    ///      .expect("There should be at least one location");
36    /// # });
37    /// ```
38    ///
39    /// # Errors
40    ///
41    /// Returns an error if the API request fails. This may occur either as the result of an API
42    /// error, or if the authentication token needs to be refreshed and it is not successful.
43    pub async fn get_locations(&self) -> Result<Vec<Location<'_>>, api::ApiError> {
44        Ok(self
45            .api
46            .get_location_data(
47                &*self
48                    .refresh_tokens_if_needed()
49                    .await
50                    .map_err(api::ApiError::AuthenticationRefreshFailed)?,
51            )
52            .await?
53            .into_iter()
54            .map(|data| Location::new(self, data))
55            .collect())
56    }
57}