|
| 1 | +use crate::api::fav::Fav; |
| 2 | +use crate::infra::http::{body_or_err, RequestBuilderExt, VecExt as HttpVecExt}; |
| 3 | +use crate::infra::iter::IntoIteratorExt; |
| 4 | +use crate::infra::json; |
| 5 | +use crate::infra::result::IntoResult; |
| 6 | +use crate::infra::vec::VecExt; |
| 7 | +use crate::openapi; |
| 8 | +use anyhow::Result; |
| 9 | +use serde::{Deserialize, Serialize}; |
| 10 | +use std::ops::ControlFlow; |
| 11 | + |
| 12 | +#[derive(Clone, Debug, Serialize, Deserialize)] |
| 13 | +pub struct FavEntry { |
| 14 | + #[serde(rename = "Title")] |
| 15 | + pub title: String, |
| 16 | + #[serde(rename = "LinkUrl")] |
| 17 | + pub url: String, |
| 18 | + #[serde(rename = "Summary")] |
| 19 | + pub summary: String, |
| 20 | + #[serde(rename = "Tags")] |
| 21 | + pub tags: Vec<String>, |
| 22 | + #[serde(rename = "DateAdded")] |
| 23 | + pub create_time: String, |
| 24 | +} |
| 25 | + |
| 26 | +impl Fav { |
| 27 | + pub async fn get_list(&self, skip: usize, take: usize) -> Result<Vec<FavEntry>> { |
| 28 | + let client = &reqwest::Client::new(); |
| 29 | + |
| 30 | + let range = (skip + 1)..=(skip + take); |
| 31 | + let cf = range |
| 32 | + .map(|i| async move { |
| 33 | + let req = { |
| 34 | + let query = vec![("pageIndex", i), ("pageSize", 1)].into_query_string(); |
| 35 | + let url = openapi!("/Bookmarks?{}", query); |
| 36 | + client.get(url).pat_auth(&self.pat) |
| 37 | + }; |
| 38 | + |
| 39 | + let resp = req.send().await?; |
| 40 | + |
| 41 | + let body = body_or_err(resp).await?; |
| 42 | + |
| 43 | + json::deserialize::<Vec<FavEntry>>(&body)? |
| 44 | + .pop() |
| 45 | + .into_ok::<anyhow::Error>() |
| 46 | + }) |
| 47 | + .join_all() |
| 48 | + .await |
| 49 | + .into_iter() |
| 50 | + .try_fold(vec![], |acc, it| match it { |
| 51 | + Ok(maybe) => match maybe { |
| 52 | + Some(entry) => ControlFlow::Continue(acc.chain_push(entry)), |
| 53 | + None => ControlFlow::Break(Ok(acc)), |
| 54 | + }, |
| 55 | + Err(e) => ControlFlow::Break(Err(e)), |
| 56 | + }); |
| 57 | + |
| 58 | + match cf { |
| 59 | + ControlFlow::Continue(vec) => Ok(vec), |
| 60 | + ControlFlow::Break(result) => result, |
| 61 | + } |
| 62 | + } |
| 63 | +} |
0 commit comments