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::{AppMonitor, CreateMonitorRequest, MonitorHistory};

/// App Monitors resource — schedule recurring captures and get change alerts.
///
/// All monitor endpoints require a **management JWT**.
///
/// # Example
///
/// ```rust,no_run
/// use screenshotfreeapi::{ScreenshotFreeAPIClient, CreateMonitorRequest};
///
/// #[tokio::main]
/// async fn main() -> screenshotfreeapi::Result<()> {
///     let client = ScreenshotFreeAPIClient::new("sfa_your_api_key");
///     let jwt = "eyJ...";
///
///     let monitor = client.monitors.create(
///         CreateMonitorRequest {
///             app_id: "com.instagram.android".into(),
///             platform: "android".into(),
///             schedule: "0 9 * * *".into(), // daily at 09:00 UTC
///             webhook_url: Some("https://your-app.com/webhook".into()),
///             diff_threshold: None,
///             label: Some("Instagram".into()),
///         },
///         jwt,
///     ).await?;
///     println!("Monitor created: {}", monitor.id);
///     Ok(())
/// }
/// ```
#[derive(Clone)]
pub struct MonitorsResource {
    pub(crate) http: Arc<HttpClient>,
}

impl MonitorsResource {
    /// List all app monitors owned by the caller.
    ///
    /// Corresponds to `GET /monitors/app`.
    pub async fn list(&self, jwt: &str) -> Result<Vec<AppMonitor>> {
        self.http.get("/monitors/app", Some(jwt)).await
    }

    /// Create a new recurring app monitor.
    ///
    /// Corresponds to `POST /monitors/app`.
    pub async fn create(
        &self,
        request: CreateMonitorRequest,
        jwt: &str,
    ) -> Result<AppMonitor> {
        self.http.post("/monitors/app", &request, Some(jwt)).await
    }

    /// Get details of a single monitor.
    ///
    /// Corresponds to `GET /monitors/app/:id`.
    pub async fn get(&self, monitor_id: &str, jwt: &str) -> Result<AppMonitor> {
        self.http
            .get(&format!("/monitors/app/{monitor_id}"), Some(jwt))
            .await
    }

    /// Delete a monitor and stop all future scheduled captures.
    ///
    /// Corresponds to `DELETE /monitors/app/:id`.
    pub async fn delete(
        &self,
        monitor_id: &str,
        jwt: &str,
    ) -> Result<serde_json::Value> {
        self.http
            .delete(&format!("/monitors/app/{monitor_id}"), Some(jwt))
            .await
    }

    /// Get the run history for a monitor, including diff results and screenshots.
    ///
    /// Corresponds to `GET /monitors/app/:id/history`.
    pub async fn history(&self, monitor_id: &str, jwt: &str) -> Result<Vec<MonitorHistory>> {
        self.http
            .get(&format!("/monitors/app/{monitor_id}/history"), Some(jwt))
            .await
    }
}