Skip to main content

alpaca_data/news/
request.rs

1use crate::common::enums::Sort;
2use crate::common::query::QueryWriter;
3use crate::transport::pagination::PaginatedRequest;
4
5#[derive(Clone, Debug, Default)]
6pub struct ListRequest {
7    pub start: Option<String>,
8    pub end: Option<String>,
9    pub sort: Option<Sort>,
10    pub symbols: Option<Vec<String>>,
11    pub limit: Option<u32>,
12    pub include_content: Option<bool>,
13    pub exclude_contentless: Option<bool>,
14    pub page_token: Option<String>,
15}
16
17impl ListRequest {
18    pub(crate) fn to_query(self) -> Vec<(String, String)> {
19        let mut query = QueryWriter::default();
20        query.push_opt("start", self.start);
21        query.push_opt("end", self.end);
22        query.push_opt("sort", self.sort);
23        if let Some(symbols) = self.symbols {
24            query.push_csv("symbols", symbols);
25        }
26        query.push_opt("limit", self.limit);
27        query.push_opt("include_content", self.include_content);
28        query.push_opt("exclude_contentless", self.exclude_contentless);
29        query.push_opt("page_token", self.page_token);
30        query.finish()
31    }
32}
33
34impl PaginatedRequest for ListRequest {
35    fn with_page_token(&self, page_token: Option<String>) -> Self {
36        let mut next = self.clone();
37        next.page_token = page_token;
38        next
39    }
40}
41
42#[cfg(test)]
43mod tests {
44    use super::ListRequest;
45    use crate::common::enums::Sort;
46
47    #[test]
48    fn list_request_serializes_official_query_words() {
49        let query = ListRequest {
50            start: Some("2026-04-01T00:00:00Z".into()),
51            end: Some("2026-04-04T00:00:00Z".into()),
52            sort: Some(Sort::Desc),
53            symbols: Some(vec!["AAPL".into(), "BTCUSD".into()]),
54            limit: Some(2),
55            include_content: Some(false),
56            exclude_contentless: Some(true),
57            page_token: Some("page-2".into()),
58        }
59        .to_query();
60
61        assert_eq!(
62            query,
63            vec![
64                ("start".to_string(), "2026-04-01T00:00:00Z".to_string()),
65                ("end".to_string(), "2026-04-04T00:00:00Z".to_string()),
66                ("sort".to_string(), "desc".to_string()),
67                ("symbols".to_string(), "AAPL,BTCUSD".to_string()),
68                ("limit".to_string(), "2".to_string()),
69                ("include_content".to_string(), "false".to_string()),
70                ("exclude_contentless".to_string(), "true".to_string()),
71                ("page_token".to_string(), "page-2".to_string()),
72            ]
73        );
74    }
75}