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
use crate::error::Error;
use crate::model::{Request, Response};
#[cfg(test)]
use mockito;
use reqwest::header::{HeaderValue, CONTENT_TYPE};
use serde_json::json;
#[cfg(not(test))]
fn jqdata_url() -> String {
String::from("https://dataapi.joinquant.com/apis")
}
#[cfg(test)]
fn jqdata_url() -> String {
mockito::server_url()
}
pub struct JqdataClient {
token: String,
}
fn get_token(mob: &str, pwd: &str, reuse: bool) -> Result<String, Error> {
let method = if reuse {
"get_current_token"
} else {
"get_token"
};
let token_req = json!({
"method": method,
"mob": mob,
"pwd": pwd,
});
let client = reqwest::blocking::Client::new();
let response = client
.post(&jqdata_url())
.header(CONTENT_TYPE, HeaderValue::from_static("application/json"))
.body(token_req.to_string())
.send()?;
let token: String = response.text()?;
if token.starts_with("error") {
return Err(Error::Server(token));
}
Ok(token)
}
impl JqdataClient {
pub fn with_credential(mob: &str, pwd: &str) -> Result<Self, Error> {
let token = get_token(mob, pwd, true)?;
Ok(JqdataClient { token })
}
pub fn with_token(token: &str) -> Result<Self, Error> {
Ok(JqdataClient {
token: token.to_string(),
})
}
pub fn execute<C: Request + Response>(&self, command: C) -> Result<C::Output, Error> {
let req_body = command.request(&self.token)?;
let client = reqwest::blocking::Client::new();
let response = client
.post(&jqdata_url())
.header(CONTENT_TYPE, HeaderValue::from_static("application/json"))
.body(req_body)
.send()?;
let output = command.response(response)?;
Ok(output)
}
}
#[cfg(test)]
mod tests {
use super::*;
use crate::model::*;
use mockito::mock;
#[test]
fn test_error_response() {
let response_body = "error: invalid token";
let _m = mock("POST", "/")
.with_status(200)
.with_body(response_body)
.create();
let client = JqdataClient::with_token("abc").unwrap();
let response = client.execute(GetAllSecurities {
code: SecurityKind::Stock,
date: None,
});
assert!(response.is_err());
}
#[test]
fn test_get_all_securities() {
let response_body = {
let mut s = String::from("code,display_name,name,start_date,end_date,type\n");
s.push_str("000001.XSHE,平安银行,PAYH,1991-04-03,2200-01-01,stock\n");
s.push_str("000002.XSHE,万科A,WKA,1991-01-29,2200-01-01,stock\n");
s
};
let _m = mock("POST", "/")
.with_status(200)
.with_body(&response_body)
.create();
let client = JqdataClient::with_token("abc").unwrap();
let ss = client
.execute(GetAllSecurities {
code: SecurityKind::Stock,
date: None,
})
.unwrap();
assert_eq!(
vec![
Security {
code: "000001.XSHE".to_string(),
display_name: "平安银行".to_string(),
name: "PAYH".to_string(),
start_date: "1991-04-03".to_string(),
end_date: "2200-01-01".to_string(),
kind: SecurityKind::Stock,
parent: None,
},
Security {
code: "000002.XSHE".to_string(),
display_name: "万科A".to_string(),
name: "WKA".to_string(),
start_date: "1991-01-29".to_string(),
end_date: "2200-01-01".to_string(),
kind: SecurityKind::Stock,
parent: None,
}
],
ss
);
}
}