From 8df09d11eb95552a1c39460a8d3a640a8e2fa882 Mon Sep 17 00:00:00 2001 From: Thaumy Date: Wed, 27 Sep 2023 17:02:33 +0800 Subject: [PATCH] refactor: use reqwest query fn --- src/api/fav/get_list.rs | 8 ++++---- src/api/ing/get_list.rs | 2 +- src/api/news/get_list.rs | 10 ++++------ src/api/post/get_count.rs | 11 ++++------- src/api/post/get_meta_list.rs | 11 ++++------- src/api/post/search.rs | 24 +++++++++++------------- 6 files changed, 28 insertions(+), 38 deletions(-) diff --git a/src/api/fav/get_list.rs b/src/api/fav/get_list.rs index 26f8d8c..7f0dc02 100644 --- a/src/api/fav/get_list.rs +++ b/src/api/fav/get_list.rs @@ -1,5 +1,5 @@ use crate::api::fav::Fav; -use crate::infra::http::{body_or_err, RequestBuilderExt, VecExt as HttpVecExt}; +use crate::infra::http::{body_or_err, RequestBuilderExt}; use crate::infra::iter::IntoIteratorExt; use crate::infra::json; use crate::infra::result::IntoResult; @@ -31,9 +31,9 @@ impl Fav { let cf = range .map(|i| async move { let req = { - let query = vec![("pageIndex", i), ("pageSize", 1)].into_query_string(); - let url = openapi!("/Bookmarks?{}", query); - client.get(url).pat_auth(&self.pat) + let url = openapi!("/bookmarks"); + let query = [("pageIndex", i), ("pageSize", 1)]; + client.get(url).query(&query).pat_auth(&self.pat) }; let resp = req.send().await?; diff --git a/src/api/ing/get_list.rs b/src/api/ing/get_list.rs index b09f14d..8357b5e 100644 --- a/src/api/ing/get_list.rs +++ b/src/api/ing/get_list.rs @@ -53,7 +53,7 @@ impl Ing { .map(|i| async move { let req = { let url = openapi!("/statuses/@{}", ing_type.clone() as usize); - let query = vec![("pageIndex", i), ("pageSize", 1)]; + let query = [("pageIndex", i), ("pageSize", 1)]; client.get(url).query(&query).pat_auth(&self.pat) }; diff --git a/src/api/news/get_list.rs b/src/api/news/get_list.rs index 5456cb4..c6718ab 100644 --- a/src/api/news/get_list.rs +++ b/src/api/news/get_list.rs @@ -1,5 +1,5 @@ use crate::api::news::News; -use crate::infra::http::{body_or_err, RequestBuilderExt, VecExt}; +use crate::infra::http::{body_or_err, RequestBuilderExt}; use crate::infra::iter::IntoIteratorExt; use crate::infra::json; use crate::infra::result::IntoResult; @@ -37,11 +37,9 @@ impl News { range .map(|i| async move { let req = { - let url = { - let query = vec![("pageIndex", i), ("pageSize", 1)].into_query_string(); - openapi!("/newsitems/?{}", query) - }; - client.get(url).pat_auth(&self.pat) + let url = openapi!("/newsitems"); + let query = [("pageIndex", i), ("pageSize", 1)]; + client.get(url).query(&query).pat_auth(&self.pat) }; let resp = req.send().await?; diff --git a/src/api/post/get_count.rs b/src/api/post/get_count.rs index cc7ddd5..4ed8a08 100644 --- a/src/api/post/get_count.rs +++ b/src/api/post/get_count.rs @@ -1,6 +1,6 @@ use crate::api::post::Post; use crate::blog_backend; -use crate::infra::http::{body_or_err, RequestBuilderExt, VecExt}; +use crate::infra::http::{body_or_err, RequestBuilderExt}; use crate::infra::json; use crate::infra::result::IntoResult; use anyhow::Result; @@ -11,12 +11,9 @@ impl Post { let client = reqwest::Client::new(); let req = { - let url = { - let query = vec![('t', 1), ('p', 1), ('s', 1)].into_query_string(); - blog_backend!("/posts/list?{}", query) - }; - - client.get(url).pat_auth(&self.pat) + let url = blog_backend!("/posts/list"); + let query = [('t', 1), ('p', 1), ('s', 1)]; + client.get(url).query(&query).pat_auth(&self.pat) }; let resp = req.send().await?; diff --git a/src/api/post/get_meta_list.rs b/src/api/post/get_meta_list.rs index 128a3a4..2c12d82 100644 --- a/src/api/post/get_meta_list.rs +++ b/src/api/post/get_meta_list.rs @@ -1,7 +1,7 @@ use crate::api::post::get_one::PostEntry; use crate::api::post::Post; use crate::blog_backend; -use crate::infra::http::{body_or_err, RequestBuilderExt, VecExt}; +use crate::infra::http::{body_or_err, RequestBuilderExt}; use crate::infra::iter::IntoIteratorExt; use crate::infra::json; use crate::infra::result::IntoResult; @@ -35,12 +35,9 @@ impl Post { let vec = range .map(|i| async move { let req = { - let url = { - let query = vec![('t', 1), ('p', i), ('s', 1)].into_query_string(); - blog_backend!("/posts/list?{}", query) - }; - - client.get(url).pat_auth(&self.pat) + let url = blog_backend!("/posts/list"); + let query = [('t', 1), ('p', i), ('s', 1)]; + client.get(url).query(&query).pat_auth(&self.pat) }; let resp = req.send().await?; diff --git a/src/api/post/search.rs b/src/api/post/search.rs index 4366243..fa0655f 100644 --- a/src/api/post/search.rs +++ b/src/api/post/search.rs @@ -1,6 +1,6 @@ use crate::api::post::Post; use crate::blog_backend; -use crate::infra::http::{body_or_err, RequestBuilderExt, VecExt}; +use crate::infra::http::{body_or_err, RequestBuilderExt}; use crate::infra::iter::IntoIteratorExt; use crate::infra::json; use crate::infra::result::IntoResult; @@ -22,15 +22,14 @@ impl Post { // If index is greater than the max page index, API will still return the last page let total_count = { let req = { - let query = vec![ - ("t", "1".to_string()), + let url = blog_backend!("/posts/list"); + let query = [ + ("t", 1.to_string()), ("p", 1.to_string()), ("s", 1.to_string()), ("search", keyword.to_string()), - ] - .into_query_string(); - let url = blog_backend!("/posts/list?{}", query); - client.get(url).pat_auth(&self.pat) + ]; + client.get(url).query(&query).pat_auth(&self.pat) }; let resp = req.send().await?; @@ -48,15 +47,14 @@ impl Post { let id_list = range .map(|i| async move { let req = { - let query = vec![ - ("t", "1".to_string()), + let url = blog_backend!("/posts/list"); + let query = [ + ("t", 1.to_string()), ("p", i.to_string()), ("s", 1.to_string()), ("search", keyword.to_string()), - ] - .into_query_string(); - let url = blog_backend!("/posts/list?{}", query); - client.get(url).pat_auth(&self.pat) + ]; + client.get(url).query(&query).pat_auth(&self.pat) }; let resp = req.send().await?;