use foundation_netio::{DynNetClient, HttpClientBuilder};
use crate::types::*;
pub const DEFAULT_BASE_URL: &str = "https://api.cloudflare.com/client/v4";
pub struct CloudflareClient {
http: DynNetClient,
token: String,
zone_id: String,
domain: String,
base_url: String,
}
impl CloudflareClient {
pub fn from_env() -> Result<Self, CloudflareError> {
let token = std::env::var("CLOUDFLARE_API_TOKEN")
.map_err(|_| CloudflareError::Auth("CLOUDFLARE_API_TOKEN not set".into()))?;
let zone_id = std::env::var("CLOUDFLARE_ZONE_ID")
.map_err(|_| CloudflareError::Auth("CLOUDFLARE_ZONE_ID not set".into()))?;
let domain = std::env::var("CLOUDFLARE_DOMAIN").unwrap_or_default();
Ok(Self {
http: HttpClientBuilder::new().build(),
token,
zone_id,
domain,
base_url: DEFAULT_BASE_URL.to_string(),
})
}
pub fn new(token: String, zone_id: String) -> Self {
Self {
http: HttpClientBuilder::new().build(),
token,
zone_id,
domain: String::new(),
base_url: DEFAULT_BASE_URL.to_string(),
}
}
pub fn with_client(http: DynNetClient, token: String, zone_id: String) -> Self {
Self {
http,
token,
zone_id,
domain: String::new(),
base_url: DEFAULT_BASE_URL.to_string(),
}
}
#[must_use]
pub fn with_domain(mut self, domain: String) -> Self {
self.domain = domain;
self
}
#[must_use] pub fn zone_id(&self) -> &str { &self.zone_id }
#[must_use] pub fn http(&self) -> DynNetClient { self.http.clone() }
#[must_use] pub fn token(&self) -> String { self.token.clone() }
#[must_use] pub fn domain(&self) -> &str { &self.domain }
#[must_use] pub fn base_url(&self) -> &str { &self.base_url }
#[must_use]
pub fn with_base_url(mut self, base_url: impl Into<String>) -> Self {
self.base_url = base_url.into();
self
}
}