From 0f3474585f4b0327a927b39ac7308decf4ba5612 Mon Sep 17 00:00:00 2001 From: Marcin Kulik Date: Fri, 20 Jun 2025 17:09:21 +0200 Subject: [PATCH] Convert upload command to async --- Cargo.lock | 14 ++++++++++++++ Cargo.toml | 2 +- src/api.rs | 27 ++++++++++++++------------- src/cmd/upload.rs | 7 ++++++- 4 files changed, 35 insertions(+), 15 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 8215282..c550c54 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -1143,6 +1143,7 @@ dependencies = [ "url", "wasm-bindgen", "wasm-bindgen-futures", + "wasm-streams", "web-sys", ] @@ -2002,6 +2003,19 @@ dependencies = [ "unicode-ident", ] +[[package]] +name = "wasm-streams" +version = "0.4.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "15053d8d85c7eccdbefef60f06769760a563c7f0a9d6902a13d35c7800b0ad65" +dependencies = [ + "futures-util", + "js-sys", + "wasm-bindgen", + "wasm-bindgen-futures", + "web-sys", +] + [[package]] name = "web-sys" version = "0.3.77" diff --git a/Cargo.toml b/Cargo.toml index 254512a..31c2dcb 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -21,7 +21,7 @@ serde_json = "1.0" clap = { version = "4.0", features = ["derive", "wrap_help"] } signal-hook = { version = "0.3", default-features = false } uuid = { version = "1.6", features = ["v4"] } -reqwest = { version = "0.12", default-features = false, features = ["blocking", "rustls-tls-native-roots", "multipart", "gzip", "json"] } +reqwest = { version = "0.12", default-features = false, features = ["blocking", "rustls-tls-native-roots", "multipart", "gzip", "json", "stream"] } rustyline = { version = "14.0", default-features = false } config = { version = "0.15", default-features = false, features = ["toml"] } which = "8.0" diff --git a/src/api.rs b/src/api.rs index cff4c89..b88e84c 100644 --- a/src/api.rs +++ b/src/api.rs @@ -2,11 +2,8 @@ use std::env; use std::fmt::Debug; use anyhow::{bail, Context, Result}; -use reqwest::blocking::{ - multipart::Form, Client as BlockingClient, RequestBuilder as BlockingRequestBuilder, -}; use reqwest::header; -use reqwest::{Client as AsyncClient, RequestBuilder as AsyncRequestBuilder}; +use reqwest::{multipart::Form, Client, RequestBuilder}; use serde::Deserialize; use url::Url; @@ -36,10 +33,14 @@ pub fn get_auth_url(config: &Config) -> Result { Ok(url) } -pub fn upload_asciicast(path: &str, config: &Config) -> Result { +pub async fn upload_asciicast(path: &str, config: &Config) -> Result { let server_url = &config.get_server_url()?; let install_id = config.get_install_id()?; - let response = upload_request(server_url, path, install_id)?.send()?; + + let response = upload_request(server_url, path, install_id) + .await? + .send() + .await?; if response.status().as_u16() == 413 { bail!("The size of the recording exceeds the server's configured limit"); @@ -47,18 +48,18 @@ pub fn upload_asciicast(path: &str, config: &Config) -> Result()?) + Ok(response.json::().await?) } -fn upload_request( +async fn upload_request( server_url: &Url, path: &str, install_id: String, -) -> Result { - let client = BlockingClient::new(); +) -> Result { + let client = Client::new(); let mut url = server_url.clone(); url.set_path("api/asciicasts"); - let form = Form::new().file("asciicast", path)?; + let form = Form::new().file("asciicast", path).await?; Ok(client .post(url) @@ -104,8 +105,8 @@ pub async fn create_user_stream(stream_id: &str, config: &Config) -> Result AsyncRequestBuilder { - let client = AsyncClient::new(); +fn user_stream_request(server_url: &Url, stream_id: &str, install_id: &str) -> RequestBuilder { + let client = Client::new(); let mut url = server_url.clone(); let builder = if stream_id.is_empty() { diff --git a/src/cmd/upload.rs b/src/cmd/upload.rs index 8c5db27..fa561b9 100644 --- a/src/cmd/upload.rs +++ b/src/cmd/upload.rs @@ -1,4 +1,5 @@ use anyhow::Result; +use tokio::runtime::Runtime; use crate::api; use crate::asciicast; @@ -7,9 +8,13 @@ use crate::config::Config; impl cli::Upload { pub fn run(self) -> Result<()> { + Runtime::new()?.block_on(self.do_run()) + } + + async fn do_run(self) -> Result<()> { let config = Config::new(self.server_url.clone())?; let _ = asciicast::open_from_path(&self.file)?; - let response = api::upload_asciicast(&self.file, &config)?; + let response = api::upload_asciicast(&self.file, &config).await?; println!("{}", response.message.unwrap_or(response.url)); Ok(())