Convert upload command to async

This commit is contained in:
Marcin Kulik
2025-06-20 17:09:21 +02:00
parent 8f6a977977
commit 0f3474585f
4 changed files with 35 additions and 15 deletions

14
Cargo.lock generated
View File

@@ -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"

View File

@@ -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"

View File

@@ -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<Url> {
Ok(url)
}
pub fn upload_asciicast(path: &str, config: &Config) -> Result<UploadAsciicastResponse> {
pub async fn upload_asciicast(path: &str, config: &Config) -> Result<UploadAsciicastResponse> {
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<UploadAsciicastRe
response.error_for_status_ref()?;
Ok(response.json::<UploadAsciicastResponse>()?)
Ok(response.json::<UploadAsciicastResponse>().await?)
}
fn upload_request(
async fn upload_request(
server_url: &Url,
path: &str,
install_id: String,
) -> Result<BlockingRequestBuilder> {
let client = BlockingClient::new();
) -> Result<RequestBuilder> {
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<GetU
.map_err(|e| e.into())
}
fn user_stream_request(server_url: &Url, stream_id: &str, install_id: &str) -> 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() {

View File

@@ -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(())