1use futu_core::error::{FutuError, Result};
2use futu_core::proto_id;
3use futu_net::client::FutuClient;
4
5use crate::types::{KLType, KLine, RehabType, Security};
6
7#[derive(Debug, Clone)]
9pub struct KLineResult {
10 pub security: Security,
11 pub kl_list: Vec<KLine>,
12}
13
14pub async fn get_kl(
22 client: &FutuClient,
23 security: &Security,
24 rehab_type: RehabType,
25 kl_type: KLType,
26 req_num: i32,
27) -> Result<KLineResult> {
28 let req = futu_proto::qot_get_kl::Request {
29 c2s: futu_proto::qot_get_kl::C2s {
30 rehab_type: rehab_type as i32,
31 kl_type: kl_type as i32,
32 security: security.to_proto(),
33 req_num,
34 header: None,
35 },
36 };
37
38 let body = prost::Message::encode_to_vec(&req);
39 let resp_frame = client.request(proto_id::QOT_GET_KL, body).await?;
40
41 let resp: futu_proto::qot_get_kl::Response =
42 prost::Message::decode(resp_frame.body.as_ref()).map_err(FutuError::Proto)?;
43
44 if resp.ret_type != 0 {
45 return Err(FutuError::ServerError {
46 ret_type: resp.ret_type,
47 msg: resp.ret_msg.unwrap_or_default(),
48 });
49 }
50
51 let s2c = resp
52 .s2c
53 .ok_or(FutuError::Codec("missing s2c in GetKL".into()))?;
54
55 Ok(KLineResult {
56 security: Security::from_proto(&s2c.security),
57 kl_list: s2c.kl_list.iter().map(KLine::from_proto).collect(),
58 })
59}