Skip to main content

bpi_rs/login/member_center/
sign.rs

1// 修改签名
2//
3// [查看 API 文档](https://github.com/Yuelioi/bilibili-API-collect/tree/cfc5fddcc8a94b74d91970bb5b4eaeb349addc47/docs/login/member_center.md)
4
5use crate::BilibiliRequest;
6use crate::BpiError;
7use crate::BpiResult;
8use crate::login::LoginClient;
9
10const UPDATE_USER_SIGN_ENDPOINT: &str = "https://api.bilibili.com/x/member/web/sign/update";
11
12/// Parameters for updating the member-center user sign.
13#[derive(Debug, Clone, PartialEq, Eq)]
14pub struct LoginUserSignParams {
15    user_sign: String,
16}
17
18impl LoginUserSignParams {
19    pub fn new(user_sign: impl Into<String>) -> BpiResult<Self> {
20        let user_sign = user_sign.into();
21        if user_sign.len() > 70 {
22            return Err(BpiError::invalid_parameter(
23                "user_sign",
24                "length cannot exceed 70 bytes",
25            ));
26        }
27
28        Ok(Self { user_sign })
29    }
30
31    fn form_pairs(&self, csrf: &str) -> Vec<(&'static str, String)> {
32        vec![
33            ("user_sign", self.user_sign.clone()),
34            ("csrf", csrf.to_string()),
35        ]
36    }
37}
38
39impl<'a> LoginClient<'a> {
40    /// Updates the member-center user sign and returns the canonical payload result.
41    pub async fn update_user_sign(
42        &self,
43        params: LoginUserSignParams,
44    ) -> BpiResult<Option<serde_json::Value>> {
45        let csrf = self.client.csrf()?;
46
47        self.client
48            .post(UPDATE_USER_SIGN_ENDPOINT)
49            .form(&params.form_pairs(&csrf))
50            .send_bpi_optional_payload("login.member_center.user_sign.update")
51            .await
52    }
53}