pub struct ApiConfig {
pub base_url: String,
pub api_key: Option<SecretApiKey>,
pub http: HttpClientConfig,
}Expand description
Generic API configuration
Provides common configuration options for all API clients:
- Base URL (validated to be HTTPS in production)
- API key (optional, redacted in Debug)
- HTTP client settings (timeout, proxy, user-agent)
§Security
- API keys are wrapped in
SecretApiKeyto prevent accidental logging - Use
validate()to check that the base URL uses HTTPS
Fields§
§base_url: StringBase URL for the API
api_key: Option<SecretApiKey>API key for authentication (optional, redacted in Debug)
http: HttpClientConfigHTTP client configuration
Implementations§
Source§impl ApiConfig
impl ApiConfig
Sourcepub fn with_api_key(
base_url: impl Into<String>,
api_key: impl Into<String>,
) -> ApiConfig
pub fn with_api_key( base_url: impl Into<String>, api_key: impl Into<String>, ) -> ApiConfig
Create a new config with base URL and API key
Sourcepub fn optional_api_key(self, key: Option<String>) -> ApiConfig
pub fn optional_api_key(self, key: Option<String>) -> ApiConfig
Set optional API key
Sourcepub fn with_timeout_secs(self, secs: u64) -> ApiConfig
pub fn with_timeout_secs(self, secs: u64) -> ApiConfig
Set request timeout in seconds
Sourcepub fn optional_proxy(self, proxy: Option<String>) -> ApiConfig
pub fn optional_proxy(self, proxy: Option<String>) -> ApiConfig
Set optional proxy URL
Sourcepub fn build_client(&self) -> Result<Client, HttpError>
pub fn build_client(&self) -> Result<Client, HttpError>
Build an HTTP client from this configuration
Sourcepub fn validate(&self) -> Result<(), ConfigValidationError>
pub fn validate(&self) -> Result<(), ConfigValidationError>
Validate the configuration for security.
Returns an error if:
- The base URL uses HTTP instead of HTTPS (security risk)
- The base URL is malformed
§Example
use yldfi_common::api::ApiConfig;
// HTTPS URLs are valid
let config = ApiConfig::new("https://api.example.com");
assert!(config.validate().is_ok());
// HTTP URLs are rejected
let config = ApiConfig::new("http://api.example.com");
assert!(config.validate().is_err());Sourcepub fn get_api_key(&self) -> Option<&str>
pub fn get_api_key(&self) -> Option<&str>
Get the exposed API key, if set.
Trait Implementations§
Auto Trait Implementations§
impl Freeze for ApiConfig
impl RefUnwindSafe for ApiConfig
impl Send for ApiConfig
impl Sync for ApiConfig
impl Unpin for ApiConfig
impl UnwindSafe for ApiConfig
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Mutably borrows from an owned value. Read more