Skip to main content

apify_client/clients/
schedule.rs

1//! Client for a single schedule (`/v2/schedules/{scheduleId}`).
2
3use serde::Serialize;
4
5use crate::clients::base::{
6    delete_resource, get_raw, get_resource, update_resource, ResourceContext,
7};
8use crate::common::QueryParams;
9use crate::error::ApifyClientResult;
10use crate::http_client::HttpClient;
11use crate::models::Schedule;
12
13/// Client for a specific schedule.
14#[derive(Debug, Clone)]
15pub struct ScheduleClient {
16    ctx: ResourceContext,
17}
18
19impl ScheduleClient {
20    pub(crate) fn new(http: HttpClient, base_url: &str, id: &str) -> Self {
21        Self {
22            ctx: ResourceContext::single(http, base_url, "schedules", id),
23        }
24    }
25
26    /// Fetches the schedule, or `None` if it does not exist.
27    pub async fn get(&self) -> ApifyClientResult<Option<Schedule>> {
28        get_resource(&self.ctx, None, &QueryParams::new()).await
29    }
30
31    /// Updates the schedule with the given fields.
32    pub async fn update<T: Serialize>(&self, new_fields: &T) -> ApifyClientResult<Schedule> {
33        update_resource(&self.ctx, None, new_fields).await
34    }
35
36    /// Deletes the schedule.
37    pub async fn delete(&self) -> ApifyClientResult<()> {
38        delete_resource(&self.ctx, None).await
39    }
40
41    /// Fetches the schedule's invocation log as text, or `None` if not available.
42    pub async fn get_log(&self) -> ApifyClientResult<Option<String>> {
43        let response = get_raw(&self.ctx, Some("log"), &QueryParams::new()).await?;
44        match response {
45            Some(r) => Ok(Some(String::from_utf8_lossy(&r.body).into_owned())),
46            None => Ok(None),
47        }
48    }
49}