Skip to main content

bpi_rs/fav/
action.rs

1use super::info::FavFolderInfo;
2use crate::BilibiliRequest;
3use crate::fav::FavClient;
4use crate::fav::params::{
5    FavFolderAddParams, FavFolderDeleteParams, FavFolderEditParams, FavResourceBatchDeleteParams,
6    FavResourceCleanParams, FavResourceTransferParams,
7};
8use crate::response::BpiResult;
9
10const FOLDER_ADD_ENDPOINT: &str = "https://api.bilibili.com/x/v3/fav/folder/add";
11const FOLDER_EDIT_ENDPOINT: &str = "https://api.bilibili.com/x/v3/fav/folder/edit";
12const FOLDER_DEL_ENDPOINT: &str = "https://api.bilibili.com/x/v3/fav/folder/del";
13const RESOURCE_COPY_ENDPOINT: &str = "https://api.bilibili.com/x/v3/fav/resource/copy";
14const RESOURCE_MOVE_ENDPOINT: &str = "https://api.bilibili.com/x/v3/fav/resource/move";
15const RESOURCE_BATCH_DEL_ENDPOINT: &str = "https://api.bilibili.com/x/v3/fav/resource/batch-del";
16const RESOURCE_CLEAN_ENDPOINT: &str = "https://api.bilibili.com/x/v3/fav/resource/clean";
17
18impl<'a> FavClient<'a> {
19    /// Creates a favorite folder and returns the canonical payload result.
20    pub async fn add_folder(&self, params: FavFolderAddParams) -> BpiResult<FavFolderInfo> {
21        let csrf = self.client.csrf()?;
22        self.client
23            .post(FOLDER_ADD_ENDPOINT)
24            .form(&params.form_pairs(&csrf))
25            .send_bpi_payload("fav.folder.add")
26            .await
27    }
28
29    /// Edits a favorite folder and returns the canonical payload result.
30    pub async fn edit_folder(&self, params: FavFolderEditParams) -> BpiResult<FavFolderInfo> {
31        let csrf = self.client.csrf()?;
32        self.client
33            .post(FOLDER_EDIT_ENDPOINT)
34            .form(&params.form_pairs(&csrf))
35            .send_bpi_payload("fav.folder.edit")
36            .await
37    }
38
39    /// Deletes favorite folders and returns the canonical payload result.
40    pub async fn delete_folders(&self, params: FavFolderDeleteParams) -> BpiResult<i32> {
41        let csrf = self.client.csrf()?;
42        self.client
43            .post(FOLDER_DEL_ENDPOINT)
44            .form(&params.form_pairs(&csrf))
45            .send_bpi_payload("fav.folder.delete")
46            .await
47    }
48
49    /// Copies favorite resources and returns the canonical payload result.
50    pub async fn copy_resources(&self, params: FavResourceTransferParams) -> BpiResult<i32> {
51        let csrf = self.client.csrf()?;
52        self.client
53            .post(RESOURCE_COPY_ENDPOINT)
54            .form(&params.form_pairs(&csrf))
55            .send_bpi_payload("fav.resource.copy")
56            .await
57    }
58
59    /// Moves favorite resources and returns the canonical payload result.
60    pub async fn move_resources(&self, params: FavResourceTransferParams) -> BpiResult<i32> {
61        let csrf = self.client.csrf()?;
62        self.client
63            .post(RESOURCE_MOVE_ENDPOINT)
64            .form(&params.form_pairs(&csrf))
65            .send_bpi_payload("fav.resource.move")
66            .await
67    }
68
69    /// Deletes favorite resources and returns the canonical payload result.
70    pub async fn delete_resources(&self, params: FavResourceBatchDeleteParams) -> BpiResult<i32> {
71        let csrf = self.client.csrf()?;
72        self.client
73            .post(RESOURCE_BATCH_DEL_ENDPOINT)
74            .form(&params.form_pairs(&csrf))
75            .send_bpi_payload("fav.resource.batch_delete")
76            .await
77    }
78
79    /// Cleans invalid favorite resources and returns the canonical payload result.
80    pub async fn clean_resources(&self, params: FavResourceCleanParams) -> BpiResult<i32> {
81        let csrf = self.client.csrf()?;
82
83        self.client
84            .post(RESOURCE_CLEAN_ENDPOINT)
85            .form(&params.form_pairs(&csrf))
86            .send_bpi_payload("fav.resource.clean")
87            .await
88    }
89}
90
91#[cfg(test)]
92mod tests {}