Skip to content

Commit c0f6f87

Browse files
committed
refactor: simplify impl
1 parent 2c5199c commit c0f6f87

File tree

14 files changed

+126
-190
lines changed

14 files changed

+126
-190
lines changed

src/api/ing/comment.rs

Lines changed: 17 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
use crate::api::ing::Ing;
2-
use crate::infra::http::{setup_auth, unit_or_err};
2+
use crate::infra::http::{unit_or_err, RequestBuilderExt};
33
use crate::openapi;
44
use anyhow::Result;
55
use serde::{Deserialize, Serialize};
@@ -16,27 +16,23 @@ impl Ing {
1616

1717
let req = {
1818
let url = openapi!("/statuses/{}/comments", ing_id);
19-
let req = {
20-
let req = client.post(url);
21-
let body = {
22-
#[serde_with::skip_serializing_none]
23-
#[derive(Clone, Debug, Serialize, Deserialize)]
24-
struct Body {
25-
#[serde(rename(serialize = "replyTo"))]
26-
reply_to: Option<usize>,
27-
#[serde(rename(serialize = "parentCommentId"))]
28-
parent_comment_id: Option<usize>,
29-
content: String,
30-
}
31-
Body {
32-
reply_to,
33-
parent_comment_id,
34-
content,
35-
}
36-
};
37-
req.json(&body)
19+
let body = {
20+
#[serde_with::skip_serializing_none]
21+
#[derive(Clone, Debug, Serialize, Deserialize)]
22+
struct Body {
23+
#[serde(rename(serialize = "replyTo"))]
24+
reply_to: Option<usize>,
25+
#[serde(rename(serialize = "parentCommentId"))]
26+
parent_comment_id: Option<usize>,
27+
content: String,
28+
}
29+
Body {
30+
reply_to,
31+
parent_comment_id,
32+
content,
33+
}
3834
};
39-
setup_auth(req, &self.pat)
35+
client.post(url).json(&body).pat_auth(&self.pat)
4036
};
4137

4238
let resp = req.send().await?;

src/api/ing/get_comment_list.rs

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
use crate::api::ing::get_list::IngCommentEntry;
22
use crate::api::ing::Ing;
3-
use crate::infra::http::{body_or_err, setup_auth};
3+
use crate::infra::http::{body_or_err, RequestBuilderExt};
44
use crate::infra::json;
55
use crate::infra::result::IntoResult;
66
use crate::openapi;
@@ -12,8 +12,7 @@ impl Ing {
1212

1313
let req = {
1414
let url = openapi!("/statuses/{}/comments", ing_id);
15-
let req = client.get(url);
16-
setup_auth(req, &self.pat)
15+
client.get(url).pat_auth(&self.pat)
1716
};
1817
let resp = req.send().await?;
1918

src/api/ing/get_list.rs

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
use crate::api::ing::{Ing, IngType};
2-
use crate::infra::http::{body_or_err, setup_auth};
2+
use crate::infra::http::{body_or_err, RequestBuilderExt};
33
use crate::infra::json;
44
use crate::infra::result::IntoResult;
55
use crate::openapi;
6-
use anyhow::{anyhow, Result};
6+
use anyhow::Result;
77
use serde::{Deserialize, Serialize};
88

99
#[derive(Clone, Debug, Serialize, Deserialize)]
@@ -71,19 +71,16 @@ impl Ing {
7171
let fut_iter = range.map(|i| async move {
7272
let req = {
7373
let url = openapi!("/statuses/@{}", ing_type.clone() as usize);
74-
let req = client.get(url);
7574
let queries = vec![("pageIndex", i), ("pageSize", 1)];
76-
setup_auth(req, &self.pat).query(&queries)
75+
client.get(url).query(&queries).pat_auth(&self.pat)
7776
};
7877

7978
let resp = req.send().await?;
8079

8180
let body = body_or_err(resp).await?;
8281

8382
let entry_with_comment = {
84-
let entry = json::deserialize::<Vec<IngEntry>>(&body)?
85-
.pop()
86-
.ok_or(anyhow!("No item in response list"))?;
83+
let [entry, ..] = json::deserialize::<[IngEntry; 1]>(&body)?;
8784

8885
let id = entry.id;
8986
(entry, self.get_comment_list(id).await?)

src/api/ing/publish.rs

Lines changed: 4 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,8 @@
11
use crate::api::ing::Ing;
2-
use crate::infra::http::setup_auth;
3-
use crate::infra::result::IntoResult;
2+
use crate::infra::http::{unit_or_err, RequestBuilderExt};
43
use crate::openapi;
5-
use anyhow::{bail, Result};
6-
use serde::{Deserialize, Serialize};
4+
use anyhow::Result;
75
use serde_json::json;
8-
use std::ops::Not;
9-
10-
#[derive(Clone, Debug, Serialize, Deserialize)]
11-
struct IngPubErr {
12-
#[serde(rename = "Message")]
13-
message: String,
14-
}
156

167
impl Ing {
178
pub async fn publish(&self, content: &str) -> Result<()> {
@@ -24,19 +15,11 @@ impl Ing {
2415
"isPrivate": false,
2516
});
2617

27-
let req = client.post(url).json(&body);
28-
setup_auth(req, &self.pat)
18+
client.post(url).json(&body).pat_auth(&self.pat)
2919
};
3020

3121
let resp = req.send().await?;
32-
let code = resp.status();
33-
34-
if code.is_success().not() {
35-
let body = resp.text().await?;
36-
let err = serde_json::from_str::<IngPubErr>(&body)?;
37-
bail!(err.message)
38-
}
3922

40-
().into_ok()
23+
unit_or_err(resp).await
4124
}
4225
}

src/api/post/create.rs

Lines changed: 6 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,10 @@
11
use crate::api::post::Post;
22
use crate::blog_backend;
3-
use crate::infra::http::{body_or_err, setup_auth};
3+
use crate::infra::http::{body_or_err, RequestBuilderExt};
44
use crate::infra::json;
55
use crate::infra::result::IntoResult;
66
use anyhow::Result;
7-
use serde::{Deserialize, Serialize};
8-
use serde_json::json;
7+
use serde_json::{json, Value};
98

109
impl Post {
1110
pub async fn create(&self, title: &str, body: &str, publish: bool) -> Result<usize> {
@@ -19,20 +18,15 @@ impl Post {
1918
"postBody": body,
2019
"isPublished": publish
2120
});
22-
let req = client.post(url).json(&body);
23-
setup_auth(req, &self.pat)
21+
client.post(url).json(&body).pat_auth(&self.pat)
2422
};
2523

2624
let resp = req.send().await?;
2725

2826
let id = {
29-
let json = body_or_err(resp).await?;
30-
#[derive(Serialize, Deserialize, Debug)]
31-
struct Body {
32-
pub id: usize,
33-
}
34-
let body = json::deserialize::<Body>(&json)?;
35-
body.id
27+
let body = body_or_err(resp).await?;
28+
let json = json::deserialize::<Value>(&body)?;
29+
json["id"].as_u64().unwrap() as usize
3630
};
3731

3832
id.into_ok()

src/api/post/del_one.rs

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
use crate::api::post::Post;
22
use crate::blog_backend;
3-
use crate::infra::http::{setup_auth, unit_or_err};
3+
use crate::infra::http::{unit_or_err, RequestBuilderExt};
44
use anyhow::Result;
55

66
impl Post {
@@ -9,8 +9,7 @@ impl Post {
99

1010
let req = {
1111
let url = blog_backend!("/posts/{}", id);
12-
let req = client.delete(url);
13-
setup_auth(req, &self.pat)
12+
client.delete(url).pat_auth(&self.pat)
1413
};
1514
let resp = req.send().await?;
1615

src/api/post/get_count.rs

Lines changed: 7 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,38 +1,30 @@
11
use crate::api::post::Post;
22
use crate::blog_backend;
3-
use crate::infra::http::{body_or_err, cons_query_string, setup_auth};
3+
use crate::infra::http::{body_or_err, RequestBuilderExt, VecExt};
44
use crate::infra::json;
55
use crate::infra::result::IntoResult;
66
use anyhow::Result;
7-
use serde::{Deserialize, Serialize};
7+
use serde_json::Value;
88

99
impl Post {
1010
pub async fn get_count(&self) -> Result<usize> {
1111
let client = reqwest::Client::new();
1212

1313
let req = {
1414
let url = {
15-
let query = vec![('t', 1), ('p', 1), ('s', 1)];
16-
let query = cons_query_string(query);
15+
let query = vec![('t', 1), ('p', 1), ('s', 1)].into_query_string();
1716
blog_backend!("/posts/list?{}", query)
1817
};
1918

20-
let req = client.get(url);
21-
setup_auth(req, &self.pat)
19+
client.get(url).pat_auth(&self.pat)
2220
};
2321

2422
let resp = req.send().await?;
2523

2624
let count = {
27-
let json = body_or_err(resp).await?;
28-
#[derive(Serialize, Deserialize, Debug)]
29-
struct Body {
30-
#[serde(rename = "postsCount")]
31-
pub total_count: usize,
32-
}
33-
let body = json::deserialize::<Body>(&json)?;
34-
35-
body.total_count
25+
let body = body_or_err(resp).await?;
26+
let json = json::deserialize::<Value>(&body)?;
27+
json["postsCount"].as_u64().unwrap() as usize
3628
};
3729

3830
count.into_ok()

src/api/post/get_meta_list.rs

Lines changed: 13 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
11
use crate::api::post::get_one::PostEntry;
22
use crate::api::post::Post;
33
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};
55
use crate::infra::json;
66
use crate::infra::result::IntoResult;
7-
use anyhow::{anyhow, Result};
8-
use serde::{Deserialize, Serialize};
7+
use anyhow::Result;
8+
use serde_json::Value;
99

1010
/*
1111
Fields only available over blog_backend!("/posts/list?{}", query):
@@ -34,31 +34,24 @@ impl Post {
3434
let fut_iter = range.map(|i| async move {
3535
let req = {
3636
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();
3938
blog_backend!("/posts/list?{}", query)
4039
};
4140

42-
let req = client.get(url);
43-
setup_auth(req, &self.pat)
41+
client.get(url).pat_auth(&self.pat)
4442
};
4543

4644
let resp = req.send().await?;
4745

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();
5949

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()
6255
});
6356

6457
let vec = futures::future::join_all(fut_iter)

src/api/post/get_one.rs

Lines changed: 6 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,11 @@
11
use crate::api::post::Post;
22
use crate::blog_backend;
3-
use crate::infra::http::{body_or_err, setup_auth};
3+
use crate::infra::http::{body_or_err, RequestBuilderExt};
44
use crate::infra::json;
55
use crate::infra::result::IntoResult;
66
use anyhow::Result;
77
use serde::{Deserialize, Serialize};
8+
use serde_json::Value;
89

910
// TODO: not elegant
1011
#[derive(Serialize, Deserialize, Debug)]
@@ -41,21 +42,15 @@ impl Post {
4142

4243
let req = {
4344
let url = blog_backend!("/posts/{}", id);
44-
let req = client.get(url);
45-
setup_auth(req, &self.pat)
45+
client.get(url).pat_auth(&self.pat)
4646
};
4747

4848
let resp = req.send().await?;
4949

5050
let entry = {
51-
let json = body_or_err(resp).await?;
52-
#[derive(Serialize, Deserialize, Debug)]
53-
struct Body {
54-
#[serde(rename = "blogPost")]
55-
pub entry: PostEntry,
56-
}
57-
let body = json::deserialize::<Body>(&json)?;
58-
body.entry
51+
let body = body_or_err(resp).await?;
52+
let json = json::deserialize::<Value>(&body)?["blogPost"].take();
53+
serde_json::from_value::<PostEntry>(json)?
5954
};
6055

6156
entry.into_ok()

src/api/post/get_one_raw.rs

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
use crate::api::post::Post;
22
use crate::blog_backend;
3-
use crate::infra::http::{body_or_err, setup_auth};
3+
use crate::infra::http::{body_or_err, RequestBuilderExt};
44
use crate::infra::result::IntoResult;
55
use anyhow::Result;
66
use serde_json::Value;
@@ -61,8 +61,7 @@ impl Post {
6161

6262
let req = {
6363
let url = blog_backend!("/posts/{}", id);
64-
let req = client.get(url);
65-
setup_auth(req, &self.pat)
64+
client.get(url).pat_auth(&self.pat)
6665
};
6766

6867
let resp = req.send().await?;

0 commit comments

Comments
 (0)