fml/
client.rs

1use crate::{
2    types::{
3        ApiResponse,
4        Article,
5    },
6    FmlResult,
7};
8
9/// An FML Client
10#[derive(Debug, Clone)]
11pub struct Client {
12    client: reqwest::Client,
13    api_key: String,
14}
15
16impl Client {
17    /// Make a new Client from an api key
18    pub fn new(api_key: String) -> Self {
19        Client {
20            client: reqwest::Client::new(),
21            api_key,
22        }
23    }
24
25    /// Get a list of random articles.
26    pub async fn list_random(&self, n: usize) -> FmlResult<Vec<Article>> {
27        let url = format!("https://www.fmylife.com/api/v2/article/list?page[number]=1&page[bypage]={}&orderby[RAND()]=ASC", n);
28        let text = self
29            .client
30            .get(&url)
31            .header("X-VDM-Api-Key", &self.api_key)
32            .send()
33            .await?
34            .error_for_status()?
35            .text()
36            .await?;
37        let api_response: ApiResponse<_> = serde_json::from_str(&text)?;
38        api_response.into()
39    }
40}