Skip to main content

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