feat(sdk): Implement IntoFuture for LoginBuilder and SsoLoginBuilder

This commit is contained in:
Jonas Platte 2022-12-06 18:56:58 +01:00 committed by Jonas Platte
parent 2176b7ee39
commit 262fe5630f
17 changed files with 44 additions and 44 deletions

View File

@ -14,8 +14,10 @@
// limitations under the License.
#![cfg_attr(not(target_arch = "wasm32"), deny(clippy::future_not_send))]
#[cfg(feature = "sso-login")]
use std::future::Future;
use std::{
future::{Future, IntoFuture},
pin::Pin,
};
use ruma::{
api::client::{session::login, uiaa::UserIdentifier},
@ -136,6 +138,9 @@ impl LoginBuilder {
}
/// Send the login request.
///
/// Instead of calling this function and `.await`ing its return value, you
/// can also `.await` the `LoginBuilder` directly.
#[instrument(
target = "matrix_sdk::client",
name = "login",
@ -159,6 +164,16 @@ impl LoginBuilder {
}
}
impl IntoFuture for LoginBuilder {
type Output = Result<login::v3::Response>;
// TODO: Use impl Trait once allowed in this position on stable
type IntoFuture = Pin<Box<dyn Future<Output = Self::Output>>>;
fn into_future(self) -> Self::IntoFuture {
Box::pin(self.send())
}
}
/// Builder type used to configure optional settings for logging in via SSO.
///
/// Created with [`Client::login_sso`].
@ -260,6 +275,9 @@ where
}
/// Send the login request.
///
/// Instead of calling this function and `.await`ing its return value, you
/// can also `.await` the `SsoLoginBuilder` directly.
#[instrument(target = "matrix_sdk::client", name = "login", skip_all, fields(method = "sso"))]
pub async fn send(self) -> Result<login::v3::Response> {
use std::{
@ -399,3 +417,18 @@ where
login_builder.send().await
}
}
#[cfg(feature = "sso-login")]
impl<F, Fut> IntoFuture for SsoLoginBuilder<F>
where
F: FnOnce(String) -> Fut + Send + 'static,
Fut: Future<Output = Result<()>> + Send + 'static,
{
type Output = Result<login::v3::Response>;
// TODO: Use impl Trait once allowed in this position on stable
type IntoFuture = Pin<Box<dyn Future<Output = Self::Output>>>;
fn into_future(self) -> Self::IntoFuture {
Box::pin(self.send())
}
}

View File

@ -983,7 +983,6 @@ impl Client {
/// let response = client
/// .login_username(user, "wordpass")
/// .initial_device_display_name("My bot")
/// .send()
/// .await?;
///
/// println!(
@ -1046,7 +1045,6 @@ impl Client {
/// let response = client
/// .login_token(login_token)
/// .initial_device_display_name("My app")
/// .send()
/// .await
/// .unwrap();
///
@ -1107,7 +1105,6 @@ impl Client {
/// Ok(())
/// })
/// .initial_device_display_name("My app")
/// .send()
/// .await
/// .unwrap();
///

View File

@ -132,7 +132,6 @@ async fn login_with_sso() {
Ok(())
})
.identity_provider_id(&idp.id)
.send()
.await
.unwrap();

View File

@ -82,7 +82,6 @@ async fn login_sso_refresh_token() {
})
.identity_provider_id(&idp.id)
.request_refresh_token()
.send()
.await
.unwrap();

View File

@ -60,11 +60,7 @@ async fn login_and_sync(
let client = client_builder.build().await?;
client
.login_username(username, password)
.initial_device_display_name("autojoin bot")
.send()
.await?;
client.login_username(username, password).initial_device_display_name("autojoin bot").await?;
println!("logged in as {username}");

View File

@ -51,11 +51,7 @@ async fn login_and_sync(
}
let client = client_builder.build().await.unwrap();
client
.login_username(&username, &password)
.initial_device_display_name("command bot")
.send()
.await?;
client.login_username(&username, &password).initial_device_display_name("command bot").await?;
println!("logged in as {username}");

View File

@ -36,11 +36,8 @@ async fn login(homeserver_url: String, username: &str, password: &str) -> matrix
let homeserver_url = Url::parse(&homeserver_url).expect("Couldn't parse the homeserver URL");
let client = Client::new(homeserver_url).await.unwrap();
let response = client
.login_username(username, password)
.initial_device_display_name("rust-sdk")
.send()
.await?;
let response =
client.login_username(username, password).initial_device_display_name("rust-sdk").await?;
let user_id = &response.user_id;
let client_ref = &client;

View File

@ -109,7 +109,6 @@ async fn login_and_sync(
client
.login_username(username, password)
.initial_device_display_name("getting started bot")
.send()
.await?;
// it worked!

View File

@ -175,7 +175,6 @@ async fn login(cli: Cli) -> Result<Client> {
client
.login_username(&cli.user_name, &cli.password)
.initial_device_display_name("rust-sdk")
.send()
.await?;
Ok(client)

View File

@ -39,11 +39,7 @@ async fn login(
let homeserver_url = Url::parse(&homeserver_url).expect("Couldn't parse the homeserver URL");
let client = Client::new(homeserver_url).await.unwrap();
client
.login_username(username, password)
.initial_device_display_name("rust-sdk")
.send()
.await?;
client.login_username(username, password).initial_device_display_name("rust-sdk").await?;
Ok(client)
}

View File

@ -75,7 +75,6 @@ async fn login_and_sync(
client
.login_username(username, password)
.initial_device_display_name("getting started bot")
.send()
.await?;
// It worked!

View File

@ -33,11 +33,7 @@ async fn login_and_sync(
let homeserver_url = Url::parse(&homeserver_url).expect("Couldn't parse the homeserver URL");
let client = Client::new(homeserver_url).await.unwrap();
client
.login_username(&username, &password)
.initial_device_display_name("command bot")
.send()
.await?;
client.login_username(&username, &password).initial_device_display_name("command bot").await?;
let response = client.sync_once(SyncSettings::default()).await.unwrap();

View File

@ -36,11 +36,7 @@ async fn login(homeserver_url: String, username: &str, password: &str) -> matrix
client.add_event_handler(on_room_message);
client
.login_username(username, password)
.initial_device_display_name("rust-sdk")
.send()
.await?;
client.login_username(username, password).initial_device_display_name("rust-sdk").await?;
client.sync(SyncSettings::new()).await?;
Ok(())

View File

@ -45,7 +45,6 @@ async fn login(cli: Cli) -> Result<Client> {
client
.login_username(&cli.user_name, &cli.password)
.initial_device_display_name("rust-sdk")
.send()
.await?;
Ok(client)

View File

@ -73,7 +73,6 @@ pub async fn run() -> Result<JsValue, JsValue> {
client
.login_username(username, password)
.initial_device_display_name("rust-sdk-wasm")
.send()
.await
.unwrap();

View File

@ -253,7 +253,7 @@ async fn main() -> Result<()> {
.with_prompt(format!("Password for {user_id:} :"))
.interact()?,
};
client.login_username(&user_id, &password).send().await?;
client.login_username(&user_id, &password).await?;
}
if let Some(session) = client.session() {

View File

@ -71,7 +71,7 @@ pub async fn get_client_for_user(username: String, use_sled_store: bool) -> Resu
let _ = client.register(request).await;
}
}
client.login_username(&username, &username).send().await?;
client.login_username(&username, &username).await?;
users.insert(username, (client.clone(), tmp_dir)); // keeping temp dir around so it doesn't get destroyed yet
Ok(client)