pub enum AuthProvider {
OpenAI(OpenAIAuth),
Azure(AzureAuth),
}Expand description
Authentication provider for OpenAI APIs
This enum encapsulates the authentication strategy for different API providers. It handles both endpoint URL construction and HTTP header application.
§Example
use openai_tools::common::auth::AuthProvider;
// Auto-detect from environment
let auth = AuthProvider::from_env()?;
// Get endpoint for chat completions
let endpoint = auth.endpoint("chat/completions");
// Apply auth headers to a request
let mut headers = request::header::HeaderMap::new();
auth.apply_headers(&mut headers)?;Variants§
Implementations§
Source§impl AuthProvider
impl AuthProvider
Sourcepub fn openai_from_env() -> Result<Self>
pub fn openai_from_env() -> Result<Self>
Creates an OpenAI authentication provider from environment variables
Reads the API key from OPENAI_API_KEY environment variable.
§Returns
Result<AuthProvider> - OpenAI auth provider or error if env var not set
§Environment Variables
| Variable | Required | Description |
|---|---|---|
OPENAI_API_KEY | Yes | OpenAI API key |
§Example
use openai_tools::common::auth::AuthProvider;
let auth = AuthProvider::openai_from_env()?;Sourcepub fn azure_from_env() -> Result<Self>
pub fn azure_from_env() -> Result<Self>
Creates an Azure OpenAI authentication provider from environment variables
§Returns
Result<AuthProvider> - Azure auth provider or error if required vars not set
§Environment Variables
| Variable | Required | Description |
|---|---|---|
AZURE_OPENAI_API_KEY | Yes | Azure API key |
AZURE_OPENAI_BASE_URL | Yes | Complete endpoint URL including deployment, API path, and api-version |
§Example
use openai_tools::common::auth::AuthProvider;
// With environment variables:
// AZURE_OPENAI_API_KEY=xxx
// AZURE_OPENAI_BASE_URL=https://my-resource.openai.azure.com/openai/deployments/gpt-4o/chat/completions?api-version=2024-08-01-preview
let auth = AuthProvider::azure_from_env()?;Sourcepub fn from_env() -> Result<Self>
pub fn from_env() -> Result<Self>
Creates an authentication provider by auto-detecting from environment
Tries Azure first (if AZURE_OPENAI_API_KEY is set), then falls back to OpenAI.
§Returns
Result<AuthProvider> - Detected auth provider or error
§Detection Order
- If
AZURE_OPENAI_API_KEYis set → Azure - If
OPENAI_API_KEYis set → OpenAI - Otherwise → Error
§Example
use openai_tools::common::auth::AuthProvider;
let auth = AuthProvider::from_env()?;
match &auth {
AuthProvider::OpenAI(_) => println!("Using OpenAI"),
AuthProvider::Azure(_) => println!("Using Azure"),
}Sourcepub fn endpoint(&self, path: &str) -> String
pub fn endpoint(&self, path: &str) -> String
Constructs the full endpoint URL for a given API path
§Arguments
path- API path (e.g., “chat/completions”, “embeddings”)
§Returns
Full endpoint URL appropriate for the provider
§Example
use openai_tools::common::auth::AuthProvider;
let auth = AuthProvider::openai_from_env()?;
let url = auth.endpoint("chat/completions");
// OpenAI: https://api.openai.com/v1/chat/completions
// Azure: https://{resource}.openai.azure.com/openai/deployments/{deployment}/chat/completions?api-version={ver}Sourcepub fn apply_headers(&self, headers: &mut HeaderMap) -> Result<()>
pub fn apply_headers(&self, headers: &mut HeaderMap) -> Result<()>
Sourcepub fn from_url_with_key<S: Into<String>>(base_url: S, api_key: S) -> Self
pub fn from_url_with_key<S: Into<String>>(base_url: S, api_key: S) -> Self
Creates an authentication provider by detecting the provider from URL pattern.
This method analyzes the URL to determine whether it’s an Azure OpenAI endpoint or a standard OpenAI (or compatible) endpoint.
§Arguments
base_url- The complete base URL for API requestsapi_key- The API key or token
§URL Detection
| URL Pattern | Detected Provider |
|---|---|
*.openai.azure.com* | Azure |
| All others | OpenAI (or compatible) |
§Returns
AuthProvider - Detected provider
§Example
use openai_tools::common::auth::AuthProvider;
// OpenAI compatible API
let openai = AuthProvider::from_url_with_key(
"https://api.openai.com/v1",
"sk-key",
);
assert!(openai.is_openai());
// Azure OpenAI (complete base URL)
let azure = AuthProvider::from_url_with_key(
"https://my-resource.openai.azure.com/openai/deployments/gpt-4o?api-version=2024-08-01-preview",
"azure-key",
);
assert!(azure.is_azure());
// Local API (Ollama, LocalAI, etc.)
let local = AuthProvider::from_url_with_key(
"http://localhost:11434/v1",
"dummy-key",
);
assert!(local.is_openai()); // Treated as OpenAI-compatibleSourcepub fn from_url<S: Into<String>>(base_url: S) -> Result<Self>
pub fn from_url<S: Into<String>>(base_url: S) -> Result<Self>
Creates an authentication provider from URL, using environment variables for credentials.
This is a convenience method that combines URL-based detection with environment variable-based credential loading.
§Arguments
base_url- The complete base URL for API requests
§Environment Variables
For Azure URLs (*.openai.azure.com):
AZURE_OPENAI_API_KEY(required)
For other URLs:
OPENAI_API_KEY(required)
§Example
use openai_tools::common::auth::AuthProvider;
// Uses OPENAI_API_KEY from environment
let auth = AuthProvider::from_url("https://api.openai.com/v1")?;
// Uses AZURE_OPENAI_API_KEY from environment
let azure = AuthProvider::from_url(
"https://my-resource.openai.azure.com/openai/deployments/gpt-4o/chat/completions?api-version=2024-08-01-preview"
)?;Trait Implementations§
Source§impl Clone for AuthProvider
impl Clone for AuthProvider
Source§fn clone(&self) -> AuthProvider
fn clone(&self) -> AuthProvider
1.0.0 (const: unstable) · Source§fn clone_from(&mut self, source: &Self)
fn clone_from(&mut self, source: &Self)
source. Read more