Skip to content

Commit

Permalink
feat: add fav api
Browse files Browse the repository at this point in the history
  • Loading branch information
Thaumy committed Sep 26, 2023
1 parent f756117 commit 2bc78b4
Show file tree
Hide file tree
Showing 3 changed files with 66 additions and 0 deletions.
59 changes: 59 additions & 0 deletions src/api/fav/get_list.rs
Original file line number Diff line number Diff line change
@@ -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<String>,
#[serde(rename = "DateAdded")]
pub create_time: String,
}

impl Fav {
pub async fn get_list(&self, skip: usize, take: usize) -> Result<Vec<PostEntry>> {
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::<Value>(&body)?["postList"].take();

let [entry, ..] = serde_json::from_value::<[PostEntry; 1]>(json)?;
entry
};

entry.into_ok()
})
.join_all()
.await
.into_iter()
.collect()
}
}
6 changes: 6 additions & 0 deletions src/api/fav/mod.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
pub mod get_list;

// Aka cnblogs wz
pub struct Fav {
pat: String,
}
1 change: 1 addition & 0 deletions src/api/mod.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
pub mod auth;
pub mod fav;
pub mod ing;
pub mod news;
pub mod post;
Expand Down

0 comments on commit 2bc78b4

Please sign in to comment.