use crate::client::CloudflareClient;
use crate::error::Result;
use serde::{Deserialize, Serialize};
#[derive(Clone)]
pub struct CacheService {
client: CloudflareClient,
}
impl CacheService {
pub(crate) fn new(client: CloudflareClient) -> Self {
Self { client }
}
pub async fn purge_everything(&self, zone_id: impl Into<String>) -> Result<PurgeResponse> {
let zone_id = zone_id.into();
let payload = serde_json::json!({
"purge_everything": true
});
let response = self
.client
.post(&format!("/zones/{}/purge_cache", zone_id), &payload)
.await?;
CloudflareClient::handle_response(response).await
}
pub fn purge_urls(&self, zone_id: impl Into<String>) -> PurgeUrlsRequest {
PurgeUrlsRequest {
service: self.clone(),
zone_id: zone_id.into(),
files: Vec::new(),
}
}
pub fn purge_tags(&self, zone_id: impl Into<String>) -> PurgeTagsRequest {
PurgeTagsRequest {
service: self.clone(),
zone_id: zone_id.into(),
tags: Vec::new(),
}
}
pub fn purge_hosts(&self, zone_id: impl Into<String>) -> PurgeHostsRequest {
PurgeHostsRequest {
service: self.clone(),
zone_id: zone_id.into(),
hosts: Vec::new(),
}
}
pub fn purge_prefixes(&self, zone_id: impl Into<String>) -> PurgePrefixesRequest {
PurgePrefixesRequest {
service: self.clone(),
zone_id: zone_id.into(),
prefixes: Vec::new(),
}
}
}
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct PurgeResponse {
pub id: String,
}
pub struct PurgeUrlsRequest {
service: CacheService,
zone_id: String,
files: Vec<String>,
}
impl PurgeUrlsRequest {
pub fn urls<I, S>(mut self, urls: I) -> Self
where
I: IntoIterator<Item = S>,
S: Into<String>,
{
self.files = urls.into_iter().map(|s| s.into()).collect();
self
}
pub fn add_url(mut self, url: impl Into<String>) -> Self {
self.files.push(url.into());
self
}
pub async fn send(self) -> Result<PurgeResponse> {
if self.files.is_empty() {
return Err(crate::error::Error::InvalidInput(
"At least one URL is required".to_string(),
));
}
if self.files.len() > 30 {
return Err(crate::error::Error::InvalidInput(
"Cannot purge more than 30 URLs at a time".to_string(),
));
}
let payload = serde_json::json!({
"files": self.files
});
let response = self
.service
.client
.post(&format!("/zones/{}/purge_cache", self.zone_id), &payload)
.await?;
CloudflareClient::handle_response(response).await
}
}
pub struct PurgeTagsRequest {
service: CacheService,
zone_id: String,
tags: Vec<String>,
}
impl PurgeTagsRequest {
pub fn tags<I, S>(mut self, tags: I) -> Self
where
I: IntoIterator<Item = S>,
S: Into<String>,
{
self.tags = tags.into_iter().map(|s| s.into()).collect();
self
}
pub fn add_tag(mut self, tag: impl Into<String>) -> Self {
self.tags.push(tag.into());
self
}
pub async fn send(self) -> Result<PurgeResponse> {
if self.tags.is_empty() {
return Err(crate::error::Error::InvalidInput(
"At least one tag is required".to_string(),
));
}
if self.tags.len() > 30 {
return Err(crate::error::Error::InvalidInput(
"Cannot purge more than 30 tags at a time".to_string(),
));
}
let payload = serde_json::json!({
"tags": self.tags
});
let response = self
.service
.client
.post(&format!("/zones/{}/purge_cache", self.zone_id), &payload)
.await?;
CloudflareClient::handle_response(response).await
}
}
pub struct PurgeHostsRequest {
service: CacheService,
zone_id: String,
hosts: Vec<String>,
}
impl PurgeHostsRequest {
pub fn hosts<I, S>(mut self, hosts: I) -> Self
where
I: IntoIterator<Item = S>,
S: Into<String>,
{
self.hosts = hosts.into_iter().map(|s| s.into()).collect();
self
}
pub fn add_host(mut self, host: impl Into<String>) -> Self {
self.hosts.push(host.into());
self
}
pub async fn send(self) -> Result<PurgeResponse> {
if self.hosts.is_empty() {
return Err(crate::error::Error::InvalidInput(
"At least one host is required".to_string(),
));
}
if self.hosts.len() > 30 {
return Err(crate::error::Error::InvalidInput(
"Cannot purge more than 30 hosts at a time".to_string(),
));
}
let payload = serde_json::json!({
"hosts": self.hosts
});
let response = self
.service
.client
.post(&format!("/zones/{}/purge_cache", self.zone_id), &payload)
.await?;
CloudflareClient::handle_response(response).await
}
}
pub struct PurgePrefixesRequest {
service: CacheService,
zone_id: String,
prefixes: Vec<String>,
}
impl PurgePrefixesRequest {
pub fn prefixes<I, S>(mut self, prefixes: I) -> Self
where
I: IntoIterator<Item = S>,
S: Into<String>,
{
self.prefixes = prefixes.into_iter().map(|s| s.into()).collect();
self
}
pub fn add_prefix(mut self, prefix: impl Into<String>) -> Self {
self.prefixes.push(prefix.into());
self
}
pub async fn send(self) -> Result<PurgeResponse> {
if self.prefixes.is_empty() {
return Err(crate::error::Error::InvalidInput(
"At least one prefix is required".to_string(),
));
}
if self.prefixes.len() > 30 {
return Err(crate::error::Error::InvalidInput(
"Cannot purge more than 30 prefixes at a time".to_string(),
));
}
let payload = serde_json::json!({
"prefixes": self.prefixes
});
let response = self
.service
.client
.post(&format!("/zones/{}/purge_cache", self.zone_id), &payload)
.await?;
CloudflareClient::handle_response(response).await
}
}