Skip to main content

open_library_api_rs/api/
reading_log.rs

1// v0.0.1
2use crate::client::OpenLibraryClient;
3use crate::error::Result;
4use crate::models::common::ReadingShelf;
5use crate::models::reading_log::ReadingLog;
6use crate::validation::validate_username;
7
8impl OpenLibraryClient {
9    /// Fetch a user's public reading log for the given shelf.
10    async fn get_reading_log_shelf(
11        &self,
12        username: &str,
13        shelf: ReadingShelf,
14    ) -> Result<ReadingLog> {
15        validate_username(username)?;
16        let url = self
17            .base_url
18            .join(&format!("people/{username}/books/{}.json", shelf.as_str()))?;
19        self.get_json(url).await
20    }
21
22    /// Books on the user's "Want to Read" shelf.
23    pub async fn get_want_to_read(&self, username: &str) -> Result<ReadingLog> {
24        self.get_reading_log_shelf(username, ReadingShelf::WantToRead).await
25    }
26
27    /// Books the user is currently reading.
28    pub async fn get_currently_reading(&self, username: &str) -> Result<ReadingLog> {
29        self.get_reading_log_shelf(username, ReadingShelf::CurrentlyReading).await
30    }
31
32    /// Books the user has already read.
33    pub async fn get_already_read(&self, username: &str) -> Result<ReadingLog> {
34        self.get_reading_log_shelf(username, ReadingShelf::AlreadyRead).await
35    }
36}