Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
132 changes: 128 additions & 4 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 3 additions & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,9 @@ uuid = { version = "1.22.0", features = [
"macro-diagnostics", # Enable better diagnostics for compile-time UUIDs
] }

reqwest = { version = "0.13", features = ["blocking", "multipart"] }
reqwest = { version = "0.13", features = ["blocking", "multipart", "json"] }

tungstenite = { version = "0.28", features = ["rustls-tls-webpki-roots"] }

rustc_version_runtime = "0.3.0"
os_info = "3"
Expand Down
110 changes: 109 additions & 1 deletion src/commands/api/asciinema.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use super::ApiService;
use super::{ApiService, StreamInfo};

use base64::Engine;
use base64::prelude::BASE64_STANDARD;
Expand Down Expand Up @@ -193,11 +193,73 @@ impl ApiService for Asciinema {
None
}
}

fn create_stream(&self, cols: u16, rows: u16) -> Option<StreamInfo> {
#[derive(Deserialize)]
struct CreateStreamResponse {
id: String,
url: String,
ws_producer_url: String,
}

let stream_url = format!("{}/api/streams", &self.config.api_server);
let body = serde_json::json!({ "cols": cols, "rows": rows });
let res = match self
.http_client
.post(stream_url)
.json(&body)
.send()
{
Ok(r) => r,
Err(e) => {
println!("Failed to reach stream server: {}", e);
return None;
}
};

if res.status().is_success() {
match res.json::<CreateStreamResponse>() {
Ok(resp) => Some(StreamInfo {
id: resp.id,
url: resp.url,
ws_producer_url: resp.ws_producer_url,
}),
Err(e) => {
println!("Failed to parse stream response: {}", e);
None
}
}
} else {
println!("Failed to create stream:");
println!("{}", res.text().unwrap_or_default());
None
}
}

fn get_stream_ws_url(&self, stream_id: &str) -> String {
// Derive the producer WebSocket URL from the server base URL and stream ID.
// The path follows the asciinema server convention: /ws/S/<stream_id>
let base = self
.config
.api_server
.trim_end_matches('/')
.replace("https://", "wss://")
.replace("http://", "ws://");
format!("{}/ws/S/{}", base, stream_id)
}

fn get_auth_header(&self) -> String {
let cred = format!("user:{}", self.config.install_id);
format!("Basic {}", BASE64_STANDARD.encode(&cred))
}
Comment on lines +197 to +254
Copy link

Copilot AI Mar 10, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

New API helpers create_stream, get_stream_ws_url, and get_auth_header were added, but the test module only covers config creation. Adding unit tests for get_stream_ws_url (http/https, trailing slash handling) and get_auth_header (expected Basic base64 format) would help prevent regressions without needing network calls.

Copilot uses AI. Check for mistakes.
}

#[cfg(test)]
mod tests {
use base64::prelude::BASE64_STANDARD;
use base64::Engine;
use crate::commands::api::asciinema::Config;
use crate::commands::api::ApiService;

use uuid::{Uuid, Version};

Expand All @@ -207,4 +269,50 @@ mod tests {
let uuid = Uuid::parse_str(&c.install_id);
assert_eq!(uuid.unwrap().get_version(), Some(Version::Random)); // uuid4
}

#[test]
fn test_get_stream_ws_url_https_and_http() {
let client = reqwest::blocking::Client::new();

let asc_https = super::Asciinema {
config: Config {
install_id: "install".to_string(),
api_server: "https://demo.asciinema.org/".to_string(),
location: String::new(),
},
http_client: client.clone(),
};
assert_eq!(
asc_https.get_stream_ws_url("abc123"),
"wss://demo.asciinema.org/ws/S/abc123"
);

let asc_http = super::Asciinema {
config: Config {
install_id: "install".to_string(),
api_server: "http://asciinema.test".to_string(),
location: String::new(),
},
http_client: client,
};
assert_eq!(
asc_http.get_stream_ws_url("xyz"),
"ws://asciinema.test/ws/S/xyz"
);
}

#[test]
fn test_get_auth_header_format() {
let asc = super::Asciinema {
config: Config {
install_id: "token-123".to_string(),
api_server: "https://example".to_string(),
location: String::new(),
},
http_client: reqwest::blocking::Client::new(),
};

let expected = format!("Basic {}", BASE64_STANDARD.encode("user:token-123"));
assert_eq!(asc.get_auth_header(), expected);
}
}
13 changes: 13 additions & 0 deletions src/commands/api/mod.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,21 @@
mod asciinema;

pub struct StreamInfo {
pub id: String,
pub url: String,
pub ws_producer_url: String,
}

pub trait ApiService {
fn auth(&self);
fn upload(&self, filepath: &str) -> Option<String>;
/// Create a new live stream on the server. Returns `StreamInfo` with the public viewer
/// URL and the WebSocket producer URL to push events to.
fn create_stream(&self, cols: u16, rows: u16) -> Option<StreamInfo>;
/// Build the WebSocket producer URL for an *existing* stream identified by `stream_id`.
fn get_stream_ws_url(&self, stream_id: &str) -> String;
/// Return the `Authorization` header value to authenticate WebSocket connections.
fn get_auth_header(&self) -> String;
}

pub use asciinema::Asciinema;
Loading
Loading