Skip to main content

ybd_core/
client.rs

1//! HTTP 客户端封装模块
2//!
3//! 包装带有 Bilibili 用户鉴权凭证(Cookie/SESSDATA 与 Referer)的请求客户端。
4
5use reqwest::Client;
6
7use crate::{error::Result, model::account::Account, url::UA};
8
9#[derive(Debug)]
10pub struct BiliClient {
11        client: Client,
12        sessdata: String,
13}
14
15impl BiliClient {
16        pub fn new(account: &Account) -> Result<Self> {
17                let client = Client::builder().user_agent(UA).build()?;
18                Ok(Self {
19                        client,
20                        sessdata: account.get_sessdata().into(),
21                })
22        }
23
24        pub fn get(
25                &self,
26                url: &str,
27        ) -> reqwest::RequestBuilder {
28                self.client
29                        .get(url)
30                        .header("Cookie", format!("SESSDATA={}", self.sessdata))
31                        .header("Referer", "https://www.bilibili.com")
32        }
33
34        pub fn downgrade(&self) -> &Client {
35                &self.client
36        }
37}