bpi_rs/vip/
action.rs

1use crate::{ BilibiliRequest, BpiClient, BpiError, BpiResponse };
2use serde::{ Deserialize, Serialize };
3
4/// 大会员每日经验返回数据
5#[derive(Debug, Clone, Deserialize, Serialize)]
6pub struct VipExperienceData {
7    pub r#type: u32,
8    /// 是否领取成功
9    pub is_grant: bool,
10}
11
12impl BpiClient {
13    /// 兑换大会员卡券
14    ///
15    /// 文档: https://socialsisteryi.github.io/bilibili-API-collect/docs/vip/action.html#兑换大会员卡券
16    ///
17    /// # 参数
18    /// | 名称    | 类型 | 说明         |
19    /// | ------- | ---- | ------------|
20    /// | `type_` | u8   | 卡券类型     |
21    pub async fn vip_receive_privilege(
22        &self,
23        type_: u8
24    ) -> Result<BpiResponse<serde_json::Value>, BpiError> {
25        let csrf = self.csrf()?;
26
27        let params = [
28            ("type", type_.to_string()),
29            ("csrf", csrf),
30        ];
31        self
32            .post("https://api.bilibili.com/x/vip/privilege/receive")
33            .form(&params)
34            .send_bpi("兑换大会员卡券").await
35    }
36
37    /// 领取大会员每日经验
38    ///
39    /// 文档: https://socialsisteryi.github.io/bilibili-API-collect/docs/vip/action.html#领取大会员每日经验
40    ///
41    pub async fn vip_add_experience(&self) -> Result<BpiResponse<VipExperienceData>, BpiError> {
42        let csrf = self.csrf()?;
43        let params = [("csrf", csrf)];
44        self
45            .post("https://api.bilibili.com/x/vip/experience/add")
46            .form(&params)
47            .send_bpi("领取大会员每日经验").await
48    }
49}
50
51#[cfg(test)]
52mod tests {
53    use super::*;
54
55    #[tokio::test]
56    async fn test_receive_vip_privilege() {
57        let bpi = BpiClient::new();
58        // 1: B币券,2: 会员购优惠券等
59        let resp = bpi.vip_receive_privilege(1).await;
60        match resp {
61            Ok(resp) => { assert_eq!(resp.code, 0) }
62            Err(e) => {
63                assert_eq!(e.code().unwrap(), 69801);
64            }
65        }
66    }
67
68    #[tokio::test]
69    async fn test_add_vip_experience() {
70        let bpi = BpiClient::new();
71        let resp = bpi.vip_add_experience().await;
72        match resp {
73            Ok(resp) => { assert_eq!(resp.code, 0) }
74            Err(e) => {
75                // 领过了 请求频繁
76                assert!([69198, 6034007].contains(&e.code().unwrap()));
77            }
78        }
79    }
80}