feat(base): Add a From login response implementation for the Session

This commit is contained in:
Damir Jelić
2021-11-19 09:41:48 +01:00
parent 6606703001
commit 5a4aa71f6a
2 changed files with 32 additions and 0 deletions

View File

@@ -44,3 +44,13 @@ pub struct Session {
/// The ID of the client device
pub device_id: DeviceIdBox,
}
impl From<ruma::api::client::r0::session::login::Response> for Session {
fn from(response: ruma::api::client::r0::session::login::Response) -> Self {
Self {
access_token: response.access_token,
user_id: response.user_id,
device_id: response.device_id,
}
}
}

View File

@@ -1213,6 +1213,28 @@ impl Client {
/// # anyhow::Result::<()>::Ok(()) });
/// ```
///
/// The `Session` object can also be created from the response the
/// [`Client::login()`] method returns:
///
/// ```no_run
/// use matrix_sdk::{Client, Session, ruma::{DeviceIdBox, user_id}};
/// # use url::Url;
/// # use futures::executor::block_on;
/// # block_on(async {
///
/// let homeserver = Url::parse("http://example.com")?;
/// let client = Client::new(homeserver)?;
///
/// let session: Session = client
/// .login("example", "my-password", None, None)
/// .await?
/// .into();
///
/// // Persist the `Session` so it can later be used to restore the login.
/// // client.restore_session(session).await?;
/// # anyhow::Result::<()>::Ok(()) });
/// ```
///
/// [`login`]: #method.login
pub async fn restore_login(&self, session: Session) -> Result<()> {
Ok(self.base_client.restore_login(session).await?)