Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor: 为 serde_json::Value 实现 validate trait,避免重复代码 #82

Merged
merged 1 commit into from
Apr 26, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
34 changes: 7 additions & 27 deletions src/bilibili/credential.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,7 @@ use rsa::sha2::Sha256;
use rsa::{Oaep, RsaPublicKey};
use serde::{Deserialize, Serialize};

use crate::bilibili::error::BiliError;
use crate::bilibili::Client;
use crate::bilibili::{Client, Validate};

#[derive(Default, Debug, Clone, Serialize, Deserialize)]
pub struct Credential {
Expand All @@ -34,14 +33,8 @@ impl Credential {
.await?
.error_for_status()?
.json::<serde_json::Value>()
.await?;
let (code, msg) = match (res["code"].as_i64(), res["message"].as_str()) {
(Some(code), Some(msg)) => (code, msg),
_ => bail!("no code or message found"),
};
if code != 0 {
bail!(BiliError::RequestFailed(code, msg.to_owned()));
}
.await?
.validate()?;
res["data"]["refresh"].as_bool().ok_or(anyhow!("check refresh failed"))
}

Expand Down Expand Up @@ -105,14 +98,7 @@ JNrRuoEUXpabUzGB8QIDAQAB
.error_for_status()?;
// 必须在 .json 前取出 headers,否则 res 会被消耗
let headers = std::mem::take(res.headers_mut());
let res = res.json::<serde_json::Value>().await?;
let (code, msg) = match (res["code"].as_i64(), res["message"].as_str()) {
(Some(code), Some(msg)) => (code, msg),
_ => bail!("no code or message found"),
};
if code != 0 {
bail!(BiliError::RequestFailed(code, msg.to_owned()));
}
let res = res.json::<serde_json::Value>().await?.validate()?;
let set_cookies = headers.get_all(header::SET_COOKIE);
let mut credential = Self {
buvid3: self.buvid3.clone(),
Expand Down Expand Up @@ -144,7 +130,7 @@ JNrRuoEUXpabUzGB8QIDAQAB
}

async fn confirm_refresh(&self, client: &Client, new_credential: &Credential) -> Result<()> {
let res = client
client
.request(
Method::POST,
"https://passport.bilibili.com/x/passport-login/web/confirm/refresh",
Expand All @@ -159,14 +145,8 @@ JNrRuoEUXpabUzGB8QIDAQAB
.await?
.error_for_status()?
.json::<serde_json::Value>()
.await?;
let (code, msg) = match (res["code"].as_i64(), res["message"].as_str()) {
(Some(code), Some(msg)) => (code, msg),
_ => bail!("no code or message found"),
};
if code != 0 {
bail!(BiliError::RequestFailed(code, msg.to_owned()));
}
.await?
.validate()?;
Ok(())
}
}
Expand Down
29 changes: 7 additions & 22 deletions src/bilibili/favorite_list.rs
Original file line number Diff line number Diff line change
@@ -1,12 +1,11 @@
use anyhow::{bail, Result};
use anyhow::Result;
use async_stream::stream;
use chrono::serde::ts_seconds;
use chrono::{DateTime, Utc};
use futures::Stream;
use serde_json::Value;

use crate::bilibili::error::BiliError;
use crate::bilibili::BiliClient;
use crate::bilibili::{BiliClient, Validate};
pub struct FavoriteList<'a> {
client: &'a BiliClient,
fid: String,
Expand Down Expand Up @@ -56,20 +55,13 @@ impl<'a> FavoriteList<'a> {
.await?
.error_for_status()?
.json::<serde_json::Value>()
.await?;
let (code, msg) = match (res["code"].as_i64(), res["message"].as_str()) {
(Some(code), Some(msg)) => (code, msg),
_ => bail!("no code or message found"),
};
if code != 0 {
bail!(BiliError::RequestFailed(code, msg.to_owned()));
}
.await?
.validate()?;
Ok(serde_json::from_value(res["data"].take())?)
}

async fn get_videos(&self, page: u32) -> Result<Value> {
let res = self
.client
self.client
.request(reqwest::Method::GET, "https://api.bilibili.com/x/v3/fav/resource/list")
.query(&[
("media_id", self.fid.as_str()),
Expand All @@ -83,15 +75,8 @@ impl<'a> FavoriteList<'a> {
.await?
.error_for_status()?
.json::<serde_json::Value>()
.await?;
let (code, msg) = match (res["code"].as_i64(), res["message"].as_str()) {
(Some(code), Some(msg)) => (code, msg),
_ => bail!("no code or message found"),
};
if code != 0 {
bail!(BiliError::RequestFailed(code, msg.to_owned()));
}
Ok(res)
.await?
.validate()
}

// 拿到收藏夹的所有权,返回一个收藏夹下的视频流
Expand Down
22 changes: 22 additions & 0 deletions src/bilibili/mod.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
pub use analyzer::{BestStream, FilterOption};
use anyhow::{bail, Result};
pub use client::{BiliClient, Client};
pub use credential::Credential;
pub use danmaku::DanmakuOption;
Expand All @@ -13,3 +14,24 @@ mod danmaku;
mod error;
mod favorite_list;
mod video;

pub(crate) trait Validate {
type Output;

fn validate(self) -> Result<Self::Output>;
}

impl Validate for serde_json::Value {
type Output = serde_json::Value;

fn validate(self) -> Result<Self::Output> {
let (code, msg) = match (self["code"].as_i64(), self["message"].as_str()) {
(Some(code), Some(msg)) => (code, msg),
_ => bail!("no code or message found"),
};
if code != 0 {
bail!(BiliError::RequestFailed(code, msg.to_owned()));
}
Ok(self)
}
}
32 changes: 7 additions & 25 deletions src/bilibili/video.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ use reqwest::Method;
use crate::bilibili::analyzer::PageAnalyzer;
use crate::bilibili::client::BiliClient;
use crate::bilibili::danmaku::{DanmakuElem, DanmakuWriter, DmSegMobileReply};
use crate::bilibili::error::BiliError;
use crate::bilibili::Validate;

static MASK_CODE: u64 = 2251799813685247;
static XOR_CODE: u64 = 23442827791579;
Expand Down Expand Up @@ -70,14 +70,8 @@ impl<'a> Video<'a> {
.await?
.error_for_status()?
.json::<serde_json::Value>()
.await?;
let (code, msg) = match (res["code"].as_i64(), res["message"].as_str()) {
(Some(code), Some(msg)) => (code, msg),
_ => bail!("no code or message found"),
};
if code != 0 {
bail!(BiliError::RequestFailed(code, msg.to_owned()));
}
.await?
.validate()?;
Ok(serde_json::from_value(res["data"].take())?)
}

Expand All @@ -90,14 +84,8 @@ impl<'a> Video<'a> {
.await?
.error_for_status()?
.json::<serde_json::Value>()
.await?;
let (code, msg) = match (res["code"].as_i64(), res["message"].as_str()) {
(Some(code), Some(msg)) => (code, msg),
_ => bail!("no code or message found"),
};
if code != 0 {
bail!(BiliError::RequestFailed(code, msg.to_owned()));
}
.await?
.validate()?;
Ok(serde_json::from_value(res["data"].take())?)
}

Expand Down Expand Up @@ -148,14 +136,8 @@ impl<'a> Video<'a> {
.await?
.error_for_status()?
.json::<serde_json::Value>()
.await?;
let (code, msg) = match (res["code"].as_i64(), res["message"].as_str()) {
(Some(code), Some(msg)) => (code, msg),
_ => bail!("no code or message found"),
};
if code != 0 {
bail!(BiliError::RequestFailed(code, msg.to_owned()));
}
.await?
.validate()?;
Ok(PageAnalyzer::new(res["data"].take()))
}
}
Expand Down