newsdata_io_api/apis/
news_archive.rs

1use std::collections::HashMap;
2
3use crate::{
4    newsdata_io::{NewsdataIO, Requests},
5    ApiResult, Json,
6};
7
8/// Trait for the News Archive API.
9pub trait NewsArchive {
10    /// Get news articles from the archive.
11    ///
12    /// # Arguments
13    ///
14    /// * `params`: The parameters for the request.
15    ///
16    /// # Returns
17    ///
18    /// An `ApiResult` containing the JSON response from the API.
19    fn get_news_archive(&self, params: &GetNewsArchiveParams) -> ApiResult<Json>;
20}
21
22impl NewsArchive for NewsdataIO {
23    fn get_news_archive(&self, params: &GetNewsArchiveParams) -> ApiResult<Json> {
24        let mut query_params = HashMap::new();
25
26        // Add id parameter to query params
27        if let Some(id) = &params.id {
28            query_params.insert("id".to_string(), id.join(","));
29        }
30
31        // Add from_date parameter to query params
32        if let Some(from_date) = &params.from_date {
33            query_params.insert("from_date".to_string(), from_date.clone());
34        }
35
36        // Add to_date parameter to query params
37        if let Some(to_date) = &params.to_date {
38            query_params.insert("to_date".to_string(), to_date.clone());
39        }
40
41        // Add q parameter to query params
42        if let Some(q) = &params.q {
43            query_params.insert("q".to_string(), q.clone());
44        }
45
46        // Add q_in_title parameter to query params
47        if let Some(q_in_title) = &params.q_in_title {
48            query_params.insert("qInTitle".to_string(), q_in_title.clone());
49        }
50
51        // Add q_in_meta parameter to query params
52        if let Some(q_in_meta) = &params.q_in_meta {
53            query_params.insert("qInMeta".to_string(), q_in_meta.clone());
54        }
55
56        // Add country parameter to query params
57        if let Some(country) = &params.country {
58            query_params.insert("country".to_string(), country.join(","));
59        }
60
61        // Add category parameter to query params
62        if let Some(category) = &params.category {
63            query_params.insert("category".to_string(), category.join(","));
64        }
65
66        // Add exclude_category parameter to query params
67        if let Some(exclude_category) = &params.exclude_category {
68            query_params.insert("excludecategory".to_string(), exclude_category.join(","));
69        }
70
71        // Add language parameter to query params
72        if let Some(language) = &params.language {
73            query_params.insert("language".to_string(), language.clone());
74        }
75
76        // Add domain parameter to query params
77        if let Some(domain) = &params.domain {
78            query_params.insert("domain".to_string(), domain.clone());
79        }
80
81        // Add exclude_domain parameter to query params
82        if let Some(exclude_domain) = &params.exclude_domain {
83            query_params.insert("excludedomain".to_string(), exclude_domain.clone());
84        }
85
86        // Add domain_url parameter to query params
87        if let Some(domain_url) = &params.domain_url {
88            query_params.insert("domainurl".to_string(), domain_url.clone());
89        }
90
91        // Add exclude_field parameter to query params
92        if let Some(exclude_field) = &params.exclude_field {
93            query_params.insert("excludefield".to_string(), exclude_field.clone());
94        }
95
96        // Add priority_domain parameter to query params
97        if let Some(priority_domain) = &params.priority_domain {
98            query_params.insert("prioritydomain".to_string(), priority_domain.clone());
99        }
100
101        // Add timezone parameter to query params
102        if let Some(timezone) = &params.timezone {
103            query_params.insert("timezone".to_string(), timezone.clone());
104        }
105
106        // Add full_content parameter to query params
107        if let Some(full_content) = &params.full_content {
108            println!("full_content: {}", full_content.value().to_string());
109            query_params.insert("full_content".to_string(), full_content.value().to_string());
110        }
111
112        // Add image parameter to query params
113        if let Some(image) = &params.image {
114            query_params.insert("image".to_string(), image.value().to_string());
115        }
116
117        // Add video parameter to query params
118        if let Some(video) = &params.video {
119            query_params.insert("video".to_string(), video.value().to_string());
120        }
121
122        // Add size parameter to query params
123        if let Some(size) = &params.size {
124            query_params.insert("size".to_string(), size.to_string());
125        }
126
127        // Add page parameter to query params
128        if let Some(page) = &params.page {
129            query_params.insert("page".to_string(), page.to_string());
130        }
131
132        // Make the GET request to the archive endpoint
133        self.get("archive", Some(query_params))
134    }
135}
136
137/// Enum for representing boolean values as strings.
138#[derive(Debug)]
139pub enum Flag {
140    /// False value.
141    False,
142    /// True value.
143    True,
144}
145
146impl Flag {
147    /// Returns the string representation of the flag.
148    fn value(&self) -> &str {
149        match self {
150            Flag::True => "1",
151            Flag::False => "0",
152        }
153    }
154}
155
156/// Parameters for the `get_news_archive` method.
157#[derive(Debug, Default)]
158pub struct GetNewsArchiveParams {
159    /// Unique identifier of the news article.\
160    /// Max no. of id could be added: 50
161    pub id: Option<Vec<String>>,
162    /// Start date for the news articles.\
163    /// If not specified, the api will fetch the data from the past 1 years if you have professional subscription.\
164    /// Format: YYYY-MM-DD
165    pub from_date: Option<String>,
166    /// End date for the news articles.\
167    /// If not specified, it will be today\
168    /// Format: YYYY-MM-DD
169    pub to_date: Option<String>,
170    /// Keywords to search for in the news articles.\
171    /// Max characters: 512.\
172    /// Exclusive with q_in_title and q_in_meta
173    pub q: Option<String>,
174    /// Keywords to search for in the title of the news articles.\
175    /// Max characters: 512.\
176    /// Exclusive with q and q_in_meta
177    pub q_in_title: Option<String>,
178    /// Keywords to search for in the meta description of the news articles.\
179    /// Max characters: 512.\
180    /// Exclusive with q and q_in_title
181    pub q_in_meta: Option<String>,
182    /// Country code for the news articles.\
183    /// Max no. of country could be added: 5.\
184    /// Examples: "hk", "us", "wo"
185    pub country: Option<Vec<String>>,
186    /// Category for the news articles.\
187    /// Max no. of category could be added: 5.\
188    /// Exclusive with exclude_category.\
189    /// Possible values: "business", "crime", "domestic", "education", "entertainment", "environment", "food", "health", "lifestyle", "other", "politics", "science", "sports", "technology", "top", "tourism", "world"
190    pub category: Option<Vec<String>>,
191    /// Category to exclude from the results.\
192    /// Max no. of exclude_category could be added: 5.\
193    /// Exclusive with category.\
194    /// Possible values: "business", "crime", "domestic", "education", "entertainment", "environment", "food", "health", "lifestyle", "other", "politics", "science", "sports", "technology", "top", "tourism", "world"
195    pub exclude_category: Option<Vec<String>>,
196    /// Language code for the news articles.\
197    /// Max no. of language could be added: 5.
198    pub language: Option<String>,
199    /// Domain for the news articles.\
200    /// Max no. of domain could be added: 5.\
201    /// Possible values in [here](https://newsdata.io/documentation/#latest-news)
202    pub domain: Option<String>,
203    /// Domain to exclude from the results.\
204    /// Max no. of domain could be added: 5.\
205    /// Possible values in [here](https://newsdata.io/documentation/#latest-news)
206    pub exclude_domain: Option<String>,
207    /// Domain URL for the news articles.\
208    /// Max no. of domain could be added: 5.
209    pub domain_url: Option<String>,
210    /// Field to exclude from the results.\
211    /// "article_id" is not excludable in response
212    pub exclude_field: Option<String>,
213    /// Priority domain for the news articles.\
214    /// Top: Fetches news articles from the top 10% of the news domains\
215    /// Medium: Fetches news articles from the top 30% of the news domains. It means it already includes all the news articles of "top" priority.\
216    /// Low: Fetches news articles from the top 50% of the news domains. It means it already includes all the news articles of "top" and "medium" priorities.
217    pub priority_domain: Option<String>,
218    /// Timezone for the news articles.
219    pub timezone: Option<String>,
220    /// Whether to include full content in the results.
221    pub full_content: Option<Flag>,
222    /// Whether to include images in the results.
223    pub image: Option<Flag>,
224    /// Whether to include videos in the results.
225    pub video: Option<Flag>,
226    /// Number of results to return.\
227    /// Could only be 1 to 50
228    pub size: Option<i32>,
229    /// page parameter from last result\
230    /// [Detail](https://newsdata.io/documentation/#pagination)
231    pub page: Option<String>,
232}
233
234impl GetNewsArchiveParams {
235    /// Creates a new `GetNewsArchiveParams` with default values.
236    ///
237    /// This method sets the default values for all parameters, which are:
238    ///
239    /// * `id`: `None`
240    /// * `q`: `None`
241    /// * `q_in_title`: `None`
242    /// * `q_in_meta`: `None`
243    /// * `country`: `None`
244    /// * `category`: `None`
245    /// * `exclude_category`: `None`
246    /// * `language`: `None`
247    /// * `domain`: `None`
248    /// * `exclude_domain`: `None`
249    /// * `domain_url`: `None`
250    /// * `exclude_field`: `None`
251    /// * `priority_domain`: `None`
252    /// * `timezone`: `None`
253    /// * `full_content`: `None`
254    /// * `image`: `None`
255    /// * `video`: `None`
256    /// * `size`: `None`
257    /// * `page`: `None`
258    /// * `from_date`: `None`
259    /// * `to_date`: `None`
260    ///
261    /// This allows you to easily create a `GetNewsArchiveParams` object without having to specify all the parameters manually.
262    pub fn default() -> Self {
263        GetNewsArchiveParams {
264            id: None,
265            q: None,
266            q_in_title: None,
267            q_in_meta: None,
268            country: None,
269            category: None,
270            exclude_category: None,
271            language: None,
272            domain: None,
273            exclude_domain: None,
274            domain_url: None,
275            exclude_field: None,
276            priority_domain: None,
277            timezone: None,
278            full_content: None,
279            image: None,
280            video: None,
281            size: None,
282            page: None,
283            from_date: None,
284            to_date: None,
285        }
286    }
287}