Skip to content
Open
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
17 changes: 15 additions & 2 deletions crates/vika-cli/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,10 @@ struct Cli {
#[arg(long, env = "VIKA_HOST", global = true, default_value = "https://vika.cn")]
host: String,

/// User agent (defaults to vika-sdk-rust/0.1.0)
#[arg(long, env = "VIKA_USER_AGENT", global = true, default_value = "vika-sdk-rust/0.1.0")]
user_agent: String,

/// Output compact JSON instead of pretty-printed
#[arg(long, global = true)]
compact: bool,
Expand Down Expand Up @@ -150,7 +154,7 @@ enum NodesCmd {
#[cfg(test)]
mod tests {
use super::*;
use clap::Parser;
use clap::{CommandFactory, Parser};

fn parse(args: &[&str]) -> Cli {
Cli::parse_from(std::iter::once("vika").chain(args.iter().copied()))
Expand Down Expand Up @@ -241,6 +245,15 @@ mod tests {
assert_eq!(cli.host, "https://custom.vika.cn");
}

#[test]
fn test_user_agent_help_shows_default() {
let mut cmd = Cli::command();
let help = cmd.render_long_help().to_string();

assert!(help.contains("--user-agent <USER_AGENT>"));
assert!(help.contains("[default: vika-sdk-rust/0.1.0]"));
}

#[test]
fn test_fields_create() {
let cli = parse(&["--token", "tok", "fields", "create",
Expand Down Expand Up @@ -276,7 +289,7 @@ async fn main() -> Result<()> {
let cli = Cli::parse();

let token = cli.token.context("API token required: set VIKA_TOKEN or pass --token")?;
let client = VIkaClient::with_host(token, cli.host);
let client = VIkaClient::with_host_and_user_agent(token, cli.host, Some(cli.user_agent));

let value = dispatch(&client, cli.command).await?;

Expand Down
7 changes: 6 additions & 1 deletion crates/vika-sdk/src/client.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,13 @@ impl VIkaClient {
}

pub fn with_host(token: impl Into<String>, host: impl Into<String>) -> Self {
Self::with_host_and_user_agent(token, host, None)
}

pub fn with_host_and_user_agent(token: impl Into<String>, host: impl Into<String>, user_agent: Option<String>) -> Self {
let ua = user_agent.unwrap_or_else(|| "vika-sdk-rust/0.1.0".to_string());
let http = Client::builder()
.user_agent("vika-sdk-rust/0.1.0")
.user_agent(ua)
.build()
.expect("failed to build HTTP client");
Self {
Expand Down