Skip to content

Commit 1be5224

Browse files
committed
feat(post api): create post
1 parent 7511bdc commit 1be5224

File tree

4 files changed

+51
-21
lines changed

4 files changed

+51
-21
lines changed

src/api/ing/comment.rs

Lines changed: 4 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,6 @@ use crate::api::ing::Ing;
22
use crate::infra::http::{setup_auth, unit_or_err};
33
use crate::openapi;
44
use anyhow::Result;
5-
use mime::APPLICATION_JSON;
6-
use reqwest::header::CONTENT_TYPE;
75
use serde::{Deserialize, Serialize};
86

97
impl Ing {
@@ -19,9 +17,7 @@ impl Ing {
1917
let req = {
2018
let url = openapi!("/statuses/{}/comments", ing_id);
2119
let req = {
22-
let req = client
23-
.post(url)
24-
.header(CONTENT_TYPE, APPLICATION_JSON.to_string());
20+
let req = client.post(url);
2521
let body = {
2622
#[serde_with::skip_serializing_none]
2723
#[derive(Clone, Debug, Serialize, Deserialize)]
@@ -32,14 +28,13 @@ impl Ing {
3228
parent_comment_id: Option<usize>,
3329
content: String,
3430
}
35-
let body = Body {
31+
Body {
3632
reply_to,
3733
parent_comment_id,
3834
content,
39-
};
40-
serde_json::to_string(&body)?
35+
}
4136
};
42-
req.body(body)
37+
req.json(&body)
4338
};
4439
setup_auth(req, &self.pat)
4540
};

src/api/ing/publish.rs

Lines changed: 6 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,6 @@ use crate::infra::http::setup_auth;
33
use crate::infra::result::IntoResult;
44
use crate::openapi;
55
use anyhow::{bail, Result};
6-
use mime::APPLICATION_JSON;
7-
use reqwest::header::CONTENT_TYPE;
86
use serde::{Deserialize, Serialize};
97
use serde_json::json;
108
use std::ops::Not;
@@ -17,20 +15,16 @@ struct IngPubErr {
1715

1816
impl Ing {
1917
pub async fn publish(&self, content: &str) -> Result<()> {
20-
let body = json!({
21-
"content": content,
22-
"isPrivate": false,
23-
})
24-
.to_string();
25-
2618
let client = reqwest::Client::new();
2719

2820
let req = {
2921
let url = openapi!("/statuses");
30-
let req = client
31-
.post(url)
32-
.header(CONTENT_TYPE, APPLICATION_JSON.to_string())
33-
.body(body);
22+
let body = json!({
23+
"content": content,
24+
"isPrivate": false,
25+
});
26+
27+
let req = client.post(url).json(&body);
3428
setup_auth(req, &self.pat)
3529
};
3630

src/api/post/create.rs

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
use crate::api::post::Post;
2+
use crate::blog_backend;
3+
use crate::infra::http::{body_or_err, setup_auth};
4+
use crate::infra::json;
5+
use crate::infra::result::IntoResult;
6+
use anyhow::Result;
7+
use serde::{Deserialize, Serialize};
8+
use serde_json::json;
9+
10+
impl Post {
11+
pub async fn create(&self, title: &str, body: &str, publish: bool) -> Result<usize> {
12+
let client = reqwest::Client::new();
13+
14+
let req = {
15+
let url = blog_backend!("/posts");
16+
let body = json!({
17+
"postType": 1,
18+
"title": title,
19+
"postBody": body,
20+
"isPublished": publish
21+
});
22+
let req = client.post(url).json(&body);
23+
setup_auth(req, &self.pat)
24+
};
25+
26+
let resp = req.send().await?;
27+
28+
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
36+
};
37+
38+
id.into_ok()
39+
}
40+
}

src/api/post/mod.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
pub mod create;
12
pub mod del_one;
23
pub mod get_count;
34
pub mod get_meta_list;

0 commit comments

Comments
 (0)