apify_client/clients/
task.rs1use serde::Serialize;
4use serde_json::Value;
5
6use crate::client::ApifyClient;
7use crate::clients::actor::ActorStartOptions;
8use crate::clients::base::{
9 delete_resource, get_resource, post_with_body, update_resource, ResourceContext,
10};
11use crate::clients::run::{LastRunOptions, RunClient};
12use crate::clients::run_collection::RunCollectionClient;
13use crate::clients::webhook_collection::WebhookCollectionClient;
14use crate::common::QueryParams;
15use crate::error::ApifyClientResult;
16use crate::http_client::HttpClient;
17use crate::models::{ActorRun, Task};
18
19#[derive(Debug, Clone)]
21pub struct TaskClient {
22 root: ApifyClient,
23 ctx: ResourceContext,
24}
25
26impl TaskClient {
27 pub(crate) fn new(root: ApifyClient, http: HttpClient, base_url: &str, id: &str) -> Self {
28 Self {
29 root,
30 ctx: ResourceContext::single(http, base_url, "actor-tasks", id),
31 }
32 }
33
34 pub async fn get(&self) -> ApifyClientResult<Option<Task>> {
36 get_resource(&self.ctx, None, &QueryParams::new()).await
37 }
38
39 pub async fn update<T: Serialize>(&self, new_fields: &T) -> ApifyClientResult<Task> {
41 update_resource(&self.ctx, None, new_fields).await
42 }
43
44 pub async fn delete(&self) -> ApifyClientResult<()> {
46 delete_resource(&self.ctx, None).await
47 }
48
49 pub async fn publish(&self) -> ApifyClientResult<Task> {
58 self.update(&serde_json::json!({ "isPublic": true })).await
59 }
60
61 pub async fn unpublish(&self) -> ApifyClientResult<Task> {
68 self.update(&serde_json::json!({ "isPublic": false })).await
69 }
70
71 pub async fn start<T: Serialize>(
75 &self,
76 input: Option<&T>,
77 options: ActorStartOptions,
78 ) -> ApifyClientResult<ActorRun> {
79 let mut params = QueryParams::new();
80 options.apply(&mut params);
81 let body = match input {
82 Some(value) => Some(serde_json::to_vec(value)?),
83 None => None,
84 };
85 post_with_body(&self.ctx, Some("runs"), ¶ms, body, "application/json").await
86 }
87
88 pub async fn call<T: Serialize>(
96 &self,
97 input: Option<&T>,
98 options: ActorStartOptions,
99 wait_secs: Option<i64>,
100 ) -> ApifyClientResult<ActorRun> {
101 let run = self.start(input, options).await?;
102 self.root.run(run.id).wait_for_finish(wait_secs).await
103 }
104
105 pub async fn get_input(&self) -> ApifyClientResult<Option<Value>> {
107 let response =
108 crate::clients::base::get_raw(&self.ctx, Some("input"), &QueryParams::new()).await?;
109 match response {
110 Some(r) => Ok(Some(serde_json::from_slice(&r.body)?)),
111 None => Ok(None),
112 }
113 }
114
115 pub async fn update_input<T: Serialize>(&self, input: &T) -> ApifyClientResult<Value> {
117 let body = serde_json::to_vec(input)?;
118 let url = self.ctx.url(Some("input"));
119 let mut headers = std::collections::HashMap::new();
120 headers.insert("Content-Type".to_string(), "application/json".to_string());
121 let response = self
122 .ctx
123 .http
124 .call(crate::http_client::HttpRequest {
125 method: crate::http_client::HttpMethod::Put,
126 url,
127 headers,
128 body: Some(body),
129 timeout: crate::clients::base::DEFAULT_REQUEST_TIMEOUT,
130 })
131 .await?;
132 Ok(serde_json::from_slice(&response.body)?)
133 }
134
135 pub fn last_run(&self, status: Option<&str>) -> RunClient {
142 self.last_run_with_options(LastRunOptions {
143 status: status.map(str::to_owned),
144 origin: None,
145 })
146 }
147
148 pub fn last_run_with_options(&self, options: LastRunOptions) -> RunClient {
157 let mut client = RunClient::new(
158 self.root.clone(),
159 self.ctx.http.clone(),
160 &self.ctx.url(None),
161 "runs",
162 "last",
163 );
164 if let Some(status) = options.status.as_deref() {
165 client.set_base_param("status", status);
166 }
167 if let Some(origin) = options.origin.as_deref() {
168 client.set_base_param("origin", origin);
169 }
170 client
171 }
172
173 pub fn runs(&self) -> RunCollectionClient {
175 RunCollectionClient::new(self.ctx.http.clone(), &self.ctx.url(None), "runs")
176 }
177
178 pub fn webhooks(&self) -> WebhookCollectionClient {
180 WebhookCollectionClient::with_base(self.ctx.http.clone(), &self.ctx.url(None))
181 }
182}