Skip to content

Commit 5a4aa71

Browse files
committed
feat(base): Add a From login response implementation for the Session
1 parent 6606703 commit 5a4aa71

File tree

2 files changed

+32
-0
lines changed

2 files changed

+32
-0
lines changed

crates/matrix-sdk-base/src/session.rs

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,3 +44,13 @@ pub struct Session {
4444
/// The ID of the client device
4545
pub device_id: DeviceIdBox,
4646
}
47+
48+
impl From<ruma::api::client::r0::session::login::Response> for Session {
49+
fn from(response: ruma::api::client::r0::session::login::Response) -> Self {
50+
Self {
51+
access_token: response.access_token,
52+
user_id: response.user_id,
53+
device_id: response.device_id,
54+
}
55+
}
56+
}

crates/matrix-sdk/src/client.rs

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1213,6 +1213,28 @@ impl Client {
12131213
/// # anyhow::Result::<()>::Ok(()) });
12141214
/// ```
12151215
///
1216+
/// The `Session` object can also be created from the response the
1217+
/// [`Client::login()`] method returns:
1218+
///
1219+
/// ```no_run
1220+
/// use matrix_sdk::{Client, Session, ruma::{DeviceIdBox, user_id}};
1221+
/// # use url::Url;
1222+
/// # use futures::executor::block_on;
1223+
/// # block_on(async {
1224+
///
1225+
/// let homeserver = Url::parse("http://example.com")?;
1226+
/// let client = Client::new(homeserver)?;
1227+
///
1228+
/// let session: Session = client
1229+
/// .login("example", "my-password", None, None)
1230+
/// .await?
1231+
/// .into();
1232+
///
1233+
/// // Persist the `Session` so it can later be used to restore the login.
1234+
/// // client.restore_session(session).await?;
1235+
/// # anyhow::Result::<()>::Ok(()) });
1236+
/// ```
1237+
///
12161238
/// [`login`]: #method.login
12171239
pub async fn restore_login(&self, session: Session) -> Result<()> {
12181240
Ok(self.base_client.restore_login(session).await?)

0 commit comments

Comments
 (0)