|
1 | 1 | use crate::api::post::get_one::PostEntry;
|
2 | 2 | use crate::api::post::Post;
|
3 | 3 | use crate::blog_backend;
|
4 |
| -use crate::infra::http::{body_or_err, cons_query_string, setup_auth}; |
| 4 | +use crate::infra::http::{body_or_err, RequestBuilderExt, VecExt}; |
5 | 5 | use crate::infra::json;
|
6 | 6 | use crate::infra::result::IntoResult;
|
7 |
| -use anyhow::{anyhow, Result}; |
8 |
| -use serde::{Deserialize, Serialize}; |
| 7 | +use anyhow::Result; |
| 8 | +use serde_json::Value; |
9 | 9 |
|
10 | 10 | /*
|
11 | 11 | Fields only available over blog_backend!("/posts/list?{}", query):
|
@@ -34,31 +34,24 @@ impl Post {
|
34 | 34 | let fut_iter = range.map(|i| async move {
|
35 | 35 | let req = {
|
36 | 36 | let url = {
|
37 |
| - let query = vec![('t', 1), ('p', i), ('s', 1)]; |
38 |
| - let query = cons_query_string(query); |
| 37 | + let query = vec![('t', 1), ('p', i), ('s', 1)].into_query_string(); |
39 | 38 | blog_backend!("/posts/list?{}", query)
|
40 | 39 | };
|
41 | 40 |
|
42 |
| - let req = client.get(url); |
43 |
| - setup_auth(req, &self.pat) |
| 41 | + client.get(url).pat_auth(&self.pat) |
44 | 42 | };
|
45 | 43 |
|
46 | 44 | let resp = req.send().await?;
|
47 | 45 |
|
48 |
| - // entry |
49 |
| - { |
50 |
| - let json = body_or_err(resp).await?; |
51 |
| - #[derive(Serialize, Deserialize, Debug)] |
52 |
| - struct Body { |
53 |
| - #[serde(rename = "postList")] |
54 |
| - pub list: Vec<PostEntry>, |
55 |
| - #[serde(rename = "postsCount")] |
56 |
| - pub total_count: usize, |
57 |
| - } |
58 |
| - let mut body = json::deserialize::<Body>(&json)?; |
| 46 | + let entry = { |
| 47 | + let body = body_or_err(resp).await?; |
| 48 | + let json = json::deserialize::<Value>(&body)?["postList"].take(); |
59 | 49 |
|
60 |
| - body.list.pop().ok_or(anyhow!("No item in response list")) |
61 |
| - } |
| 50 | + let [entry, ..] = serde_json::from_value::<[PostEntry; 1]>(json)?; |
| 51 | + entry |
| 52 | + }; |
| 53 | + |
| 54 | + entry.into_ok() |
62 | 55 | });
|
63 | 56 |
|
64 | 57 | let vec = futures::future::join_all(fut_iter)
|
|
0 commit comments