Skip to main content

open_library_api_rs/api/
authors.rs

1// v0.0.1
2use crate::client::OpenLibraryClient;
3use crate::error::Result;
4use crate::models::author::{Author, AuthorWorks};
5use crate::validation::{validate_author_id, validate_limit};
6
7impl OpenLibraryClient {
8    /// Fetch an Author by their Open Library ID (e.g. `"OL23919A"`).
9    ///
10    /// An Author OLID has the form `OL<digits>A`.
11    pub async fn get_author(&self, id: &str) -> Result<Author> {
12        validate_author_id(id)?;
13        let url = self.base_url.join(&format!("authors/{id}.json"))?;
14        self.get_json(url).await
15    }
16
17    /// Fetch the works attributed to an Author, paginated.
18    ///
19    /// `limit` must be 1–1000; `offset` is zero-based. The response includes
20    /// `entries` (the current page) and `links` for pagination.
21    pub async fn get_author_works(
22        &self,
23        id: &str,
24        limit: u32,
25        offset: u32,
26    ) -> Result<AuthorWorks> {
27        validate_author_id(id)?;
28        validate_limit(limit)?;
29        let mut url = self.base_url.join(&format!("authors/{id}/works.json"))?;
30        url.query_pairs_mut()
31            .append_pair("limit", &limit.to_string())
32            .append_pair("offset", &offset.to_string());
33        self.get_json(url).await
34    }
35}