newsdata_io_api/apis/news_sources.rs
1use std::collections::HashMap;
2
3use crate::{
4 newsdata_io::{NewsdataIO, Requests},
5 ApiResult, Json,
6};
7
8/// Trait for the News Sources API.
9pub trait NewsSources {
10 /// Get news sources.
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_sources(&self, params: &GetNewsSourcesParams) -> ApiResult<Json>;
20}
21
22impl NewsSources for NewsdataIO {
23 fn get_news_sources(&self, params: &GetNewsSourcesParams) -> ApiResult<Json> {
24 let mut query_params = HashMap::new();
25
26 // Add id parameter to query params
27 if let Some(id) = ¶ms.id {
28 query_params.insert("id".to_string(), id.join(","));
29 }
30
31 // Add country parameter to query params
32 if let Some(country) = ¶ms.country {
33 query_params.insert("country".to_string(), country.join(","));
34 }
35
36 // Add category parameter to query params
37 if let Some(category) = ¶ms.category {
38 query_params.insert("category".to_string(), category.join(","));
39 }
40
41 // Add exclude_category parameter to query params
42 if let Some(exclude_category) = ¶ms.exclude_category {
43 query_params.insert("excludecategory".to_string(), exclude_category.join(","));
44 }
45
46 // Add language parameter to query params
47 if let Some(language) = ¶ms.language {
48 query_params.insert("language".to_string(), language.clone());
49 }
50
51 // Add priority_domain parameter to query params
52 if let Some(priority_domain) = ¶ms.priority_domain {
53 query_params.insert("prioritydomain".to_string(), priority_domain.clone());
54 }
55
56 // Make the GET request to the sources endpoint
57 self.get("sources", Some(query_params))
58 }
59}
60
61/// Parameters for the `get_news_sources` method.
62#[derive(Debug, Default)]
63pub struct GetNewsSourcesParams {
64 /// Unique identifier of the news source.\
65 /// Max no. of id could be added: 50
66 pub id: Option<Vec<String>>,
67 /// Country code for the news sources.\
68 /// Max no. of country could be added: 5.\
69 /// Examples: "hk", "us", "wo"
70 pub country: Option<Vec<String>>,
71 /// Category for the news sources.\
72 /// Max no. of category could be added: 5.\
73 /// Possible values: "business", "crime", "domestic", "education", "entertainment", "environment", "food", "health", "lifestyle", "other", "politics", "science", "sports", "technology", "top", "tourism", "world"
74 pub category: Option<Vec<String>>,
75 /// Exclude category for the news sources.\
76 /// Max no. of category could be added: 5.\
77 /// Exclusive with category.\
78 /// Possible values: "business", "crime", "domestic", "education", "entertainment", "environment", "food", "health", "lifestyle", "other", "politics", "science", "sports", "technology", "top", "tourism", "world"
79 pub exclude_category: Option<Vec<String>>,
80 /// Language code for the news sources.\
81 /// Max no. of language could be added: 5.
82 pub language: Option<String>,
83 /// Priority domain for the news articles.\
84 /// Top: Fetches news articles from the top 10% of the news domains\
85 /// Medium: Fetches news articles from the top 30% of the news domains. It means it already includes all the news articles of "top" priority.\
86 /// 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.
87 pub priority_domain: Option<String>,
88}
89
90impl GetNewsSourcesParams {
91 /// Creates a new `GetNewsSourcesParams` with default values.
92 ///
93 /// This method sets the default values for all parameters, which are:
94 ///
95 /// * `id`: `None`
96 /// * `country`: `None`
97 /// * `category`: `None`
98 /// * `exclude_category`: `None`
99 /// * `language`: `None`
100 /// * `priority_domain`: `None`
101 ///
102 /// This allows you to easily create a `GetNewsSourcesParams` object without having to specify all the parameters manually.
103 pub fn default() -> Self {
104 GetNewsSourcesParams {
105 id: None,
106 country: None,
107 category: None,
108 exclude_category: None,
109 language: None,
110 priority_domain: None,
111 }
112 }
113}