Skip to content

Commit

Permalink
Pass vec of symbols to last_quote and last_trade requests
Browse files Browse the repository at this point in the history
  • Loading branch information
sbeam committed Nov 15, 2022
1 parent 20d428f commit fb1b65e
Show file tree
Hide file tree
Showing 3 changed files with 48 additions and 27 deletions.
8 changes: 6 additions & 2 deletions examples/latest-quote-and-trade.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ async fn main() {
let api_info = ApiInfo::from_env().unwrap();
let client = Client::new(api_info);

let quote_req = last_quote::LastQuoteReq::new("AAPL,MSFT");
let quote_req = last_quote::LastQuoteReq::new(vec!["AAPL".to_string(), "MSFT".to_string()]);
let quotes = client.issue::<last_quote::Get>(&quote_req).await.unwrap();
quotes.iter().for_each(|q| {
println!(
Expand All @@ -26,7 +26,11 @@ async fn main() {
)
});

let trade_req = last_trade::LastTradeRequest::new("SPY,QQQ,MSFT");
let trade_req = last_trade::LastTradeRequest::new(vec![
"SPY".to_string(),
"QQQ".to_string(),
"IWM".to_string(),
]);
let trades = client.issue::<last_trade::Get>(&trade_req).await.unwrap();
trades.iter().for_each(|trade| {
println!(
Expand Down
40 changes: 27 additions & 13 deletions src/data/v2/last_quote.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,15 +27,17 @@ pub struct LastQuoteReq {

impl LastQuoteReq {
/// Create a new LastQuoteReq with the given symbols.
pub fn new<S>(symbols: S) -> Self
where
S: Into<String>,
{
pub fn new(symbols: Vec<String>) -> Self {
Self {
symbols: symbols.into(),
symbols: symbols.join(",").into(),
feed: None,
}
}
/// Set the data feed to use.
pub fn with_feed(mut self, feed: Feed) -> Self {
self.feed = Some(feed);
self
}
}

/// A quote bar as returned by the /v2/stocks/quotes/latest endpoint.
Expand Down Expand Up @@ -196,7 +198,7 @@ mod tests {
let api_info = ApiInfo::from_env().unwrap();
let client = Client::new(api_info);

let req = LastQuoteReq::new("SPY");
let req = LastQuoteReq::new(vec!["SPY".to_string()]);
let quotes = client.issue::<Get>(&req).await.unwrap();
// Just as a rough sanity check, we require that the reported time
// is some time after two weeks before today. That should safely
Expand All @@ -210,7 +212,11 @@ mod tests {
let api_info = ApiInfo::from_env().unwrap();
let client = Client::new(api_info);

let req = LastQuoteReq::new("SPY,QQQ,MSFT");
let req = LastQuoteReq::new(vec![
"SPY".to_string(),
"QQQ".to_string(),
"MSFT".to_string(),
]);
let quotes = client.issue::<Get>(&req).await.unwrap();
assert_eq!(quotes.len(), 3);
assert!(quotes[0].time >= Utc::now() - Duration::weeks(2));
Expand All @@ -222,10 +228,7 @@ mod tests {
let api_info = ApiInfo::from_env().unwrap();
let client = Client::new(api_info);

let req = LastQuoteReq {
symbols: "SPY".to_string(),
feed: Some(Feed::SIP),
};
let req = LastQuoteReq::new(vec!["SPY".to_string()]).with_feed(Feed::SIP);

let result = client.issue::<Get>(&req).await;
// Unfortunately we can't really know whether the user has the
Expand All @@ -237,13 +240,24 @@ mod tests {
}
}

/// Bad symbol results in an error response from the server.
/// Non-existent symbol is skipped in the result.
#[test(tokio::test)]
async fn nonexistent_symbol() {
let api_info = ApiInfo::from_env().unwrap();
let client = Client::new(api_info);

let req = LastQuoteReq::new("SPY,ABC123");
let req = LastQuoteReq::new(vec!["SPY".to_string(), "NOSUCHSYMBOL".to_string()]);
let quotes = client.issue::<Get>(&req).await.unwrap();
assert_eq!(quotes.len(), 1);
}

/// Symbol with characters outside A-Z results in an error response from the server.
#[test(tokio::test)]
async fn bad_symbol() {
let api_info = ApiInfo::from_env().unwrap();
let client = Client::new(api_info);

let req = LastQuoteReq::new(vec!["ABC123".to_string()]);
let err = client.issue::<Get>(&req).await.unwrap_err();
match err {
RequestError::Endpoint(GetError::InvalidInput(_)) => (),
Expand Down
27 changes: 15 additions & 12 deletions src/data/v2/last_trade.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,15 +27,17 @@ pub struct LastTradeRequest {

impl LastTradeRequest {
/// Create a new LastTradeRequest.
pub fn new<S>(symbols: S) -> Self
where
S: Into<String>,
{
pub fn new(symbols: Vec<String>) -> Self {
Self {
symbols: symbols.into(),
symbols: symbols.join(",").into(),
feed: None,
}
}
/// Set the data feed to use.
pub fn with_feed(mut self, feed: Feed) -> Self {
self.feed = Some(feed);
self
}
}

/// A trade data point as returned by the /v2/stocks/{symbol}/trades/latest endpoint.
Expand Down Expand Up @@ -182,7 +184,7 @@ mod tests {
let api_info = ApiInfo::from_env().unwrap();
let client = Client::new(api_info);

let req = LastTradeRequest::new("SPY");
let req = LastTradeRequest::new(vec!["SPY".to_string()]);
let trades = client.issue::<Get>(&req).await.unwrap();
// Just as a rough sanity check, we require that the reported time
// is some time after two weeks before today. That should safely
Expand All @@ -198,7 +200,11 @@ mod tests {
let api_info = ApiInfo::from_env().unwrap();
let client = Client::new(api_info);

let req = LastTradeRequest::new("SPY,QQQ,MSFT");
let req = LastTradeRequest::new(vec![
"SPY".to_string(),
"QQQ".to_string(),
"MSFT".to_string(),
]);
let trades = client.issue::<Get>(&req).await.unwrap();
assert_eq!(trades.len(), 3);
assert!(trades[0].time >= Utc::now() - Duration::weeks(2));
Expand All @@ -211,10 +217,7 @@ mod tests {
let api_info = ApiInfo::from_env().unwrap();
let client = Client::new(api_info);

let req = LastTradeRequest {
symbols: "SPY".to_string(),
feed: Some(Feed::SIP),
};
let req = LastTradeRequest::new(vec!["SPY".to_string()]).with_feed(Feed::SIP);

let result = client.issue::<Get>(&req).await;
// Unfortunately we can't really know whether the user has the
Expand All @@ -232,7 +235,7 @@ mod tests {
let api_info = ApiInfo::from_env().unwrap();
let client = Client::new(api_info);

let req = LastTradeRequest::new("BZZZZZZT,AAPL");
let req = LastTradeRequest::new(vec!["BZZZZZZT".to_string(), "AAPL".to_string()]);
let trades = client.issue::<Get>(&req).await.unwrap();
assert_eq!(trades.len(), 1);
}
Expand Down

0 comments on commit fb1b65e

Please sign in to comment.