aboutsummaryrefslogtreecommitdiff
path: root/lib/cfcache/src/lib.rs
blob: baf6775d58276c6939bb2d19d4ec90ed852c85c3 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
use reqwest::header;
use tracing::instrument;

pub type Result<T = ()> = std::result::Result<T, Error>;

#[derive(thiserror::Error, Debug)]
pub enum Error {
    #[error("json error: {0}")]
    Json(#[from] serde_json::Error),

    #[error("request error: {0}")]
    Request(#[from] reqwest::Error),

    #[error("invalid header value: {0}")]
    InvalidHeaderValue(#[from] reqwest::header::InvalidHeaderValue),
}

pub struct Client {
    zone_id: String,
    cli: reqwest::Client,
}

static USER_AGENT: &str = concat!(
    "xesite ",
    env!("CARGO_PKG_NAME"),
    "/",
    env!("CARGO_PKG_VERSION")
);

impl Client {
    pub fn new(api_key: String, zone_id: String) -> Result<Self> {
        let mut headers = header::HeaderMap::new();
        headers.insert(
            header::AUTHORIZATION,
            header::HeaderValue::from_str(&format!("Bearer {}", api_key))?,
        );

        let cli = reqwest::Client::builder()
            .user_agent(USER_AGENT)
            .default_headers(headers)
            .build()?;

        Ok(Self { zone_id, cli })
    }

    #[instrument(skip(self), err)]
    pub async fn purge(&self, urls: Vec<String>) -> Result {
        #[derive(serde::Serialize)]
        struct Files {
            files: Vec<String>,
        }

        self.cli
            .post(&format!(
                "https://api.cloudflare.com/client/v4/zones/{}/purge_cache",
                self.zone_id
            ))
            .json(&Files { files: urls })
            .send()
            .await?
            .error_for_status()?;
        Ok(())
    }
}