1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
use reqwest::multipart::{Form, Part};
use crate::{
api_client::ApiClient,
error::{ApiError, ResultApi},
model::{Comment, CommentBlock, CommentsResponse},
};
impl ApiClient {
/// Get comments response
///
/// # Arguments
///
/// * `blog_name` - Blog name (blog url)
/// * `post_id` - Post id (optional)
/// * `limit` - Limit comments per request (optional)
/// * `reply_limit` - Reply levels (optional)
/// * `order` - Top or bottom (optional)
/// * `offset` - Offset (intId comment) (optional)
///
/// # Returns
///
/// On success, returns a `CommentsResponse` containing the `data` field with `Comment` items.
///
/// # Errors
///
/// - `ApiError::Unauthorized` if the HTTP status is 401 Unauthorized.
/// - `ApiError::HttpStatus` for other non-success HTTP statuses, with status and endpoint info.
/// - `ApiError::HttpRequest` if the HTTP request fails.
/// - `ApiError::JsonParseDetailed` if the response body cannot be parsed into a `CommentsResponse`.
pub async fn get_comments_response(
&self,
blog_name: &str,
post_id: &str,
limit: Option<u32>,
reply_limit: Option<u32>,
order: Option<&str>,
offset: Option<u64>,
) -> ResultApi<CommentsResponse> {
let mut path = format!("blog/{blog_name}/post/{post_id}/comment/");
let mut params = Vec::new();
if let Some(o) = offset {
params.push(format!("offset={o}"));
}
if let Some(l) = limit {
params.push(format!("limit={l}"));
}
if let Some(rl) = reply_limit {
params.push(format!("reply_limit={rl}"));
}
if let Some(ord) = order {
params.push(format!("order={ord}"));
}
if !params.is_empty() {
path.push('?');
path.push_str(¶ms.join("&"));
}
let response = self.get_request(&path).await?;
let response = self.handle_response(&path, response).await?;
self.parse_json(response).await
}
/// Get all comments for a post.
///
/// # Arguments
///
/// * `blog_name` - Blog name (blog url)
/// * `post_id` - Post id (optional)
/// * `limit` - Limit comments per request (optional)
/// * `reply_limit` - Reply levels (optional)
/// * `order` - Top or bottom (optional)
///
/// # Returns
///
/// On success, returns a vector of `Comment` items.
///
/// # Errors
///
/// - `ApiError::Unauthorized` if the HTTP status is 401 Unauthorized.
/// - `ApiError::HttpStatus` for other non-success HTTP statuses, with status and endpoint info.
/// - `ApiError::HttpRequest` if the HTTP request fails.
/// - `ApiError::JsonParseDetailed` if the response body cannot be parsed into a `Comment`.
pub async fn get_all_comments(
&self,
blog_name: &str,
post_id: &str,
limit: Option<u32>,
reply_limit: Option<u32>,
order: Option<&str>,
) -> ResultApi<Vec<Comment>> {
let mut all_comments = Vec::new();
let mut offset: Option<u64> = None;
loop {
let resp = self
.get_comments_response(blog_name, post_id, limit, reply_limit, order, offset)
.await?;
if resp.data.is_empty() {
break;
}
let last_id = resp.data.last().map(|c| c.int_id);
all_comments.extend(resp.data);
if resp.extra.is_last && resp.extra.is_first {
break;
}
if let Some(id) = last_id {
offset = Some(id);
} else {
break;
}
}
Ok(all_comments)
}
/// Create a new comment.
///
/// # Arguments
///
/// * `blog_name` - Blog name (blog url)
/// * `post_id` - Post id
/// * `blocks` - Vector of `CommentBlock` items with the comment content
/// * `reply_id` - Reply id (optional)
///
/// # Returns
///
/// On success, returns a `Comment` item.
///
/// # Errors
///
/// - `ApiError::Unauthorized` if the HTTP status is 401 Unauthorized.
/// - `ApiError::HttpStatus` for other non-success HTTP statuses, with status and endpoint info.
/// - `ApiError::HttpRequest` if the HTTP request fails.
/// - `ApiError::JsonParseDetailed` if the response body cannot be parsed into a `Comment`.
/// - `ApiError::Other` if form creation fails.
pub async fn create_comment(
&self,
blog_name: &str,
post_id: &str,
blocks: &[CommentBlock],
reply_id: Option<u64>,
) -> ResultApi<Comment> {
let path = format!("blog/{blog_name}/post/{post_id}/comment/");
let mut form = Form::new().text("from_page", "blog");
for block in blocks {
form = form.part(
"data[]",
Part::text(serde_json::to_string(block).map_err(|e| {
ApiError::JsonParseDetailed {
error: e.to_string(),
}
})?)
.mime_str("application/json")
.map_err(|e| ApiError::Other(e.to_string()))?,
);
}
if let Some(id) = reply_id {
form = form.text("reply_id", id.to_string());
}
let response = self.post_multipart(&path, form).await?;
let response = self.handle_response(&path, response).await?;
self.parse_json(response).await
}
}