fishpi-sdk 0.1.6

A Rust SDK for interacting with the FishPi community API
Documentation
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
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
//! 文章 API 模块
//!
//! 这个模块提供了与文章相关的 API 操作,包括发布、更新、查询、点赞、感谢、收藏、关注、打赏、获取在线人数和 WebSocket 监听等功能。
//! 主要结构体是 `Article`,用于管理文章相关的 HTTP 请求和 WebSocket 连接。
//! 事件通过 `ArticleListener` 回调处理,支持实时消息监听。
//!
//! # 主要组件
//!
//! - [`Article`] - 文章客户端结构体,负责所有文章相关的 API 调用和 WebSocket 连接。
//! - [`ArticleMessageHandler`] - 文章消息处理器,实现 `MessageHandler` trait,处理 WebSocket 消息并异步调用回调。
//! - [`ArticleListener`] - 文章监听器类型别名,定义异步监听器函数的签名,用于处理接收到的消息,支持多线程共享。
//!
//! # 方法列表
//!
//! - [`Article::new`] - 创建新的文章客户端实例。
//! - [`Article::post_article`] - 发布新文章。
//! - [`Article::update_article`] - 更新现有文章。
//! - [`Article::list`] - 查询文章列表(支持类型、标签、分页)。
//! - [`Article::list_by_user`] - 查询指定用户的文章列表。
//! - [`Article::detail`] - 获取文章详情(包括评论分页)。
//! - [`Article::vote`] - 点赞或点踩文章。
//! - [`Article::thank`] - 感谢文章。
//! - [`Article::follow`] - 收藏或取消收藏文章。
//! - [`Article::watch`] - 关注或取消关注文章。
//! - [`Article::reward`] - 打赏文章。
//! - [`Article::heat`] - 获取文章在线人数。
//! - [`Article::add_listener`] - 添加文章 WebSocket 监听器。
//!
//! # 示例
//!
//! ```rust,no_run
//! use fishpi_sdk::api::article::{Article, ArticleListener};
//! use fishpi_sdk::model::article::{ArticlePost, ArticleType};
//! use serde_json::Value;
//! use std::sync::Arc;
//!
//! #[tokio::main]
//! async fn main() -> Result<(), Box<dyn std::error::Error>> {
//!     let article = Article::new("your_api_key".to_string());
//!
//!     let data = ArticlePost {
//!         title: "Test Title".to_string(),
//!         content: "Test Content".to_string(),
//!         tags: "test".to_string(),
//!         commentable: true,
//!         notifyFollowers: false,
//!         type_: ArticleType::Normal,
//!         showInList: 1,
//!         rewardContent: None,
//!         rewardPoint: None,
//!         anonymous: None,
//!         offerPoint: None,
//!     };
//!     let article_id = article.post_article(&data).await?;
//!     let detail = article.detail(&article_id, 1).await?;
//!     println!("Article title: {}", detail.title);
//!
//!     let callback: ArticleListener = Arc::new(|msg: Value| {
//!         Box::pin(async move {
//!             println!("Received message: {:?}", msg);
//!         })
//!     });
//!     let _ws_client = article
//!         .add_listener(&article_id, ArticleType::Normal, Arc::clone(&callback))
//!         .await?;
//!
//!     Ok(())
//! }
//! ```
use std::{pin::Pin, sync::Arc};

use serde_json::{Value, json};

use crate::{
    api::ws::{MessageHandler, WebSocketClient, build_ws_url},
    model::article::{
        ArticleDetail, ArticleList, ArticleListType, ArticlePost, ArticleType, Pagination,
    },
    utils::{ResponseResult, build_http_path, error::Error, get, post},
};

/// 文章监听器类型
pub type ArticleListener = Arc<dyn Fn(Value) -> Pin<Box<dyn Future<Output = ()> + Send>> + Send + Sync + 'static>;

/// 文章消息处理器
pub struct ArticleMessageHandler {
    callback: ArticleListener,
}

impl ArticleMessageHandler {
    pub fn new(callback: ArticleListener) -> Self {
        Self { callback }
    }
}

impl MessageHandler for ArticleMessageHandler {
    fn handle_message(&self, msg: String) {
        let callback = Arc::clone(&self.callback);
        let msg = msg.clone();
        tokio::spawn(async move {
            if let Ok(json) = serde_json::from_str::<Value>(&msg) {
                callback(json).await;
            } else {
                callback(Value::String(msg)).await;
            }
        });
    }
}

pub struct Article {
    api_key: String,
}

impl Article {
    pub fn new(api_key: String) -> Self {
        Self { api_key }
    }

    /// 发布文章
    ///
    /// * `data` 文章信息 [ArticlePost]
    ///
    /// 返回文章 Id
    pub async fn post_article(&self, data: &ArticlePost) -> Result<String, Error> {
        let url = "article".to_string();

        let mut data_json = data.to_json()?;
        data_json["apiKey"] = Value::String(self.api_key.clone());

        let resp = post(&url, Some(data_json)).await?;

        if resp.get("code").and_then(|c| c.as_i64()).unwrap_or(-1) != 0 {
            return Err(Error::Api(
                resp["msg"].as_str().unwrap_or("API error").to_string(),
            ));
        }

        let article_id = resp["articleId"]
            .as_str()
            .ok_or_else(|| Error::Api("Missing articleId in response".to_string()))?
            .to_string();

        Ok(article_id)
    }

    /// 更新文章
    ///
    /// * `id` 文章 Id
    /// * `data` 文章信息 [ArticlePost]
    ///
    /// 返回文章 Id
    pub async fn update_article(&self, id: &str, data: &ArticlePost) -> Result<String, Error> {
        let url = format!("article/{}", id);

        let mut data_json = data.to_json()?;
        data_json["apiKey"] = Value::String(self.api_key.clone());

        let resp = post(&url, Some(data_json)).await?;

        if resp.get("code").and_then(|c| c.as_i64()).unwrap_or(-1) != 0 {
            return Err(Error::Api(
                resp["msg"].as_str().unwrap_or("API error").to_string(),
            ));
        }

        let article_id = resp["articleId"]
            .as_str()
            .ok_or_else(|| Error::Api("Missing articleId in response".to_string()))?
            .to_string();

        Ok(article_id)
    }

    /// 查询文章列表
    ///
    /// * `type` 查询类型,来自 [ArticleListType]
    /// * `tag` 指定查询标签,可选
    /// * `page` 页码
    /// * `size` 每页数量
    ///
    /// 返回文章列表
    pub async fn list(
        &self,
        type_: ArticleListType,
        page: u32,
        size: u32,
        tag: Option<&str>,
    ) -> Result<ArticleList, Error> {
        let base = if let Some(tag) = tag {
            format!("tag/{}", tag)
        } else {
            "recent".to_string()
        };

        let url = build_http_path(
            &format!("api/articles/{}{}", base, type_.to_code()),
            &[
                ("p", page.to_string()),
                ("size", size.to_string()),
                ("apiKey", self.api_key.clone()),
            ],
        );

        let rsp = get(&url).await?;

        if rsp.get("code").and_then(|c| c.as_i64()).unwrap_or(-1) != 0 {
            return Err(Error::Api(
                rsp["msg"].as_str().unwrap_or("API error").to_string(),
            ));
        }

        ArticleList::from_value(&rsp["data"])
    }

    /// 查询文章列表
    ///
    /// - `user` 指定用户
    /// - `page` 页码
    /// - `size` 每页数量
    ///
    /// 返回文章列表
    pub async fn list_by_user(
        &self,
        user: &str,
        page: u32,
        size: u32,
    ) -> Result<ArticleList, Error> {
        let url = build_http_path(
            &format!("api/articles/user/{}", user),
            &[
                ("p", page.to_string()),
                ("size", size.to_string()),
                ("apiKey", self.api_key.clone()),
            ],
        );

        let rsp = get(&url).await?;

        if rsp.get("code").and_then(|c| c.as_i64()).unwrap_or(-1) != 0 {
            return Err(Error::Api(
                rsp["msg"].as_str().unwrap_or("API error").to_string(),
            ));
        }

        ArticleList::from_value(&rsp["data"])
    }

    /// 获取文章详情
    ///
    /// - `id` 文章id
    /// - `p` 评论页码
    ///
    /// 返回文章详情 [ArticleDetail]
    pub async fn detail(&self, id: &str, p: u32) -> Result<ArticleDetail, Error> {
        let url = build_http_path(
            &format!("api/article/{}", id),
            &[("p", p.to_string()), ("apiKey", self.api_key.clone())],
        );

        let rsp = get(&url).await?;

        if rsp.get("code").and_then(|c| c.as_i64()).unwrap_or(-1) != 0 {
            return Err(Error::Api(
                rsp["msg"].as_str().unwrap_or("API error").to_string(),
            ));
        }

        let data = &rsp["data"];
        let article_node = &data["article"];
        let mut article_detail = ArticleDetail::from_value(article_node)?;
        article_detail.pagination = Some(Pagination::from_value(&data["pagination"])?);

        Ok(article_detail)
    }

    /// 点赞/取消点赞文章
    ///
    /// - `id` 文章id
    /// - `like` 点赞类型,true 为点赞,false 为点踩
    ///
    /// 返回文章点赞状态,true 为点赞,false 为点踩
    pub async fn vote(&self, id: &str, like: bool) -> Result<bool, Error> {
        let url = format!("vote/{}/article", if like { "up" } else { "down" });

        let data = json!({
            "dataId": id,
            "apiKey": self.api_key,
        });

        let rsp = post(&url, Some(data)).await?;

        if rsp.get("code").and_then(|c| c.as_i64()).unwrap_or(-1) != 0 {
            return Err(Error::Api(
                rsp["msg"].as_str().unwrap_or("API error").to_string(),
            ));
        }

        Ok(rsp.get("type").and_then(|v| v.as_i64()) == Some(-1))
    }

    /// 感谢文章
    ///
    /// - `id` 文章id
    ///
    /// 返回执行结果
    pub async fn thank(&self, id: &str) -> Result<ResponseResult, Error> {
        let url = build_http_path(
            "article/thank",
            &[
                ("articleId", id.to_string()),
                ("apiKey", self.api_key.clone()),
            ],
        );

        let rsp = post(&url, None).await?;

        ResponseResult::from_value(&rsp)
    }

    /// 收藏/取消收藏文章
    ///
    /// - `id` 文章id
    ///
    /// 返回执行结果
    pub async fn follow(&self, id: &str) -> Result<ResponseResult, Error> {
        let url = "follow/article".to_string();

        let data = json!({
            "apiKey": self.api_key,
            "followingId": id,
        });

        let rsp = post(&url, Some(data)).await?;

        ResponseResult::from_value(&rsp)
    }

    /// 关注/取消关注文章
    ///
    /// - `followingId` 文章id
    ///
    /// 返回执行结果
    pub async fn watch(&self, following_id: &str) -> Result<ResponseResult, Error> {
        let url = "follow/article-watch".to_string();

        let data = json!({
            "apiKey": self.api_key,
            "followingId": following_id,
        });

        let rsp = post(&url, Some(data)).await?;

        ResponseResult::from_value(&rsp)
    }

    /// 打赏文章
    ///
    /// - `id` 文章id
    ///
    /// 返回执行结果
    pub async fn reward(&self, id: &str) -> Result<ResponseResult, Error> {
        let url = build_http_path("article/reward", &[("articleId", id.to_string())]);

        let data = json!({
            "apiKey": self.api_key,
        });

        let rsp = post(&url, Some(data)).await?;

        ResponseResult::from_value(&rsp)
    }

    /// 获取文章在线人数
    ///
    /// - `id` 文章id
    ///
    /// 返回在线人数
    pub async fn heat(&self, id: &str) -> Result<u32, Error> {
        let url = build_http_path(
            &format!("api/article/heat/{}", id),
            &[("apiKey", self.api_key.clone())],
        );

        let rsp = get(&url).await?;

        if rsp.get("code").and_then(|c| c.as_i64()).unwrap_or(-1) != 0 {
            return Err(Error::Api(
                rsp["msg"].as_str().unwrap_or("API error").to_string(),
            ));
        }

        let heat = rsp["articleHeat"]
            .as_u64()
            .ok_or_else(|| Error::Api("Missing heat data in response".to_string()))?
            as u32;

        Ok(heat)
    }

    /// 添加文章监听器
    ///
    /// - `id` 文章id
    /// - `type_` 文章类型
    /// - `callback` 监听回调
    ///
    /// 返回 WebSocketClient
    pub async fn add_listener(
        &self,
        id: &str,
        type_: ArticleType,
        callback: ArticleListener,
    ) -> Result<WebSocketClient, Error> {
        let url = build_ws_url(
            "fishpi.cn",
            "article-channel",
            &[
                ("apiKey", self.api_key.clone()),
                ("articleId", id.to_string()),
                ("articleType", (type_ as u8).to_string()),
            ],
        )
        .map_err(|e| Error::Api(format!("WebSocket URL build failed: {}", e)))?;

        let handler = ArticleMessageHandler::new(callback);
        let ws = WebSocketClient::connect(&url, handler)
            .await
            .map_err(|e| Error::Api(format!("WebSocket connection failed: {}", e)))?;

        Ok(ws)
    }
}