From 5783564e8f79fc297dffefac8d6d9666208333c1 Mon Sep 17 00:00:00 2001 From: Thaumy Date: Tue, 26 Sep 2023 15:14:56 +0800 Subject: [PATCH] feat: add fav api --- src/api/fav/get_list.rs | 59 +++++++++++++++++++++++++++++++++++++++++ src/api/fav/mod.rs | 5 ++++ src/api/mod.rs | 1 + 3 files changed, 65 insertions(+) create mode 100644 src/api/fav/get_list.rs create mode 100644 src/api/fav/mod.rs diff --git a/src/api/fav/get_list.rs b/src/api/fav/get_list.rs new file mode 100644 index 0000000..d8b2f19 --- /dev/null +++ b/src/api/fav/get_list.rs @@ -0,0 +1,59 @@ +use crate::api::fav::Fav; +use crate::api::post::get_one::PostEntry; +use crate::infra::http::{body_or_err, RequestBuilderExt, VecExt}; +use crate::infra::iter::IntoIteratorExt; +use crate::infra::json; +use crate::infra::result::IntoResult; +use crate::openapi; +use anyhow::Result; +use serde::{Deserialize, Serialize}; +use serde_json::Value; + +#[derive(Serialize, Deserialize)] +struct FavEntry { + #[serde(rename = "Title")] + pub title: String, + #[serde(rename = "LinkUrl")] + pub link_url: String, + #[serde(rename = "Summary")] + pub summary: String, + #[serde(rename = "Tags")] + pub tags: Vec, + #[serde(rename = "DateAdded")] + pub create_time: String, +} + +impl Fav { + pub async fn get_list(&self, skip: usize, take: usize) -> Result> { + let client = &reqwest::Client::new(); + + let range = (skip + 1)..=(skip + take); + range + .map(|i| async move { + let req = { + let url = { + let query = vec![("pageIndex", i), ("pageSize", 1)].into_query_string(); + openapi!("Bookmarks?{}", query) + }; + + client.get(url).pat_auth(&self.pat) + }; + + let resp = req.send().await?; + + let entry = { + let body = body_or_err(resp).await?; + let json = json::deserialize::(&body)?["postList"].take(); + + let [entry, ..] = serde_json::from_value::<[PostEntry; 1]>(json)?; + entry + }; + + entry.into_ok() + }) + .join_all() + .await + .into_iter() + .collect() + } +} diff --git a/src/api/fav/mod.rs b/src/api/fav/mod.rs new file mode 100644 index 0000000..d0c650b --- /dev/null +++ b/src/api/fav/mod.rs @@ -0,0 +1,5 @@ +pub mod get_list; + +pub struct Fav { + pat: String, +} diff --git a/src/api/mod.rs b/src/api/mod.rs index 4ada5b2..94018dd 100644 --- a/src/api/mod.rs +++ b/src/api/mod.rs @@ -1,4 +1,5 @@ pub mod auth; +pub mod fav; pub mod ing; pub mod news; pub mod post;