Skip to content
This repository was archived by the owner on Aug 7, 2023. It is now read-only.

Commit bb5ed93

Browse files
authored
enhance-skip-the-query-params-if-empty (#25)
* enhance-skip-the-query-params-if-empty * fix code coverage * use vec with capacity 0 instead of vec macro
1 parent f441aeb commit bb5ed93

File tree

8 files changed

+71
-45
lines changed

8 files changed

+71
-45
lines changed

src/http/intraday/chart.rs

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -42,15 +42,19 @@ impl Request for ChartRequest<'_> {
4242
type Response = ChartResponse;
4343

4444
fn queries(&self) -> Vec<Query> {
45-
vec![
46-
Query {
45+
let mut ret = Vec::with_capacity(2);
46+
if !self.symbol_id.is_empty() {
47+
ret.push(Query {
4748
param: "symbolId".to_string(),
4849
value: self.symbol_id.to_string(),
49-
},
50-
Query {
50+
})
51+
}
52+
if self.odd_lot {
53+
ret.push(Query {
5154
param: "oddLot".to_string(),
5255
value: self.odd_lot.to_string(),
53-
},
54-
]
56+
})
57+
}
58+
ret
5559
}
5660
}

src/http/intraday/dealts.rs

Lines changed: 18 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -56,23 +56,31 @@ impl Request for DealtsRequest<'_> {
5656
type Response = DealtsResponse;
5757

5858
fn queries(&self) -> Vec<Query> {
59-
vec![
60-
Query {
59+
let mut ret = Vec::with_capacity(4);
60+
if !self.symbol_id.is_empty() {
61+
ret.push(Query {
6162
param: "symbolId".to_string(),
6263
value: self.symbol_id.to_string(),
63-
},
64-
Query {
64+
})
65+
}
66+
if self.odd_lot {
67+
ret.push(Query {
6568
param: "oddLot".to_string(),
6669
value: self.odd_lot.to_string(),
67-
},
68-
Query {
70+
})
71+
}
72+
if self.limit != 0 {
73+
ret.push(Query {
6974
param: "limit".to_string(),
7075
value: self.limit.to_string(),
71-
},
72-
Query {
76+
})
77+
}
78+
if self.offset != 0 {
79+
ret.push(Query {
7380
param: "offset".to_string(),
7481
value: self.offset.to_string(),
75-
},
76-
]
82+
})
83+
}
84+
ret
7785
}
7886
}

src/http/intraday/meta.rs

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -42,15 +42,19 @@ impl Request for MetaRequest<'_> {
4242
type Response = MetaResponse;
4343

4444
fn queries(&self) -> Vec<Query> {
45-
vec![
46-
Query {
45+
let mut ret = Vec::with_capacity(2);
46+
if !self.symbol_id.is_empty() {
47+
ret.push(Query {
4748
param: "symbolId".to_string(),
4849
value: self.symbol_id.to_string(),
49-
},
50-
Query {
50+
})
51+
}
52+
if self.odd_lot {
53+
ret.push(Query {
5154
param: "oddLot".to_string(),
5255
value: self.odd_lot.to_string(),
53-
},
54-
]
56+
})
57+
}
58+
ret
5559
}
5660
}

src/http/intraday/quote.rs

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -42,15 +42,19 @@ impl Request for QuoteRequest<'_> {
4242
type Response = QuoteResponse;
4343

4444
fn queries(&self) -> Vec<Query> {
45-
vec![
46-
Query {
45+
let mut ret = Vec::with_capacity(2);
46+
if !self.symbol_id.is_empty() {
47+
ret.push(Query {
4748
param: "symbolId".to_string(),
4849
value: self.symbol_id.to_string(),
49-
},
50-
Query {
50+
})
51+
}
52+
if self.odd_lot {
53+
ret.push(Query {
5154
param: "oddLot".to_string(),
5255
value: self.odd_lot.to_string(),
53-
},
54-
]
56+
})
57+
}
58+
ret
5559
}
5660
}

src/http/intraday/volumes.rs

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -42,15 +42,19 @@ impl Request for VolumesRequest<'_> {
4242
type Response = VolumesResponse;
4343

4444
fn queries(&self) -> Vec<Query> {
45-
vec![
46-
Query {
45+
let mut ret = Vec::with_capacity(2);
46+
if !self.symbol_id.is_empty() {
47+
ret.push(Query {
4748
param: "symbolId".to_string(),
4849
value: self.symbol_id.to_string(),
49-
},
50-
Query {
50+
})
51+
}
52+
if self.odd_lot {
53+
ret.push(Query {
5154
param: "oddLot".to_string(),
5255
value: self.odd_lot.to_string(),
53-
},
54-
]
56+
})
57+
}
58+
ret
5559
}
5660
}

src/http/marketdata/candles.rs

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -115,11 +115,14 @@ impl Request for CandlesRequest<'_> {
115115
type Response = CandlesResponse;
116116

117117
fn queries(&self) -> Vec<Query> {
118-
let mut ret = vec![Query {
119-
param: "symbolId".to_string(),
120-
value: self.symbol_id.to_string(),
121-
}];
118+
let mut ret = Vec::with_capacity(4);
122119

120+
if !self.symbol_id.is_empty() {
121+
ret.push(Query {
122+
param: "symbolId".to_string(),
123+
value: self.symbol_id.to_string(),
124+
})
125+
}
123126
if !self.from.is_empty() {
124127
ret.push(Query {
125128
param: "from".to_string(),
@@ -132,20 +135,19 @@ impl Request for CandlesRequest<'_> {
132135
value: self.to.to_string(),
133136
})
134137
}
138+
135139
let mut fields = vec![];
136140
for field in CandleField::iterator() {
137141
if self.fields & field.value() != 0 {
138142
fields.push(field.to_string());
139143
}
140144
}
141-
142145
if !fields.is_empty() {
143146
ret.push(Query {
144147
param: "fields".to_string(),
145148
value: fields.join(","),
146149
})
147150
}
148-
149151
ret
150152
}
151153
}

src/schema/candles.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ impl Default for CandlesResponse {
5151
typ: "".to_string(),
5252
exchange: "".to_string(),
5353
market: "".to_string(),
54-
candles: vec![],
54+
candles: Vec::with_capacity(0),
5555
}
5656
}
5757
}

tests/intraday_test.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -185,12 +185,12 @@ async fn test_intraday_async_meta_401_failed() {
185185
#[test]
186186
fn test_intraday_dealts_pass() {
187187
let it = RestfulBuilder::new().build().unwrap();
188-
let dealts = it.call(DealtsRequest::new().limit(9).offset(0)).unwrap();
188+
let dealts = it.call(DealtsRequest::new().limit(9).offset(1)).unwrap();
189189
assert_eq!(dealts.data.info.symbol_id, "2884");
190190
assert_eq!(dealts.data.info.typ, "EQUITY");
191191

192192
let dealts = it
193-
.call(DealtsRequest::default().odd_lot(true).limit(9).offset(0))
193+
.call(DealtsRequest::default().odd_lot(true).limit(9).offset(1))
194194
.unwrap();
195195
assert_eq!(dealts.data.info.symbol_id, "2884");
196196
assert_eq!(dealts.data.info.typ, "ODDLOT");
@@ -200,14 +200,14 @@ fn test_intraday_dealts_pass() {
200200
async fn test_intraday_async_dealts_pass() {
201201
let it = RestfulBuilder::new().build_async().unwrap();
202202
let dealts = it
203-
.call(DealtsRequest::new().limit(9).offset(0))
203+
.call(DealtsRequest::new().limit(9).offset(1))
204204
.await
205205
.unwrap();
206206
assert_eq!(dealts.data.info.symbol_id, "2884");
207207
assert_eq!(dealts.data.info.typ, "EQUITY");
208208

209209
let dealts = it
210-
.call(DealtsRequest::default().odd_lot(true).limit(9).offset(0))
210+
.call(DealtsRequest::default().odd_lot(true).limit(9).offset(1))
211211
.await
212212
.unwrap();
213213
assert_eq!(dealts.data.info.symbol_id, "2884");

0 commit comments

Comments
 (0)