1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
use std::collections::HashMap;

use crate::{
    newsdata_io::{NewsdataIO, Requests},
    ApiResult, Json,
};

/// Trait for the News Sources API.
pub trait NewsSources {
    /// Get news sources.
    ///
    /// # Arguments
    ///
    /// * `params`: The parameters for the request.
    ///
    /// # Returns
    ///
    /// An `ApiResult` containing the JSON response from the API.
    fn get_news_sources(&self, params: &GetNewsSourcesParams) -> ApiResult<Json>;
}

impl NewsSources for NewsdataIO {
    fn get_news_sources(&self, params: &GetNewsSourcesParams) -> ApiResult<Json> {
        let mut query_params = HashMap::new();

        // Add id parameter to query params
        if let Some(id) = &params.id {
            query_params.insert("id".to_string(), id.join(","));
        }

        // Add country parameter to query params
        if let Some(country) = &params.country {
            query_params.insert("country".to_string(), country.join(","));
        }

        // Add category parameter to query params
        if let Some(category) = &params.category {
            query_params.insert("category".to_string(), category.join(","));
        }

        // Add exclude_category parameter to query params
        if let Some(exclude_category) = &params.exclude_category {
            query_params.insert("excludecategory".to_string(), exclude_category.join(","));
        }

        // Add language parameter to query params
        if let Some(language) = &params.language {
            query_params.insert("language".to_string(), language.clone());
        }

        // Add priority_domain parameter to query params
        if let Some(priority_domain) = &params.priority_domain {
            query_params.insert("prioritydomain".to_string(), priority_domain.clone());
        }

        // Make the GET request to the sources endpoint
        self.get("sources", Some(query_params))
    }
}

/// Parameters for the `get_news_sources` method.
#[derive(Debug, Default)]
pub struct GetNewsSourcesParams {
    /// Unique identifier of the news source.\
    /// Max no. of id could be added: 50
    pub id: Option<Vec<String>>,
    /// Country code for the news sources.\
    /// Max no. of country could be added: 5.\
    /// Examples: "hk", "us", "wo"
    pub country: Option<Vec<String>>,
    /// Category for the news sources.\
    /// Max no. of category could be added: 5.\
    /// Possible values: "business", "crime", "domestic", "education", "entertainment", "environment", "food", "health", "lifestyle", "other", "politics", "science", "sports", "technology", "top", "tourism", "world"
    pub category: Option<Vec<String>>,
    /// Exclude category for the news sources.\
    /// Max no. of category could be added: 5.\
    /// Exclusive with category.\
    /// Possible values: "business", "crime", "domestic", "education", "entertainment", "environment", "food", "health", "lifestyle", "other", "politics", "science", "sports", "technology", "top", "tourism", "world"
    pub exclude_category: Option<Vec<String>>,
    /// Language code for the news sources.\
    /// Max no. of language could be added: 5.
    pub language: Option<String>,
    /// Priority domain for the news articles.\
    /// Top: Fetches news articles from the top 10% of the news domains\
    /// Medium: Fetches news articles from the top 30% of the news domains. It means it already includes all the news articles of "top" priority.\
    /// 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.
    pub priority_domain: Option<String>,
}

impl GetNewsSourcesParams {
    /// Creates a new `GetNewsSourcesParams` with default values.
    ///
    /// This method sets the default values for all parameters, which are:
    ///
    /// * `id`: `None`
    /// * `country`: `None`
    /// * `category`: `None`
    /// * `exclude_category`: `None`
    /// * `language`: `None`
    /// * `priority_domain`: `None`
    ///
    /// This allows you to easily create a `GetNewsSourcesParams` object without having to specify all the parameters manually.
    pub fn default() -> Self {
        GetNewsSourcesParams {
            id: None,
            country: None,
            category: None,
            exclude_category: None,
            language: None,
            priority_domain: None,
        }
    }
}