|
1 | 1 | use crate::api::post::Post;
|
2 | 2 | use crate::blog_backend;
|
3 | 3 | use crate::infra::http::{body_or_err, RequestBuilderExt, VecExt};
|
| 4 | +use crate::infra::iter::IntoIteratorExt; |
4 | 5 | use crate::infra::json;
|
5 | 6 | use crate::infra::result::IntoResult;
|
6 | 7 | use anyhow::Result;
|
@@ -44,43 +45,43 @@ impl Post {
|
44 | 45 | };
|
45 | 46 |
|
46 | 47 | let range = (skip + 1)..=(skip + take).min(total_count);
|
47 |
| - let fut_iter = range.map(|i| async move { |
48 |
| - let req = { |
49 |
| - let query = vec![ |
50 |
| - ("t", "1".to_string()), |
51 |
| - ("p", i.to_string()), |
52 |
| - ("s", 1.to_string()), |
53 |
| - ("search", keyword.to_string()), |
54 |
| - ] |
55 |
| - .into_query_string(); |
56 |
| - let url = blog_backend!("/posts/list?{}", query); |
57 |
| - client.get(url).pat_auth(&self.pat) |
58 |
| - }; |
59 |
| - let resp = req.send().await?; |
60 |
| - |
61 |
| - let id_list = { |
62 |
| - let body = body_or_err(resp).await?; |
63 |
| - let mut json = json::deserialize::<Value>(&body)?; |
64 |
| - let post_id = { |
65 |
| - let json = json["postList"].take(); |
66 |
| - let [post, ..] = serde_json::from_value::<[Value; 1]>(json)?; |
67 |
| - post["id"].as_u64().expect("as_u64 failed for `id`") as usize |
| 48 | + let id_list = range |
| 49 | + .map(|i| async move { |
| 50 | + let req = { |
| 51 | + let query = vec![ |
| 52 | + ("t", "1".to_string()), |
| 53 | + ("p", i.to_string()), |
| 54 | + ("s", 1.to_string()), |
| 55 | + ("search", keyword.to_string()), |
| 56 | + ] |
| 57 | + .into_query_string(); |
| 58 | + let url = blog_backend!("/posts/list?{}", query); |
| 59 | + client.get(url).pat_auth(&self.pat) |
68 | 60 | };
|
69 |
| - let zzk_post_id_list = { |
70 |
| - let json = json["zzkSearchResult"]["postIds"].take(); |
71 |
| - serde_json::from_value::<Vec<usize>>(json) |
72 |
| - }?; |
| 61 | + let resp = req.send().await?; |
73 | 62 |
|
74 |
| - zzk_post_id_list |
75 |
| - .into_iter() |
76 |
| - .chain(iter::once(post_id)) |
77 |
| - .collect::<Vec<usize>>() |
78 |
| - }; |
| 63 | + let id_list = { |
| 64 | + let body = body_or_err(resp).await?; |
| 65 | + let mut json = json::deserialize::<Value>(&body)?; |
| 66 | + let post_id = { |
| 67 | + let json = json["postList"].take(); |
| 68 | + let [post, ..] = serde_json::from_value::<[Value; 1]>(json)?; |
| 69 | + post["id"].as_u64().expect("as_u64 failed for `id`") as usize |
| 70 | + }; |
| 71 | + let zzk_post_id_list = { |
| 72 | + let json = json["zzkSearchResult"]["postIds"].take(); |
| 73 | + serde_json::from_value::<Vec<usize>>(json) |
| 74 | + }?; |
79 | 75 |
|
80 |
| - id_list.into_ok::<anyhow::Error>() |
81 |
| - }); |
| 76 | + zzk_post_id_list |
| 77 | + .into_iter() |
| 78 | + .chain(iter::once(post_id)) |
| 79 | + .collect::<Vec<usize>>() |
| 80 | + }; |
82 | 81 |
|
83 |
| - let id_list = futures::future::join_all(fut_iter) |
| 82 | + id_list.into_ok::<anyhow::Error>() |
| 83 | + }) |
| 84 | + .join_all() |
84 | 85 | .await
|
85 | 86 | .into_iter()
|
86 | 87 | .collect::<Result<Vec<_>>>()?
|
|
0 commit comments