portkey_sdk/client/
auth.rs

1//! Authentication methods for the Portkey API.
2//!
3//! This module defines the different authentication methods supported by Portkey
4//! for routing requests to various LLM providers.
5
6/// Authentication method for Portkey API.
7///
8/// Portkey supports multiple authentication methods for routing requests
9/// to different LLM providers.
10#[derive(Debug, Clone)]
11pub enum AuthMethod {
12    /// Virtual Key authentication - managed provider credentials in Portkey.
13    ///
14    /// Uses `x-portkey-virtual-key` header. Virtual keys are managed in the
15    /// Portkey dashboard and securely store provider API keys.
16    ///
17    /// # Example
18    /// ```no_run
19    /// use portkey_sdk::builder::AuthMethod;
20    ///
21    /// let auth = AuthMethod::VirtualKey {
22    ///     virtual_key: "your-virtual-key".to_string(),
23    /// };
24    /// ```
25    VirtualKey {
26        /// The virtual key ID from Portkey dashboard
27        virtual_key: String,
28    },
29
30    /// Provider authentication with direct provider credentials.
31    ///
32    /// Uses `x-portkey-provider` and `Authorization` headers to directly
33    /// authenticate with a provider.
34    ///
35    /// # Example
36    /// ```no_run
37    /// use portkey_sdk::builder::AuthMethod;
38    ///
39    /// let auth = AuthMethod::ProviderAuth {
40    ///     provider: "openai".to_string(),
41    ///     authorization: "Bearer sk-...".to_string(),
42    ///     custom_host: None,
43    /// };
44    /// ```
45    ProviderAuth {
46        /// Provider name (e.g., "openai", "anthropic", "google")
47        provider: String,
48        /// Authorization header value (e.g., "Bearer sk-...")
49        authorization: String,
50        /// Optional custom host URL for self-hosted or enterprise endpoints
51        custom_host: Option<String>,
52    },
53
54    /// Config-based authentication using Portkey configs.
55    ///
56    /// Uses `x-portkey-config` header. Configs define complex routing,
57    /// fallback, and load balancing rules in the Portkey dashboard.
58    ///
59    /// # Example
60    /// ```no_run
61    /// use portkey_sdk::builder::AuthMethod;
62    ///
63    /// let auth = AuthMethod::Config {
64    ///     config_id: "pc-config-123".to_string(),
65    /// };
66    /// ```
67    Config {
68        /// The config ID from Portkey dashboard
69        config_id: String,
70    },
71}
72
73impl AuthMethod {
74    /// Creates a virtual key authentication method.
75    ///
76    /// # Example
77    /// ```no_run
78    /// use portkey_sdk::builder::AuthMethod;
79    ///
80    /// let auth = AuthMethod::virtual_key("your-virtual-key");
81    /// ```
82    pub fn virtual_key(virtual_key: impl Into<String>) -> Self {
83        Self::VirtualKey {
84            virtual_key: virtual_key.into(),
85        }
86    }
87
88    /// Creates a provider authentication method.
89    ///
90    /// # Example
91    /// ```no_run
92    /// use portkey_sdk::builder::AuthMethod;
93    ///
94    /// let auth = AuthMethod::provider_auth("openai", "Bearer sk-...");
95    /// ```
96    pub fn provider_auth(provider: impl Into<String>, authorization: impl Into<String>) -> Self {
97        Self::ProviderAuth {
98            provider: provider.into(),
99            authorization: authorization.into(),
100            custom_host: None,
101        }
102    }
103
104    /// Creates a provider authentication method with a custom host.
105    ///
106    /// # Example
107    /// ```no_run
108    /// use portkey_sdk::builder::AuthMethod;
109    ///
110    /// let auth = AuthMethod::provider_auth_with_host(
111    ///     "openai",
112    ///     "Bearer sk-...",
113    ///     "https://custom.openai.com"
114    /// );
115    /// ```
116    pub fn provider_auth_with_host(
117        provider: impl Into<String>,
118        authorization: impl Into<String>,
119        custom_host: impl Into<String>,
120    ) -> Self {
121        Self::ProviderAuth {
122            provider: provider.into(),
123            authorization: authorization.into(),
124            custom_host: Some(custom_host.into()),
125        }
126    }
127
128    /// Creates a config-based authentication method.
129    ///
130    /// # Example
131    /// ```no_run
132    /// use portkey_sdk::builder::AuthMethod;
133    ///
134    /// let auth = AuthMethod::config("pc-config-123");
135    /// ```
136    pub fn config(config_id: impl Into<String>) -> Self {
137        Self::Config {
138            config_id: config_id.into(),
139        }
140    }
141}