lib_client_anthropic/
auth.rs

1//! Authentication strategies for the Anthropic API.
2
3use crate::error::Result;
4use async_trait::async_trait;
5use reqwest::header::HeaderMap;
6
7/// Authentication strategy trait.
8#[async_trait]
9pub trait AuthStrategy: Send + Sync {
10    /// Apply authentication to the request headers.
11    async fn apply(&self, headers: &mut HeaderMap) -> Result<()>;
12}
13
14/// API key authentication (x-api-key header).
15#[derive(Debug, Clone)]
16pub struct ApiKeyAuth {
17    api_key: String,
18}
19
20impl ApiKeyAuth {
21    /// Create a new API key authentication strategy.
22    pub fn new(api_key: impl Into<String>) -> Self {
23        Self {
24            api_key: api_key.into(),
25        }
26    }
27}
28
29#[async_trait]
30impl AuthStrategy for ApiKeyAuth {
31    async fn apply(&self, headers: &mut HeaderMap) -> Result<()> {
32        headers.insert("x-api-key", self.api_key.parse().unwrap());
33        Ok(())
34    }
35}