bunny-api 0.0.5

Alpha API client for Bunny.net
Documentation
//! Bindings to the Purge URL API.
//!
//! See [`purge_url`] for more.

use reqwest::Method;

use crate::{APIResult, Client, error::{error_from_response, NoSpecificError}};

const PURGE_METHOD: Method = Method::POST;

/// Purge a given URL from the CDN cache.
///
/// See <https://docs.bunny.net/reference/purgepublic_indexpost> for more.
///
/// # Errors
///
/// Any errors that may occur while sending, or a general error returned from the server.
#[allow(clippy::module_name_repetitions)]
pub async fn purge_url(client: &Client, url: &str, wait: bool) -> APIResult<(), NoSpecificError> {
    let post_url = format!("https://api.bunny.net/purge?async={}&url={}", !wait, urlencoding::encode(url));
    let request = client
        .get(&post_url)
        .header("accept", "application/json")
        .build()
        .map_err(crate::Error::map_request_err(
            PURGE_METHOD,
            post_url.clone(),
        ))?;

    let resp = client
        .send_logged(request)
        .await
        .map_err(crate::Error::map_request_err(
            PURGE_METHOD,
            post_url.clone(),
        ))?;

    error_from_response(resp)
        .await
        .map(|_| ())
        .map_err(crate::Error::map_response_err(
            PURGE_METHOD,
            post_url,
        ))
}