Skip to main content

apify_client/clients/
build.rs

1//! Client for a single Actor build (`/v2/actor-builds/{buildId}`).
2
3use crate::clients::base::{
4    delete_resource, get_resource, post_action, wait_for_finish, ResourceContext,
5};
6use crate::clients::log::LogClient;
7use crate::common::QueryParams;
8use crate::error::ApifyClientResult;
9use crate::http_client::HttpClient;
10use crate::models::Build;
11
12/// Client for a specific Actor build.
13#[derive(Debug, Clone)]
14pub struct BuildClient {
15    ctx: ResourceContext,
16}
17
18impl BuildClient {
19    pub(crate) fn new(http: HttpClient, base_url: &str, id: &str) -> Self {
20        Self {
21            ctx: ResourceContext::single(http, base_url, "actor-builds", id),
22        }
23    }
24
25    /// Fetches the build object, or `None` if it does not exist.
26    pub async fn get(&self) -> ApifyClientResult<Option<Build>> {
27        get_resource(&self.ctx, None, &QueryParams::new()).await
28    }
29
30    /// Aborts the build.
31    pub async fn abort(&self) -> ApifyClientResult<Build> {
32        post_action(&self.ctx, Some("abort"), &QueryParams::new(), None, None).await
33    }
34
35    /// Deletes the build.
36    pub async fn delete(&self) -> ApifyClientResult<()> {
37        delete_resource(&self.ctx, None).await
38    }
39
40    /// Waits (by client-side polling) for the build to reach a terminal state.
41    ///
42    /// `wait_secs` controls the wait budget:
43    /// - `None` polls indefinitely until the build reaches a terminal state.
44    /// - `Some(n)` bounds the wait to roughly `n` seconds; if the build has not finished by
45    ///   then, the **last fetched (still non-terminal) build is returned** rather than an
46    ///   error. Check `status` / `is_terminal()` on the result when using `Some`.
47    pub async fn wait_for_finish(&self, wait_secs: Option<i64>) -> ApifyClientResult<Build> {
48        wait_for_finish(&self.ctx, wait_secs, |b: &Build| b.is_terminal()).await
49    }
50
51    /// Retrieves the OpenAPI definition generated for this build, if available.
52    ///
53    /// Corresponds to `GET /v2/actor-builds/{buildId}/openapi.json`. The response is a raw
54    /// OpenAPI document (not wrapped in a `data` envelope), returned as JSON.
55    pub async fn get_openapi_definition(&self) -> ApifyClientResult<Option<serde_json::Value>> {
56        let response =
57            crate::clients::base::get_raw(&self.ctx, Some("openapi.json"), &QueryParams::new())
58                .await?;
59        match response {
60            Some(r) => Ok(Some(serde_json::from_slice(&r.body)?)),
61            None => Ok(None),
62        }
63    }
64
65    /// Returns a client for the build's log.
66    pub fn log(&self) -> LogClient {
67        LogClient::nested(self.ctx.http.clone(), &self.ctx.url(None), "log")
68    }
69}