screenshotfreeapi 1.0.0

Official Rust client for ScreenshotFreeAPI — Screenshot-as-a-Service
Documentation
use std::sync::Arc;

use crate::error::Result;
use crate::http::HttpClient;
use crate::types::{
    BillingUsage, CancelResponse, CurrentPlan, Plan, UpgradeRequest, UpgradeResponse,
    VerifyResponse,
};

/// Billing resource — plans, usage, upgrades, and cancellation.
///
/// Most billing endpoints require a **management JWT**, not the API key.
/// Obtain one via [`AuthResource::token`](crate::resources::auth::AuthResource::token)
/// and pass it as the `jwt` parameter.
///
/// # Example
///
/// ```rust,no_run
/// use screenshotfreeapi::{ScreenshotFreeAPIClient, UpgradeRequest};
///
/// #[tokio::main]
/// async fn main() -> screenshotfreeapi::Result<()> {
///     let client = ScreenshotFreeAPIClient::new("sfa_your_api_key");
///     let jwt = "eyJ..."; // obtained from auth.token()
///
///     let usage = client.billing.usage(jwt).await?;
///     println!("{} screenshots used", usage.screenshots_used);
///     Ok(())
/// }
/// ```
#[derive(Clone)]
pub struct BillingResource {
    pub(crate) http: Arc<HttpClient>,
}

impl BillingResource {
    /// List all available subscription plans.
    ///
    /// This is a public endpoint — no auth required.
    ///
    /// Corresponds to `GET /billing/plans`.
    pub async fn plans(&self) -> Result<Vec<Plan>> {
        self.http.get("/billing/plans", None).await
    }

    /// Get the caller's current subscription plan and quota summary.
    ///
    /// `jwt` — management JWT from [`AuthResource::token`](crate::resources::auth::AuthResource::token).
    ///
    /// Corresponds to `GET /billing/plan`.
    pub async fn plan(&self, jwt: &str) -> Result<CurrentPlan> {
        self.http.get("/billing/plan", Some(jwt)).await
    }

    /// Get 30-day daily usage history for the current billing period.
    ///
    /// `jwt` — management JWT.
    ///
    /// Corresponds to `GET /billing/usage`.
    pub async fn usage(&self, jwt: &str) -> Result<BillingUsage> {
        self.http.get("/billing/usage", Some(jwt)).await
    }

    /// Initiate a plan upgrade.  Returns a Flutterwave checkout `redirect_url`
    /// which the user should be redirected to.
    ///
    /// `jwt` — management JWT.
    ///
    /// Corresponds to `POST /billing/upgrade`.
    pub async fn upgrade(&self, request: UpgradeRequest, jwt: &str) -> Result<UpgradeResponse> {
        self.http.post("/billing/upgrade", &request, Some(jwt)).await
    }

    /// Server-side verify a Flutterwave payment after the user returns from
    /// the checkout redirect.
    ///
    /// `jwt` — management JWT.
    ///
    /// Corresponds to `GET /billing/verify`.
    pub async fn verify(&self, jwt: &str) -> Result<VerifyResponse> {
        self.http.get("/billing/verify", Some(jwt)).await
    }

    /// Cancel the current subscription.
    ///
    /// `jwt` — management JWT.
    ///
    /// Corresponds to `POST /billing/cancel`.
    pub async fn cancel(&self, jwt: &str) -> Result<CancelResponse> {
        self.http.post("/billing/cancel", &serde_json::json!({}), Some(jwt)).await
    }
}