Skip to main content

cloudreve_api/api/v3/
aria2.rs

1//! Aria2-related API endpoints for Cloudreve API v3
2
3use crate::Error;
4use crate::api::v3::ApiV3Client;
5use crate::api::v3::models::*;
6
7impl ApiV3Client {
8    /// Create one or more aria2 offline-download tasks.
9    ///
10    /// Cloudreve v3 returns the created tasks in `data` (an array of objects whose exact
11    /// shape varies by version, e.g. with/without `gid`), so we accept any JSON payload.
12    /// Callers only need success/failure here — use [`Self::list_downloading`] to follow
13    /// the new tasks afterwards.
14    pub async fn create_download(&self, request: &Aria2CreateRequest<'_>) -> Result<(), Error> {
15        let response: ApiResponse<serde_json::Value> = self.post("/aria2/url", request).await?;
16        if response.code == 0 {
17            Ok(())
18        } else {
19            Err(Error::Api {
20                code: response.code,
21                message: response.msg,
22            })
23        }
24    }
25
26    /// List in-flight aria2 tasks (`GET /aria2/downloading`).
27    ///
28    /// Cloudreve v3 returns a flat array (no pagination wrapper). When there are no active
29    /// downloads the response carries `code: 0, data: null`, which deserialises to an empty
30    /// vector here.
31    pub async fn list_downloading(&self) -> Result<Vec<Aria2DownloadingTask>, Error> {
32        let response: ApiResponse<Vec<Aria2DownloadingTask>> =
33            self.get("/aria2/downloading").await?;
34        if response.code != 0 {
35            return Err(Error::Api {
36                code: response.code,
37                message: response.msg,
38            });
39        }
40        Ok(response.data.unwrap_or_default())
41    }
42
43    /// List finished aria2 tasks (`GET /aria2/finished`).
44    ///
45    /// Cloudreve v3's response is a flat array; the legacy `/aria2/finished/:page` path
46    /// segment was removed years ago and no longer exists on supported servers. Returns
47    /// an empty vector when the server replies with `data: null`.
48    pub async fn list_finished(&self) -> Result<Vec<Aria2FinishedTask>, Error> {
49        let response: ApiResponse<Vec<Aria2FinishedTask>> = self.get("/aria2/finished").await?;
50        if response.code != 0 {
51            return Err(Error::Api {
52                code: response.code,
53                message: response.msg,
54            });
55        }
56        Ok(response.data.unwrap_or_default())
57    }
58
59    /// Cancel an aria2 task by GID.
60    pub async fn delete_task(&self, gid: &str) -> Result<(), Error> {
61        let response: ApiResponse<()> = self.delete(&format!("/aria2/task/{}", gid)).await?;
62        if response.code == 0 {
63            Ok(())
64        } else {
65            Err(Error::Api {
66                code: response.code,
67                message: response.msg,
68            })
69        }
70    }
71}