yang-base 0.1.0

个人使用构建后端服务器
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
//! HTTP 请求构建器实现
//!
//! 提供链式调用接口构建 HTTP 请求。

use crate::error::BaseError;
use crate::http::response::Response;
use reqwest::header::{HeaderMap, HeaderName, HeaderValue};
use reqwest::{Client, Method};
use serde::Serialize;
use std::time::Duration;

/// HTTP 请求构建器
///
/// 提供链式调用接口构建 HTTP 请求
///
/// # 示例
///
/// ```rust,ignore
/// use yang_base::http::HttpClient;
///
/// let response = HttpClient::global()?
///     .get("https://api.example.com/users")
///     .header("X-Custom-Header", "value")
///     .query("page", "1")
///     .bearer_token("your_token")
///     .timeout(60)
///     .send()
///     .await?;
/// ```
pub struct RequestBuilder {
    /// reqwest 客户端
    client: Client,

    /// HTTP 方法
    method: Method,

    /// 请求 URL
    url: String,

    /// 请求头
    headers: HeaderMap,

    /// 查询参数
    query_params: Vec<(String, String)>,

    /// 请求体
    body: Option<Vec<u8>>,

    /// 超时时间
    timeout: Duration,

    /// Token(可选)
    token: Option<String>,
}

impl RequestBuilder {
    /// 创建新的请求构建器
    ///
    /// # 参数
    ///
    /// - `client`: reqwest 客户端
    /// - `method`: HTTP 方法
    /// - `url`: 请求 URL
    /// - `timeout`: 超时时间
    /// - `token`: 默认 Token
    pub(crate) fn new(
        client: Client,
        method: Method,
        url: String,
        timeout: Duration,
        token: Option<String>,
    ) -> Self {
        Self {
            client,
            method,
            url,
            headers: HeaderMap::new(),
            query_params: Vec::new(),
            body: None,
            timeout,
            token,
        }
    }

    /// 设置请求头
    ///
    /// # 参数
    ///
    /// - `name`: 请求头名称
    /// - `value`: 请求头值
    ///
    /// # 示例
    ///
    /// ```rust,ignore
    /// let response = client
    ///     .get("https://api.example.com/users")
    ///     .header("X-Custom-Header", "value")
    ///     .send()
    ///     .await?;
    /// ```
    pub fn header(mut self, name: &str, value: &str) -> Self {
        if let (Ok(name), Ok(value)) = (
            HeaderName::from_bytes(name.as_bytes()),
            HeaderValue::from_str(value),
        ) {
            self.headers.insert(name, value);
        }
        self
    }

    /// 批量设置请求头
    ///
    /// # 参数
    ///
    /// - `headers`: 请求头列表
    ///
    /// # 示例
    ///
    /// ```rust,ignore
    /// let response = client
    ///     .get("https://api.example.com/users")
    ///     .headers(vec![
    ///         ("X-Custom-Header-1", "value1"),
    ///         ("X-Custom-Header-2", "value2"),
    ///     ])
    ///     .send()
    ///     .await?;
    /// ```
    pub fn headers(mut self, headers: Vec<(&str, &str)>) -> Self {
        for (name, value) in headers {
            if let (Ok(name), Ok(value)) = (
                HeaderName::from_bytes(name.as_bytes()),
                HeaderValue::from_str(value),
            ) {
                self.headers.insert(name, value);
            }
        }
        self
    }

    /// 设置 Content-Type
    ///
    /// # 参数
    ///
    /// - `content_type`: Content-Type 值
    ///
    /// # 示例
    ///
    /// ```rust,ignore
    /// let response = client
    ///     .post("https://api.example.com/users")
    ///     .content_type("application/json")
    ///     .send()
    ///     .await?;
    /// ```
    pub fn content_type(self, content_type: &str) -> Self {
        self.header("Content-Type", content_type)
    }

    /// 设置 Bearer Token
    ///
    /// # 参数
    ///
    /// - `token`: Token 字符串
    ///
    /// # 示例
    ///
    /// ```rust,ignore
    /// let response = client
    ///     .get("https://api.example.com/users")
    ///     .bearer_token("your_token")
    ///     .send()
    ///     .await?;
    /// ```
    pub fn bearer_token(mut self, token: &str) -> Self {
        self.token = Some(token.to_string());
        self
    }

    /// 设置 User-Agent
    ///
    /// # 参数
    ///
    /// - `user_agent`: User-Agent 值
    ///
    /// # 示例
    ///
    /// ```rust,ignore
    /// let response = client
    ///     .get("https://api.example.com/users")
    ///     .user_agent("MyApp/1.0")
    ///     .send()
    ///     .await?;
    /// ```
    pub fn user_agent(self, user_agent: &str) -> Self {
        self.header("User-Agent", user_agent)
    }

    /// 添加查询参数
    ///
    /// # 参数
    ///
    /// - `key`: 参数名
    /// - `value`: 参数值
    ///
    /// # 示例
    ///
    /// ```rust,ignore
    /// let response = client
    ///     .get("https://api.example.com/users")
    ///     .query("page", "1")
    ///     .query("limit", "10")
    ///     .send()
    ///     .await?;
    /// ```
    pub fn query(mut self, key: &str, value: &str) -> Self {
        self.query_params.push((key.to_string(), value.to_string()));
        self
    }

    /// 批量添加查询参数
    ///
    /// # 参数
    ///
    /// - `params`: 查询参数列表
    ///
    /// # 示例
    ///
    /// ```rust,ignore
    /// let response = client
    ///     .get("https://api.example.com/users")
    ///     .queries(vec![("page", "1"), ("limit", "10")])
    ///     .send()
    ///     .await?;
    /// ```
    pub fn queries(mut self, params: Vec<(&str, &str)>) -> Self {
        for (key, value) in params {
            self.query_params.push((key.to_string(), value.to_string()));
        }
        self
    }

    /// 设置 JSON 请求体
    ///
    /// # 参数
    ///
    /// - `json`: 可序列化为 JSON 的数据
    ///
    /// # 返回
    ///
    /// - `Ok(Self)`: 设置成功
    /// - `Err(BaseError)`: 序列化失败
    ///
    /// # 示例
    ///
    /// ```rust,ignore
    /// #[derive(Serialize)]
    /// struct User {
    ///     name: String,
    ///     email: String,
    /// }
    ///
    /// let user = User {
    ///     name: "Alice".to_string(),
    ///     email: "alice@example.com".to_string(),
    /// };
    ///
    /// let response = client
    ///     .post("https://api.example.com/users")
    ///     .json(&user)?
    ///     .send()
    ///     .await?;
    /// ```
    pub fn json<T: Serialize>(mut self, json: &T) -> Result<Self, BaseError> {
        let json_str =
            serde_json::to_vec(json).map_err(|e| BaseError::JsonSerializeFailed(e.to_string()))?;

        self.body = Some(json_str);
        self = self.content_type("application/json");

        Ok(self)
    }

    /// 设置表单请求体
    ///
    /// # 参数
    ///
    /// - `form`: 表单数据
    ///
    /// # 示例
    ///
    /// ```rust,ignore
    /// let response = client
    ///     .post("https://api.example.com/login")
    ///     .form(vec![("username", "alice"), ("password", "secret")])
    ///     .send()
    ///     .await?;
    /// ```
    pub fn form(mut self, form: Vec<(&str, &str)>) -> Self {
        let form_str = form
            .iter()
            .map(|(k, v)| format!("{}={}", k, v))
            .collect::<Vec<_>>()
            .join("&");

        self.body = Some(form_str.into_bytes());
        self = self.content_type("application/x-www-form-urlencoded");

        self
    }

    /// 设置原始字节请求体
    ///
    /// # 参数
    ///
    /// - `body`: 字节数据
    ///
    /// # 示例
    ///
    /// ```rust,ignore
    /// let response = client
    ///     .post("https://api.example.com/upload")
    ///     .body(vec![0x00, 0x01, 0x02])
    ///     .send()
    ///     .await?;
    /// ```
    pub fn body(mut self, body: Vec<u8>) -> Self {
        self.body = Some(body);
        self
    }

    /// 设置文本请求体
    ///
    /// # 参数
    ///
    /// - `text`: 文本数据
    ///
    /// # 示例
    ///
    /// ```rust,ignore
    /// let response = client
    ///     .post("https://api.example.com/notes")
    ///     .text("Hello, World!")
    ///     .send()
    ///     .await?;
    /// ```
    pub fn text(mut self, text: &str) -> Self {
        self.body = Some(text.as_bytes().to_vec());
        self = self.content_type("text/plain");
        self
    }

    /// 设置超时时间
    ///
    /// # 参数
    ///
    /// - `timeout_secs`: 超时时间(秒)
    ///
    /// # 示例
    ///
    /// ```rust,ignore
    /// let response = client
    ///     .get("https://api.example.com/users")
    ///     .timeout(60)
    ///     .send()
    ///     .await?;
    /// ```
    pub fn timeout(mut self, timeout_secs: u64) -> Self {
        self.timeout = Duration::from_secs(timeout_secs);
        self
    }

    /// 发送请求
    ///
    /// # 返回
    ///
    /// - `Ok(Response)`: 响应对象
    /// - `Err(BaseError)`: 请求失败
    ///
    /// # 示例
    ///
    /// ```rust,ignore
    /// let response = client
    ///     .get("https://api.example.com/users")
    ///     .send()
    ///     .await?;
    /// ```
    pub async fn send(self) -> Result<Response, BaseError> {
        // 构建请求
        let mut request = self
            .client
            .request(self.method, &self.url)
            .timeout(self.timeout);

        // 添加请求头
        request = request.headers(self.headers);

        // 添加 Token
        if let Some(token) = self.token {
            request = request.bearer_auth(token);
        }

        // 添加查询参数
        if !self.query_params.is_empty() {
            request = request.query(&self.query_params);
        }

        // 添加请求体
        if let Some(body) = self.body {
            request = request.body(body);
        }

        // 发送请求
        let response = request
            .send()
            .await
            .map_err(|e| BaseError::HttpRequestFailed(e.to_string()))?;

        Ok(Response::new(response))
    }
}