fishpi-sdk 1.0.0

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
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
use std::sync::Arc;
use std::time::Duration;

use reqwest::{Client, Method, Proxy, Response, StatusCode};
use serde::Serialize;
use serde::de::DeserializeOwned;
use serde_json::Value;
use tokio::time::sleep;

use super::{Config, USER_AGENT};
use crate::ids::ApiKey;
use crate::model::misc::LoginData;
use crate::utils::error::Error;
use crate::utils::{HttpProxyConfig, build_http_path};

const VISITOR_VERIFY_MESSAGE: &str = "当前网络触发摸鱼派访客验证,请完成验证后再重试";

struct Inner {
    http: Client,
    config: Config,
    api_key: ApiKey,
}

/// 摸鱼派客户端,可 `Clone`。
#[derive(Clone)]
pub struct FishPi {
    inner: Arc<Inner>,
}

impl FishPi {
    /// 开始构造客户端。
    pub fn builder() -> FishPiBuilder {
        FishPiBuilder::default()
    }

    /// 当前 api_key。
    pub fn api_key(&self) -> &ApiKey {
        &self.inner.api_key
    }

    /// 当前服务域名。
    pub(crate) fn domain(&self) -> &str {
        &self.inner.config.domain
    }

    /// 聊天室消息来源标识(如 `Rust/0.2.0`)。
    pub(crate) fn client_tag(&self) -> &str {
        &self.inner.config.client_tag
    }

    /// 是否已持有 api_key。
    pub fn is_authenticated(&self) -> bool {
        !self.inner.api_key.is_empty()
    }

    /// 换一个 api_key,返回新客户端。
    pub fn with_api_key(&self, api_key: impl Into<ApiKey>) -> Self {
        Self {
            inner: Arc::new(Inner {
                http: self.inner.http.clone(),
                config: self.inner.config.clone(),
                api_key: api_key.into(),
            }),
        }
    }

    /// 换一个聊天室消息来源标识,返回新客户端(复用连接池)。
    pub fn with_client_tag(&self, client_tag: impl Into<String>) -> Self {
        let mut config = self.inner.config.clone();
        config.client_tag = client_tag.into();
        Self {
            inner: Arc::new(Inner {
                http: self.inner.http.clone(),
                config,
                api_key: self.inner.api_key.clone(),
            }),
        }
    }
}

/// [`FishPi`] 构造器。
#[derive(Default)]
pub struct FishPiBuilder {
    api_key: Option<ApiKey>,
    domain: Option<String>,
    connect_timeout: Option<Duration>,
    timeout: Option<Duration>,
    proxy: Option<HttpProxyConfig>,
    max_retries: Option<u32>,
    client_tag: Option<String>,
}

impl FishPiBuilder {
    /// 设置 api_key。
    pub fn api_key(mut self, api_key: impl Into<ApiKey>) -> Self {
        self.api_key = Some(api_key.into());
        self
    }

    /// 设置服务域名(默认 `fishpi.cn`)。
    pub fn domain(mut self, domain: impl Into<String>) -> Self {
        self.domain = Some(domain.into());
        self
    }

    /// 连接超时。
    pub fn connect_timeout(mut self, d: Duration) -> Self {
        self.connect_timeout = Some(d);
        self
    }

    /// 整体请求超时。
    pub fn timeout(mut self, d: Duration) -> Self {
        self.timeout = Some(d);
        self
    }

    /// 代理配置。
    pub fn proxy(mut self, proxy: HttpProxyConfig) -> Self {
        self.proxy = Some(proxy);
        self
    }

    /// 传输层失败最大重试次数(默认 2)。
    pub fn max_retries(mut self, n: u32) -> Self {
        self.max_retries = Some(n);
        self
    }

    /// 聊天室消息来源标识,默认 `Rust/<版本>`。
    pub fn client_tag(mut self, tag: impl Into<String>) -> Self {
        self.client_tag = Some(tag.into());
        self
    }

    /// 构造客户端。
    pub fn build(self) -> Result<FishPi, Error> {
        let mut config = Config::default();
        if let Some(d) = self.domain {
            config.domain = d;
        }
        if let Some(t) = self.client_tag {
            config.client_tag = t;
        }
        if let Some(t) = self.connect_timeout {
            config.connect_timeout = t;
        }
        if let Some(t) = self.timeout {
            config.timeout = t;
        }
        if let Some(p) = self.proxy {
            config.proxy = p;
        }
        if let Some(n) = self.max_retries {
            config.max_retries = n;
        }

        let http = build_http_client(&config)?;
        Ok(FishPi {
            inner: Arc::new(Inner {
                http,
                config,
                api_key: self.api_key.unwrap_or_else(|| ApiKey::new("")),
            }),
        })
    }

    /// 构造并登录,返回带鉴权的客户端。
    pub async fn login(self, data: &LoginData) -> Result<FishPi, Error> {
        self.build()?.login(data).await
    }
}

fn build_http_client(config: &Config) -> Result<Client, Error> {
    let builder = Client::builder();
    let builder = match config.proxy.mode {
        crate::utils::HttpProxyMode::NoProxy => builder.no_proxy(),
        crate::utils::HttpProxyMode::System => builder,
        crate::utils::HttpProxyMode::Custom => {
            let proxy_url = config
                .proxy
                .proxy_url
                .as_deref()
                .map(str::trim)
                .filter(|v| !v.is_empty())
                .ok_or_else(|| Error::decode("proxy url is empty"))?;
            builder.no_proxy().proxy(Proxy::all(proxy_url)?)
        }
    };

    builder
        .connect_timeout(config.connect_timeout)
        .timeout(config.timeout)
        .build()
        .map_err(Error::from)
}

impl FishPi {
    fn full_url(&self, path: &str) -> String {
        format!(
            "https://{}/{}",
            self.inner.config.domain,
            path.trim_start_matches('/')
        )
    }

    /// 在路径上附加 `apiKey` 查询参数。
    pub(crate) fn with_key(&self, path: &str) -> String {
        build_http_path(path, &[("apiKey", self.inner.api_key.to_string())])
    }

    /// 发起请求,返回原始 JSON `Value`。
    pub(crate) async fn send_raw(
        &self,
        method: Method,
        path: &str,
        body: Option<&Value>,
    ) -> Result<Value, Error> {
        let url = self.full_url(path);
        let max_retries = self.inner.config.max_retries;
        let mut attempt = 0;

        loop {
            let mut req = self
                .inner
                .http
                .request(method.clone(), &url)
                .header("User-Agent", USER_AGENT)
                .header("Referer", format!("https://{}/", self.inner.config.domain));
            if let Some(b) = body {
                req = req.json(b);
            }

            let resp = match req.send().await {
                Ok(resp) => resp,
                Err(err) => {
                    if attempt < max_retries {
                        sleep(backoff(attempt)).await;
                        attempt += 1;
                        continue;
                    }
                    return Err(Error::from(err));
                }
            };

            let status = resp.status();
            if status.is_success() {
                return parse_json_response(resp).await;
            }

            match status {
                StatusCode::UNAUTHORIZED => return Err(Error::Unauthorized),
                StatusCode::NOT_FOUND => {
                    return Err(Error::NotFound {
                        resource: path.to_string(),
                    });
                }
                StatusCode::TOO_MANY_REQUESTS => {
                    return Err(Error::RateLimited {
                        retry_after: retry_after(&resp),
                    });
                }
                StatusCode::SERVICE_UNAVAILABLE if attempt < max_retries => {
                    sleep(backoff(attempt)).await;
                    attempt += 1;
                    continue;
                }
                _ => {
                    return Err(Error::decode(format!("HTTP error: {}", status)));
                }
            }
        }
    }
}

fn backoff(attempt: u32) -> Duration {
    Duration::from_millis(300 * (attempt as u64 + 1))
}

fn retry_after(resp: &Response) -> Option<Duration> {
    resp.headers()
        .get(reqwest::header::RETRY_AFTER)
        .and_then(|v| v.to_str().ok())
        .and_then(|s| s.trim().parse::<u64>().ok())
        .map(Duration::from_secs)
}

/// 校验 `{code,msg}` 信封,返回原始 `Value`。
fn check_envelope(value: Value) -> Result<Value, Error> {
    let code = value.get("code").and_then(Value::as_i64);
    match code {
        None | Some(0) => Ok(value),
        Some(c) => {
            let msg = value
                .get("msg")
                .and_then(Value::as_str)
                .unwrap_or("API error")
                .to_string();
            Err(map_business(c, msg))
        }
    }
}

fn map_business(code: i64, msg: String) -> Error {
    match code {
        -1 if msg.contains("Auth") || msg.contains("登录") || msg.contains("认证") => {
            Error::Unauthorized
        }
        _ => Error::business(code, msg),
    }
}

impl FishPi {
    /// GET 请求,反序列化 `data`。
    pub(crate) async fn get_data<T: DeserializeOwned>(&self, path: &str) -> Result<T, Error> {
        let value = self.send_raw(Method::GET, path, None).await?;
        let value = check_envelope(value)?;
        take_data(value)
    }

    /// 带 body 的 GET 请求,body 注入 apiKey,反序列化 `data`。
    pub(crate) async fn get_data_with_key<T: DeserializeOwned>(
        &self,
        path: &str,
    ) -> Result<T, Error> {
        let mut body = Value::Object(Default::default());
        self.inject_api_key(&mut body);
        let value = self.send_raw(Method::GET, path, Some(&body)).await?;
        let value = check_envelope(value)?;
        take_data(value)
    }

    /// POST 请求,反序列化 `data`。
    pub(crate) async fn post_data<T: DeserializeOwned, B: Serialize>(
        &self,
        path: &str,
        body: &B,
    ) -> Result<T, Error> {
        let mut body = serde_json::to_value(body).map_err(|e| Error::decode(e.to_string()))?;
        self.inject_api_key(&mut body);
        let value = self.send_raw(Method::POST, path, Some(&body)).await?;
        let value = check_envelope(value)?;
        take_data(value)
    }

    /// GET 请求,校验信封后返回整个响应 `Value`。
    pub(crate) async fn get_raw(&self, path: &str) -> Result<Value, Error> {
        let value = self.send_raw(Method::GET, path, None).await?;
        check_envelope(value)
    }

    /// POST 请求,校验信封后返回整个响应 `Value`。
    pub(crate) async fn post_raw<B: Serialize>(
        &self,
        path: &str,
        body: &B,
    ) -> Result<Value, Error> {
        let mut body = serde_json::to_value(body).map_err(|e| Error::decode(e.to_string()))?;
        self.inject_api_key(&mut body);
        let value = self.send_raw(Method::POST, path, Some(&body)).await?;
        check_envelope(value)
    }

    /// PUT 请求,校验信封后返回整个响应 `Value`。
    pub(crate) async fn put_raw<B: Serialize>(&self, path: &str, body: &B) -> Result<Value, Error> {
        let mut body = serde_json::to_value(body).map_err(|e| Error::decode(e.to_string()))?;
        self.inject_api_key(&mut body);
        let value = self.send_raw(Method::PUT, path, Some(&body)).await?;
        check_envelope(value)
    }

    /// DELETE 请求,校验信封后返回整个响应 `Value`。
    pub(crate) async fn delete_raw<B: Serialize>(
        &self,
        path: &str,
        body: &B,
    ) -> Result<Value, Error> {
        let mut body = serde_json::to_value(body).map_err(|e| Error::decode(e.to_string()))?;
        self.inject_api_key(&mut body);
        let value = self.send_raw(Method::DELETE, path, Some(&body)).await?;
        check_envelope(value)
    }

    /// GET 请求,返回响应正文纯文本(HTML 等非 JSON 内容)。
    pub(crate) async fn get_text(&self, path: &str) -> Result<String, Error> {
        let url = self.full_url(path);
        let resp = self
            .inner
            .http
            .get(&url)
            .header("User-Agent", USER_AGENT)
            .header("Referer", format!("https://{}/", self.inner.config.domain))
            .send()
            .await?;
        let status = resp.status();
        if !status.is_success() {
            return Err(Error::decode(format!("HTTP error: {}", status)));
        }
        resp.text().await.map_err(Error::from)
    }

    /// 在 JSON 对象 body 中注入 `apiKey`。
    fn inject_api_key(&self, body: &mut Value) {
        if self.inner.api_key.is_empty() {
            return;
        }
        if let Value::Object(map) = body
            && !map.contains_key("apiKey")
        {
            map.insert(
                "apiKey".to_string(),
                Value::String(self.inner.api_key.to_string()),
            );
        }
    }
}

/// 取 `data` 字段并反序列化。
fn take_data<T: DeserializeOwned>(mut value: Value) -> Result<T, Error> {
    let data = match value.get_mut("data") {
        Some(d) => d.take(),
        None => value,
    };
    let data = if let Value::String(s) = &data {
        serde_json::from_str(s).map_err(|e| Error::decode(e.to_string()))?
    } else {
        data
    };
    serde_json::from_value(data).map_err(|e| Error::decode(e.to_string()))
}

async fn parse_json_response(resp: Response) -> Result<Value, Error> {
    let final_path = resp.url().path().to_string();
    let content_type = resp
        .headers()
        .get("content-type")
        .and_then(|v| v.to_str().ok())
        .unwrap_or("")
        .to_ascii_lowercase();
    let text = resp.text().await?;

    if is_visitor_verify_response(&final_path, &content_type, &text) {
        return Err(Error::decode(VISITOR_VERIFY_MESSAGE));
    }

    serde_json::from_str::<Value>(&text).map_err(|err| {
        if is_html_response(&content_type, &text) {
            Error::decode("服务返回了网页内容,无法作为 API 响应解析")
        } else {
            Error::decode(format!("Failed to parse response body: {err}"))
        }
    })
}

fn is_visitor_verify_response(path: &str, content_type: &str, body: &str) -> bool {
    let lower = body.to_ascii_lowercase();
    path == "/test"
        || (is_html_response(content_type, body)
            && (body.contains("访客验证")
                || body.contains("在继续浏览摸鱼派社区前请验证")
                || body.contains("/validateCaptcha")
                || lower.contains("initgeetest4")))
}

fn is_html_response(content_type: &str, body: &str) -> bool {
    content_type.contains("text/html")
        || body
            .trim_start()
            .to_ascii_lowercase()
            .starts_with("<!doctype html")
}

impl FishPi {
    /// 登录,返回持有 api_key 的新客户端。
    pub async fn login(&self, data: &LoginData) -> Result<FishPi, Error> {
        let body = data.to_value()?;
        let rsp = self
            .send_raw(Method::POST, "api/getKey", Some(&body))
            .await?;
        let rsp = check_envelope(rsp)?;
        let token = rsp
            .get("Key")
            .and_then(Value::as_str)
            .unwrap_or("")
            .trim()
            .to_string();
        Ok(self.with_api_key(token))
    }
}