Skip to main content

futu_qot/
broker.rs

1use futu_core::error::{FutuError, Result};
2use futu_core::proto_id;
3use futu_net::client::FutuClient;
4
5use crate::types::Security;
6
7/// 经纪队列数据项
8#[derive(Debug, Clone)]
9pub struct BrokerEntry {
10    pub id: i64,
11    pub name: String,
12    pub pos: i32,
13}
14
15impl BrokerEntry {
16    pub fn from_proto(b: &futu_proto::qot_common::Broker) -> Self {
17        Self {
18            id: b.id,
19            name: b.name.clone(),
20            pos: b.pos,
21        }
22    }
23}
24
25/// 经纪队列数据
26#[derive(Debug, Clone)]
27pub struct BrokerData {
28    pub security: Security,
29    pub ask_list: Vec<BrokerEntry>,
30    pub bid_list: Vec<BrokerEntry>,
31}
32
33/// 获取经纪队列
34///
35/// 需要先订阅 SubType::Broker。
36pub async fn get_broker(client: &FutuClient, security: &Security) -> Result<BrokerData> {
37    let req = futu_proto::qot_get_broker::Request {
38        c2s: futu_proto::qot_get_broker::C2s {
39            security: security.to_proto(),
40            header: None,
41        },
42    };
43
44    let body = prost::Message::encode_to_vec(&req);
45    let resp_frame = client.request(proto_id::QOT_GET_BROKER, body).await?;
46
47    let resp: futu_proto::qot_get_broker::Response =
48        prost::Message::decode(resp_frame.body.as_ref()).map_err(FutuError::Proto)?;
49
50    if resp.ret_type != 0 {
51        return Err(FutuError::ServerError {
52            ret_type: resp.ret_type,
53            msg: resp.ret_msg.unwrap_or_default(),
54        });
55    }
56
57    let s2c = resp
58        .s2c
59        .ok_or(FutuError::Codec("missing s2c in GetBroker".into()))?;
60
61    Ok(BrokerData {
62        security: Security::from_proto(&s2c.security),
63        ask_list: s2c
64            .broker_ask_list
65            .iter()
66            .map(BrokerEntry::from_proto)
67            .collect(),
68        bid_list: s2c
69            .broker_bid_list
70            .iter()
71            .map(BrokerEntry::from_proto)
72            .collect(),
73    })
74}