Skip to main content

clia_influxdb2/api/
task.rs

1//! Tasks API
2
3use reqwest::Method;
4use serde::{Deserialize, Serialize};
5use snafu::ResultExt;
6
7use crate::models::{TaskStatusType, Tasks};
8use crate::{Client, Http, RequestError, ReqwestProcessing, Serializing};
9
10impl Client {
11    /// List all tasks.
12    pub async fn list_tasks(&self, request: ListTasksRequest) -> Result<Tasks, RequestError> {
13        let url = self.url("/api/v2/tasks");
14
15        let response = self
16            .request(Method::GET, &url)
17            .query(&request)
18            .send()
19            .await
20            .context(ReqwestProcessing)?;
21
22        if !response.status().is_success() {
23            let status = response.status();
24            let text = response.text().await.context(ReqwestProcessing)?;
25            let res = Http { status, text }.fail();
26            return res;
27        }
28
29        let res = response.json::<Tasks>().await.context(ReqwestProcessing)?;
30        Ok(res)
31    }
32
33    /// Create a new task.
34    pub async fn create_task(&self, request: CreateTaskRequest) -> Result<(), RequestError> {
35        let url = self.url("/api/v2/tasks");
36        let response = self
37            .request(Method::POST, &url)
38            .body(serde_json::to_string(&request).context(Serializing)?)
39            .send()
40            .await
41            .context(ReqwestProcessing)?;
42
43        if !response.status().is_success() {
44            let status = response.status();
45            let text = response.text().await.context(ReqwestProcessing)?;
46            Http { status, text }.fail()?;
47        }
48
49        Ok(())
50    }
51
52    /// Delete a task specified by task_id.
53    pub async fn delete_task(&self, task_id: &str) -> Result<(), RequestError> {
54        let url = self.url(&format!("/api/v2/tasks/{}", task_id));
55        let response = self
56            .request(Method::DELETE, &url)
57            .send()
58            .await
59            .context(ReqwestProcessing)?;
60        if !response.status().is_success() {
61            let status = response.status();
62            let text = response.text().await.context(ReqwestProcessing)?;
63            Http { status, text }.fail()?;
64        }
65        Ok(())
66    }
67}
68
69/// Request for list tasks api
70#[derive(Clone, Debug, Default, PartialEq, Serialize, Deserialize)]
71pub struct ListTasksRequest {
72    /// Return tasks after a specified task ID.
73    pub after: Option<String>,
74    /// The number of tasks to return. Default: 100. Valid values [1..500].
75    pub limit: Option<u16>,
76    /// Filter tasks to a specified name.
77    pub name: Option<String>,
78    /// Filter tasks to a specific organization name.
79    pub org: Option<String>,
80    /// Filter tasks to a specific organization ID.
81    #[serde(rename = "orgID")]
82    pub org_id: Option<String>,
83    /// Filter tasks by status, either "inactive" or "active".
84    pub status: Option<String>,
85    /// Filter task by type. Default: "". Valid values: ["basic", "system"].
86    #[serde(rename = "type")]
87    pub type_: Option<TaskStatusType>,
88    /// Filter tasks to a specific user ID.
89    pub user: Option<String>,
90}
91
92/// Encapsulates task data that is sent on POST via the task API.
93#[derive(Clone, Debug, Default, PartialEq, Serialize, Deserialize)]
94#[serde(rename_all = "camelCase")]
95pub struct CreateTaskRequest {
96    /// The flux script to run this task
97    pub flux: String,
98    /// An optional description of the task
99    #[serde(skip_serializing_if = "Option::is_none")]
100    pub description: Option<String>,
101    /// The name of the organization that owns this task
102    #[serde(skip_serializing_if = "Option::is_none")]
103    pub org: Option<String>,
104    /// The ID of the organization that owns this task
105    #[serde(rename = "orgID", skip_serializing_if = "Option::is_none")]
106    pub org_id: Option<String>,
107    /// Task status
108    #[serde(skip_serializing_if = "Option::is_none")]
109    pub status: Option<TaskStatusType>,
110}
111
112impl CreateTaskRequest {
113    /// Returns instance of PostTaskRequest
114    pub fn new(flux: String) -> Self {
115        Self {
116            flux,
117            description: None,
118            org: None,
119            org_id: None,
120            status: None,
121        }
122    }
123}