thenewsapi 0.1.2

A client library for interacting with The News API, a free service providing access to global news from thousands of sources.
Documentation
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
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
//! # News API Client
//!
//! This crate provides a client library for interacting with the News API.
//!
//! The News API provides global news from thousands of sources with exceptional response times. This client allows you to access headlines, top stories, all news articles, similar news articles, and sources.
//!
//! ## Usage
//!
//! Add this to your `Cargo.toml`:
//! ```toml
//! [dependencies]
//! news_api_client = "0.1.0"
//! reqwest = { version = "0.11", features = ["json"] }
//! serde = { version = "1.0", features = ["derive"] }
//! serde_json = "1.0"
//! tokio = { version = "1.0", features = ["full"] }
//! anyhow = "1.0"
//! ```
//!
//! ## Examples
//!
//! ```rust,no_run
//! use news_api_client::{Client, HeadlinesParams};
//!
//! #[tokio::main]
//! async fn main() {
//!     let api_token = "your_api_token";
//!     let client = Client::new(api_token);
//!
//!     let params = HeadlinesParams::default();
//!     let headlines = client.get_headlines(params).await.unwrap();
//!     println!("{:?}", headlines);
//! }
//! ```

use anyhow::{anyhow, Result};
use serde::{Deserialize, Serialize};
use serde_json::json;
use std::collections::HashMap;

/// Represents an article in the News API.
#[derive(Deserialize, Debug)]
pub struct Article {
    /// The unique identifier for an article.
    pub uuid: String,
    /// The article title.
    pub title: String,
    /// The article meta description.
    pub description: String,
    /// The article meta keywords.
    pub keywords: Option<String>,
    /// The first 60 characters of the article body.
    pub snippet: String,
    /// The URL to the article.
    pub url: String,
    /// The URL to the article image.
    pub image_url: Option<String>,
    /// The language of the source.
    pub language: String,
    /// The datetime the article was published.
    pub published_at: String,
    /// The domain of the source.
    pub source: String,
    /// Array of strings which the source is categorized as.
    pub categories: Vec<String>,
    /// Locale of the source.
    pub locale: Option<String>,
    /// An array of similar articles to the base article.
    pub similar: Option<Vec<SimilarArticle>>,
}

/// Represents a similar article in the News API.
#[derive(Deserialize, Debug)]
pub struct SimilarArticle {
    /// The unique identifier for a similar article.
    pub uuid: String,
    /// The article title.
    pub title: String,
    /// The article meta description.
    pub description: String,
    /// The article meta keywords.
    pub keywords: Option<String>,
    /// The first 60 characters of the article body.
    pub snippet: String,
    /// The URL to the article.
    pub url: String,
    /// The URL to the article image.
    pub image_url: Option<String>,
    /// The language of the source.
    pub language: String,
    /// The datetime the article was published.
    pub published_at: String,
    /// The domain of the source.
    pub source: String,
    /// Array of strings which the source is categorized as.
    pub categories: Vec<String>,
    /// Locale of the source.
    pub locale: Option<String>,
}

/// Represents metadata in the News API responses.
#[derive(Deserialize, Debug)]
pub struct Meta {
    /// The number of articles found for the request.
    pub found: usize,
    /// The number of articles returned on the page.
    pub returned: usize,
    /// The limit based on the limit parameter.
    pub limit: usize,
    /// The page number based on the page parameter.
    pub page: usize,
}

/// Represents the response for headlines in the News API.
#[derive(Deserialize, Debug)]
pub struct HeadlinesResponse {
    /// The data containing the headlines.
    pub data: HashMap<String, Vec<Article>>,
}

/// Represents the response for top stories in the News API.
#[derive(Deserialize, Debug)]
pub struct TopStoriesResponse {
    /// Metadata about the response.
    pub meta: Meta,
    /// The data containing the top stories.
    pub data: Vec<Article>,
}

/// Represents the response for all news articles in the News API.
#[derive(Deserialize, Debug)]
pub struct AllNewsResponse {
    /// Metadata about the response.
    pub meta: Meta,
    /// The data containing all news articles.
    pub data: Vec<Article>,
}

/// Represents the response for similar news articles in the News API.
#[derive(Deserialize, Debug)]
pub struct SimilarNewsResponse {
    /// Metadata about the response.
    pub meta: Meta,
    /// The data containing similar news articles.
    pub data: Vec<Article>,
}

/// Represents the response for a specific article by UUID in the News API.
#[derive(Deserialize, Debug)]
pub struct ArticleByUuidResponse {
    /// The unique identifier for an article.
    pub uuid: String,
    /// The article title.
    pub title: String,
    /// The article meta description.
    pub description: String,
    /// The article meta keywords.
    pub keywords: Option<String>,
    /// The first 60 characters of the article body.
    pub snippet: String,
    /// The URL to the article.
    pub url: String,
    /// The URL to the article image.
    pub image_url: Option<String>,
    /// The language of the source.
    pub language: String,
    /// The datetime the article was published.
    pub published_at: String,
    /// The domain of the source.
    pub source: String,
    /// Array of strings which the source is categorized as.
    pub categories: Vec<String>,
}

/// Represents a source in the News API.
#[derive(Deserialize, Debug)]
pub struct Source {
    /// The unique ID of the source feed.
    pub source_id: String,
    /// The domain of the source.
    pub domain: String,
    /// The source language.
    pub language: String,
    /// The source locale.
    pub locale: Option<String>,
    /// Array of strings which the source is categorized as.
    pub categories: Vec<String>,
}

/// Represents the response for sources in the News API.
#[derive(Deserialize, Debug)]
pub struct SourcesResponse {
    /// Metadata about the response.
    pub meta: Meta,
    /// The data containing the sources.
    pub data: Vec<Source>,
}

/// Parameters for the `get_headlines` API call.
#[derive(Serialize, Default)]
pub struct HeadlinesParams<'a> {
    pub locale: Option<&'a str>,
    pub domains: Option<&'a str>,
    pub exclude_domains: Option<&'a str>,
    pub source_ids: Option<&'a str>,
    pub exclude_source_ids: Option<&'a str>,
    pub language: Option<&'a str>,
    pub published_on: Option<&'a str>,
    pub headlines_per_category: Option<usize>,
    pub include_similar: Option<bool>,
}

/// Parameters for the `get_top_stories` API call.
#[derive(Serialize, Default)]
pub struct TopStoriesParams<'a> {
    pub search: Option<&'a str>,
    pub search_fields: Option<&'a str>,
    pub locale: Option<&'a str>,
    pub categories: Option<&'a str>,
    pub exclude_categories: Option<&'a str>,
    pub domains: Option<&'a str>,
    pub exclude_domains: Option<&'a str>,
    pub source_ids: Option<&'a str>,
    pub exclude_source_ids: Option<&'a str>,
    pub language: Option<&'a str>,
    pub published_before: Option<&'a str>,
    pub published_after: Option<&'a str>,
    pub published_on: Option<&'a str>,
    pub sort: Option<&'a str>,
    pub limit: Option<usize>,
    pub page: Option<usize>,
}

/// Parameters for the `get_all_news` API call.
#[derive(Serialize, Default)]
pub struct AllNewsParams<'a> {
    pub search: Option<&'a str>,
    pub search_fields: Option<&'a str>,
    pub locale: Option<&'a str>,
    pub categories: Option<&'a str>,
    pub exclude_categories: Option<&'a str>,
    pub domains: Option<&'a str>,
    pub exclude_domains: Option<&'a str>,
    pub source_ids: Option<&'a str>,
    pub exclude_source_ids: Option<&'a str>,
    pub language: Option<&'a str>,
    pub published_before: Option<&'a str>,
    pub published_after: Option<&'a str>,
    pub published_on: Option<&'a str>,
    pub sort: Option<&'a str>,
    pub limit: Option<usize>,
    pub page: Option<usize>,
}

/// Parameters for the `get_similar_news` API call.
#[derive(Serialize, Default)]
pub struct SimilarNewsParams<'a> {
    pub categories: Option<&'a str>,
    pub exclude_categories: Option<&'a str>,
    pub domains: Option<&'a str>,
    pub exclude_domains: Option<&'a str>,
    pub source_ids: Option<&'a str>,
    pub exclude_source_ids: Option<&'a str>,
    pub language: Option<&'a str>,
    pub published_before: Option<&'a str>,
    pub published_after: Option<&'a str>,
    pub published_on: Option<&'a str>,
    pub limit: Option<usize>,
    pub page: Option<usize>,
}

/// Parameters for the `get_sources` API call.
#[derive(Serialize, Default)]
pub struct SourcesParams<'a> {
    pub categories: Option<&'a str>,
    pub exclude_categories: Option<&'a str>,
    pub language: Option<&'a str>,
    pub page: Option<usize>,
}

/// A client for the News API.
pub struct Client {
    client: reqwest::Client,
    api_token: String,
}

impl Client {
    /// Creates a new News API client.
    ///
    /// # Arguments
    ///
    /// * `api_token` - Your API token which can be found on your account dashboard.
    ///
    /// # Example
    ///
    /// ```rust
    /// let client = Client::new("your_api_token");
    /// ```
    pub fn new(api_token: &str) -> Self {
        Client {
            client: reqwest::Client::new(),
            api_token: api_token.to_string(),
        }
    }

    /// Gets the latest headlines.
    ///
    /// # Arguments
    ///
    /// * `params` - Parameters to filter the headlines.
    ///
    /// # Example
    ///
    /// ```rust,no_run
    /// let params = HeadlinesParams::default();
    /// let headlines = client.get_headlines(params).await.unwrap();
    /// println!("{:?}", headlines);
    /// ```
    pub async fn get_headlines(&self, params: HeadlinesParams<'_>) -> Result<HeadlinesResponse> {
        self.get("https://api.thenewsapi.com/v1/news/headlines", params)
            .await
    }

    /// Gets the top stories.
    ///
    /// # Arguments
    ///
    /// * `params` - Parameters to filter the top stories.
    ///
    /// # Example
    ///
    /// ```rust,no_run
    /// let params = TopStoriesParams::default();
    /// let top_stories = client.get_top_stories(params).await.unwrap();
    /// println!("{:?}", top_stories);
    /// ```
    pub async fn get_top_stories(
        &self,
        params: TopStoriesParams<'_>,
    ) -> Result<TopStoriesResponse> {
        self.get("https://api.thenewsapi.com/v1/news/top", params)
            .await
    }

    /// Gets all news articles.
    ///
    /// # Arguments
    ///
    /// * `params` - Parameters to filter all news articles.
    ///
    /// # Example
    ///
    /// ```rust,no_run
    /// let params = AllNewsParams::default();
    /// let all_news = client.get_all_news(params).await.unwrap();
    /// println!("{:?}", all_news);
    /// ```
    pub async fn get_all_news(&self, params: AllNewsParams<'_>) -> Result<AllNewsResponse> {
        self.get("https://api.thenewsapi.com/v1/news/all", params)
            .await
    }

    /// Gets similar news articles based on a specific article UUID.
    ///
    /// # Arguments
    ///
    /// * `uuid` - The UUID of the article to find similar articles for.
    /// * `params` - Parameters to filter the similar news articles.
    ///
    /// # Example
    ///
    /// ```rust,no_run
    /// let params = SimilarNewsParams::default();
    /// ```
    pub async fn get_similar_news(
        &self,
        uuid: &str,
        params: SimilarNewsParams<'_>,
    ) -> Result<SimilarNewsResponse> {
        self.get(
            &format!("https://api.thenewsapi.com/v1/news/similar/{}", uuid),
            params,
        )
        .await
    }

    /// Gets a specific article by UUID.
    ///
    /// # Arguments
    ///
    /// * `uuid` - The UUID of the article to retrieve.
    ///
    /// # Example
    ///
    /// ```rust,no_run
    /// let article = client.get_article_by_uuid("article_uuid").await.unwrap();
    /// println!("{:?}", article);
    /// ```
    pub async fn get_article_by_uuid(&self, uuid: &str) -> Result<ArticleByUuidResponse> {
        self.get(
            &format!("https://api.thenewsapi.com/v1/news/uuid/{}", uuid),
            (),
        )
        .await
    }

    /// Gets the sources.
    ///
    /// # Arguments
    ///
    /// * `params` - Parameters to filter the sources.
    ///
    /// # Example
    ///
    /// ```rust,no_run
    /// let params = SourcesParams::default();
    /// let sources = client.get_sources(params).await.unwrap();
    /// println!("{:?}", sources);
    /// ```
    pub async fn get_sources(&self, params: SourcesParams<'_>) -> Result<SourcesResponse> {
        self.get("https://api.thenewsapi.com/v1/sources", params)
            .await
    }

    async fn get<T: Serialize, U: for<'de> Deserialize<'de>>(
        &self,
        url: &str,
        params: T,
    ) -> Result<U> {
        let query = self.build_query(params);
        let response = self.client.get(url).query(&query).send().await?;

        if response.status().is_success() {
            let parsed_response = response.json::<U>().await?;
            Ok(parsed_response)
        } else {
            let status = response.status();
            let error_text = response
                .text()
                .await
                .unwrap_or_else(|_| String::from("Failed to read response text"));
            Err(anyhow!("HTTP {}: {}", status, error_text))
        }
    }

    fn build_query<T: Serialize>(&self, params: T) -> HashMap<String, String> {
        let mut query = serde_json::to_value(params)
            .unwrap_or(json!({}))
            .as_object()
            .unwrap()
            .clone();

        query.insert("api_token".to_string(), json!(self.api_token));

        query
            .into_iter()
            .filter(|(_, v)| !v.is_null())
            .map(|(k, v)| {
                let value_str =
                    if k == "published_on" || k == "published_before" || k == "published_after" {
                        format!("{}", v.as_str().unwrap_or("").to_string())
                    } else {
                        v.as_str().unwrap_or("").to_string()
                    };
                (k, value_str)
            })
            .collect()
    }
}