From 4bdf4c40c292b681452c9499b9072b759073bf32 Mon Sep 17 00:00:00 2001 From: Conrad Ludgate Date: Wed, 8 Dec 2021 13:37:49 +0000 Subject: [PATCH] feat: login/register no longer blocking (#216) --- atuin-client/src/api_client.rs | 18 ++++++++++-------- src/command/login.rs | 17 +++++++++-------- src/command/mod.rs | 4 ++-- src/command/register.rs | 12 +++++------- 4 files changed, 26 insertions(+), 25 deletions(-) diff --git a/atuin-client/src/api_client.rs b/atuin-client/src/api_client.rs index 2492283..3a4c859 100644 --- a/atuin-client/src/api_client.rs +++ b/atuin-client/src/api_client.rs @@ -26,7 +26,7 @@ pub struct Client<'a> { client: reqwest::Client, } -pub fn register( +pub async fn register( address: &str, username: &str, email: &str, @@ -45,36 +45,38 @@ pub fn register( } let url = format!("{}/register", address); - let client = reqwest::blocking::Client::new(); + let client = reqwest::Client::new(); let resp = client .post(url) .header(USER_AGENT, APP_USER_AGENT) .json(&map) - .send()?; + .send() + .await?; if !resp.status().is_success() { return Err(eyre!("failed to register user")); } - let session = resp.json::()?; + let session = resp.json::().await?; Ok(session) } -pub fn login(address: &str, req: LoginRequest) -> Result> { +pub async fn login(address: &str, req: LoginRequest<'_>) -> Result> { let url = format!("{}/login", address); - let client = reqwest::blocking::Client::new(); + let client = reqwest::Client::new(); let resp = client .post(url) .header(USER_AGENT, APP_USER_AGENT) .json(&req) - .send()?; + .send() + .await?; if resp.status() != reqwest::StatusCode::OK { return Err(eyre!("invalid login details")); } - let session = resp.json::()?; + let session = resp.json::().await?; Ok(session) } diff --git a/src/command/login.rs b/src/command/login.rs index c4817a5..63a3e0e 100644 --- a/src/command/login.rs +++ b/src/command/login.rs @@ -1,10 +1,10 @@ +use std::borrow::Cow; use std::io; -use std::io::prelude::*; -use std::{borrow::Cow, fs::File}; use atuin_common::api::LoginRequest; use eyre::Result; use structopt::StructOpt; +use tokio::{fs::File, io::AsyncWriteExt}; use atuin_client::api_client; use atuin_client::settings::Settings; @@ -29,7 +29,7 @@ fn get_input() -> Result { } impl Cmd { - pub fn run(&self, settings: &Settings) -> Result<()> { + pub async fn run(&self, settings: &Settings) -> Result<()> { let session_path = atuin_common::utils::data_dir().join("session"); if session_path.exists() { @@ -47,15 +47,16 @@ impl Cmd { let session = api_client::login( settings.sync_address.as_str(), LoginRequest { username, password }, - )?; + ) + .await?; let session_path = settings.session_path.as_str(); - let mut file = File::create(session_path)?; - file.write_all(session.session.as_bytes())?; + let mut file = File::create(session_path).await?; + file.write_all(session.session.as_bytes()).await?; let key_path = settings.key_path.as_str(); - let mut file = File::create(key_path)?; - file.write_all(key.as_bytes())?; + let mut file = File::create(key_path).await?; + file.write_all(key.as_bytes()).await?; println!("Logged in!"); diff --git a/src/command/mod.rs b/src/command/mod.rs index 0295036..dc1f01f 100644 --- a/src/command/mod.rs +++ b/src/command/mod.rs @@ -142,13 +142,13 @@ impl AtuinCmd { } Self::Sync { force } => sync::run(&client_settings, force, &mut db).await, - Self::Login(l) => l.run(&client_settings), + Self::Login(l) => l.run(&client_settings).await, Self::Logout => { logout::run(); Ok(()) } Self::Register(r) => { - register::run(&client_settings, &r.username, &r.email, &r.password) + register::run(&client_settings, &r.username, &r.email, &r.password).await } Self::Key => { use atuin_client::encryption::{encode_key, load_key}; diff --git a/src/command/register.rs b/src/command/register.rs index 1317746..20ad132 100644 --- a/src/command/register.rs +++ b/src/command/register.rs @@ -1,8 +1,6 @@ -use std::fs::File; -use std::io::prelude::*; - use eyre::Result; use structopt::StructOpt; +use tokio::{fs::File, io::AsyncWriteExt}; use atuin_client::api_client; use atuin_client::settings::Settings; @@ -20,7 +18,7 @@ pub struct Cmd { pub password: Option, } -pub fn run( +pub async fn run( settings: &Settings, username: &Option, email: &Option, @@ -32,11 +30,11 @@ pub fn run( let password = or_user_input(password, "password"); let session = - api_client::register(settings.sync_address.as_str(), &username, &email, &password)?; + api_client::register(settings.sync_address.as_str(), &username, &email, &password).await?; let path = settings.session_path.as_str(); - let mut file = File::create(path)?; - file.write_all(session.session.as_bytes())?; + let mut file = File::create(path).await?; + file.write_all(session.session.as_bytes()).await?; // Create a new key, and save it to disk let _key = atuin_client::encryption::new_key(settings)?;