1use futu_core::error::{FutuError, Result};
2use futu_core::proto_id;
3use futu_net::client::FutuClient;
4
5use crate::types::{OrderBookData, OrderBookEntry, Security};
6
7pub async fn get_order_book(
14 client: &FutuClient,
15 security: &Security,
16 num: i32,
17) -> Result<OrderBookData> {
18 get_order_book_with_type(client, security, num, None).await
19}
20
21pub async fn get_order_book_with_type(
26 client: &FutuClient,
27 security: &Security,
28 num: i32,
29 order_book_type: Option<i32>,
30) -> Result<OrderBookData> {
31 let req = futu_proto::qot_get_order_book::Request {
32 c2s: futu_proto::qot_get_order_book::C2s {
33 security: security.to_proto(),
34 num,
35 order_book_type,
36 header: None,
37 },
38 };
39
40 let body = prost::Message::encode_to_vec(&req);
41 let resp_frame = client.request(proto_id::QOT_GET_ORDER_BOOK, body).await?;
42
43 let resp: futu_proto::qot_get_order_book::Response =
44 prost::Message::decode(resp_frame.body.as_ref()).map_err(FutuError::Proto)?;
45
46 if resp.ret_type != 0 {
47 return Err(crate::server_err(resp.ret_type, resp.ret_msg));
48 }
49
50 let s2c = resp
51 .s2c
52 .ok_or(FutuError::Codec("missing s2c in GetOrderBook".into()))?;
53
54 Ok(OrderBookData {
55 security: Security::from_proto(&s2c.security),
56 ask_list: s2c
57 .order_book_ask_list
58 .iter()
59 .map(OrderBookEntry::from_proto)
60 .collect(),
61 bid_list: s2c
62 .order_book_bid_list
63 .iter()
64 .map(OrderBookEntry::from_proto)
65 .collect(),
66 })
67}