Skip to main content

fishpi_sdk/client/
breezemoon.rs

1//! 清风明月方法。
2
3use crate::client::FishPi;
4use crate::domain::breezemoon::Breezemoon;
5use crate::ids::UserName;
6use crate::utils::build_http_path;
7use crate::utils::error::Error;
8use crate::wire::breezemoon::WireBreezemoonList;
9
10/// 清风明月域访问器。
11pub struct BreezemoonApi(pub(crate) FishPi);
12
13impl FishPi {
14    /// 清风明月域。
15    pub fn breezemoon(&self) -> BreezemoonApi {
16        BreezemoonApi(self.clone())
17    }
18}
19
20impl BreezemoonApi {
21    /// 获取清风明月列表。
22    ///
23    /// - `page` 页码
24    /// - `size` 每页个数
25    /// - `user` 可选,指定用户名时查询该用户的清风明月
26    pub async fn list(
27        &self,
28        page: u32,
29        size: u32,
30        user: Option<impl Into<UserName>>,
31    ) -> Result<Vec<Breezemoon>, Error> {
32        let base = if let Some(u) = user {
33            format!("user/{}/", u.into())
34        } else {
35            String::new()
36        };
37        let path = build_http_path(
38            &format!("api/{}breezemoons", base),
39            &[
40                ("p", page.to_string()),
41                ("size", size.to_string()),
42                ("apiKey", self.0.api_key().to_string()),
43            ],
44        );
45        let wire: WireBreezemoonList = self.0.get_data(&path).await?;
46        Ok(wire.breezemoons.into_iter().map(Breezemoon::from).collect())
47    }
48
49    /// 发送清风明月。
50    pub async fn send(&self, content: impl Into<String>) -> Result<(), Error> {
51        let body = serde_json::json!({
52            "apiKey": self.0.api_key().as_str(),
53            "breezemoonContent": content.into(),
54        });
55        self.0.post_raw("breezemoon", &body).await?;
56        Ok(())
57    }
58}