use anthropic_sdk::{Anthropic, ClientConfig, MessageCreateBuilder, AuthMethod};
use anthropic_sdk::types::ContentBlock;
use std::time::Duration;
fn extract_text_from_content(content: &[ContentBlock]) -> String {
content.iter()
.filter_map(|block| match block {
ContentBlock::Text { text } => Some(text.as_str()),
_ => None,
})
.collect::<Vec<_>>()
.join(" ")
}
#[tokio::main]
async fn main() -> Result<(), Box<dyn std::error::Error>> {
println!("๐ Dual Authentication Test (Anthropic + eBay Gateway)");
println!("=====================================================\n");
let api_key = std::env::var("ANTHROPIC_API_KEY")
.or_else(|_| std::env::var("EBAY_ANTHROPIC_API_KEY"))
.unwrap_or_else(|_| {
println!("โ ๏ธ No API key found. Using dummy key for configuration test.");
"test-key".to_string()
});
println!("๐งช Test 1: Standard Anthropic API (x-api-key header)");
test_anthropic_auth(&api_key).await?;
println!("\n๐งช Test 2: eBay Gateway (Bearer token)");
test_ebay_auth(&api_key).await?;
println!("\n๐งช Test 3: Custom Token Header");
test_token_auth(&api_key).await?;
println!("\nโจ Configuration Examples:");
print_configuration_examples();
Ok(())
}
async fn test_anthropic_auth(api_key: &str) -> Result<(), Box<dyn std::error::Error>> {
println!(" ๐ก URL: https://api.anthropic.com");
println!(" ๐ Auth: x-api-key header");
let config = ClientConfig::new(api_key)
.with_auth_method(AuthMethod::Anthropic)
.with_base_url("https://api.anthropic.com");
let client = Anthropic::with_config(config)?;
let response = client.messages()
.create(
MessageCreateBuilder::new("claude-3-5-sonnet-latest", 50)
.user("Hello from standard Anthropic API!")
.build()
)
.await;
match response {
Ok(msg) => {
println!(" โ
SUCCESS!");
let text = extract_text_from_content(&msg.content);
println!(" ๐ Response: {}", text);
}
Err(e) => {
println!(" โ Expected failure with dummy key: {}", e);
}
}
Ok(())
}
async fn test_ebay_auth(api_key: &str) -> Result<(), Box<dyn std::error::Error>> {
println!(" ๐ก URL: https://platformgateway2.vip.ebay.com/hubgptgatewaysvc/v1/anthropic");
println!(" ๐ Auth: Authorization: Bearer <token>");
let config = ClientConfig::new(api_key)
.with_base_url("https://platformgateway2.vip.ebay.com/hubgptgatewaysvc/v1/anthropic")
.with_auth_method(AuthMethod::Bearer);
let client = Anthropic::with_config(config)?;
let response = client.messages()
.create(
MessageCreateBuilder::new("hubgpt-chat-completions-sonnet-3-7", 50)
.user("Hello from eBay Gateway!")
.build()
)
.await;
match response {
Ok(msg) => {
println!(" โ
SUCCESS! Bearer token works!");
let text = extract_text_from_content(&msg.content);
println!(" ๐ Response: {}", text);
}
Err(e) => {
println!(" โ Error: {}", e);
if api_key == "test-key" {
println!(" ๐ก This is expected with dummy key. Set real API key to test.");
}
}
}
println!("\n ๐ง Testing convenience method:");
let ebay_config = ClientConfig::new(api_key)
.for_ebay_gateway("https://platformgateway2.vip.ebay.com/hubgptgatewaysvc/v1/anthropic");
let ebay_client = Anthropic::with_config(ebay_config)?;
let response2 = ebay_client.messages()
.create(
MessageCreateBuilder::new("hubgpt-chat-completions-sonnet-3-7", 50)
.user("Hello from eBay convenience config!")
.build()
)
.await;
match response2 {
Ok(msg) => {
println!(" โ
Convenience method works!");
let text = extract_text_from_content(&msg.content);
println!(" ๐ Response: {}", text);
}
Err(e) => {
println!(" โ Error: {}", e);
}
}
Ok(())
}
async fn test_token_auth(api_key: &str) -> Result<(), Box<dyn std::error::Error>> {
println!(" ๐ก URL: Custom gateway with token header");
println!(" ๐ Auth: token header");
let config = ClientConfig::new(api_key)
.with_base_url("https://platformgateway2.vip.ebay.com/hubgptgatewaysvc/v1/anthropic")
.with_auth_method(AuthMethod::Token);
let client = Anthropic::with_config(config)?;
let response = client.messages()
.create(
MessageCreateBuilder::new("hubgpt-chat-completions-sonnet-3-7", 50)
.user("Hello with token header!")
.build()
)
.await;
match response {
Ok(msg) => {
println!(" โ
SUCCESS! Token header works!");
let text = extract_text_from_content(&msg.content);
println!(" ๐ Response: {}", text);
}
Err(e) => {
println!(" โ Error: {}", e);
}
}
Ok(())
}
fn print_configuration_examples() {
println!();
println!("๐ How to Configure for Different Gateways:");
println!();
println!("๐ธ Standard Anthropic API:");
println!("```rust");
println!("let client = Anthropic::new(\"your-api-key\")?;");
println!("// OR");
println!("let config = ClientConfig::new(\"your-api-key\")");
println!(" .with_auth_method(AuthMethod::Anthropic);");
println!("let client = Anthropic::with_config(config)?;");
println!("```");
println!("\n๐ธ eBay Gateway (Bearer Token):");
println!("```rust");
println!("let config = ClientConfig::new(\"your-api-key\")");
println!(" .for_ebay_gateway(\"https://platformgateway2.vip.ebay.com/hubgptgatewaysvc/v1/anthropic\");");
println!("let client = Anthropic::with_config(config)?;");
println!("// OR manually:");
println!("let config = ClientConfig::new(\"your-api-key\")");
println!(" .with_base_url(\"https://platformgateway2.vip.ebay.com/hubgptgatewaysvc/v1/anthropic\")");
println!(" .with_auth_method(AuthMethod::Bearer);");
println!("```");
println!("\n๐ธ Custom Token Header:");
println!("```rust");
println!("let config = ClientConfig::new(\"your-api-key\")");
println!(" .with_base_url(\"https://your-gateway.com/api\")");
println!(" .with_auth_method(AuthMethod::Token);");
println!("```");
println!("\n๐ธ Environment Variables:");
println!("```bash");
println!("export ANTHROPIC_API_KEY='your-key'");
println!("export ANTHROPIC_BASE_URL='https://platformgateway2.vip.ebay.com/hubgptgatewaysvc/v1/anthropic'");
println!("export ANTHROPIC_AUTH_METHOD='bearer' # or 'token' or 'anthropic'");
println!("```");
println!("\n๐ก The SDK now supports all major authentication patterns!");
}