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
//! API 请求构造模块。
//! 构造 API 请求对象,将业务参数序列化为 JSON 字符串作为 biz_content。
use serde::{Deserialize, Serialize};
/// API request struct
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct ApiRequest {
/// API method name (e.g. "market_state", "place_order")
pub method: String,
/// Business parameters JSON string
pub biz_content: String,
/// Optional per-request API version override (e.g. "3.0")
#[serde(skip_serializing_if = "Option::is_none")]
pub version: Option<String>,
}
impl ApiRequest {
/// Create an API request
/// method: API method name
/// biz_content: business parameters JSON string
pub fn new(method: impl Into<String>, biz_content: impl Into<String>) -> Self {
Self {
method: method.into(),
biz_content: biz_content.into(),
version: None,
}
}
/// Create an API request with a specific API version
pub fn with_version(
method: impl Into<String>,
biz_content: impl Into<String>,
version: impl Into<String>,
) -> Self {
Self {
method: method.into(),
biz_content: biz_content.into(),
version: Some(version.into()),
}
}
/// Create an API request from serializable business parameters
pub fn from_params<T: Serialize>(
method: impl Into<String>,
params: &T,
) -> Result<Self, serde_json::Error> {
let biz_content = serde_json::to_string(params)?;
Ok(Self {
method: method.into(),
biz_content,
version: None,
})
}
}
#[cfg(test)]
mod tests;