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
use crate::{
    types::{
        ApiResponse,
        Article,
    },
    FmlResult,
};

/// An FML Client
#[derive(Debug, Clone)]
pub struct Client {
    client: reqwest::Client,
    api_key: String,
}

impl Client {
    /// Make a new Client from an api key
    pub fn new(api_key: String) -> Self {
        Client {
            client: reqwest::Client::new(),
            api_key,
        }
    }

    /// Get a list of random articles.
    pub async fn list_random(&self, n: usize) -> FmlResult<Vec<Article>> {
        let url = format!("https://www.fmylife.com/api/v2/article/list?page[number]=1&page[bypage]={}&orderby[RAND()]=ASC", n);
        let text = self
            .client
            .get(&url)
            .header("X-VDM-Api-Key", &self.api_key)
            .send()
            .await?
            .error_for_status()?
            .text()
            .await?;
        let api_response: ApiResponse<_> = serde_json::from_str(&text)?;
        api_response.into()
    }
}