open-library-api-rs 0.1.0

Async Rust client for the Open Library API
Documentation
// v0.0.1
use crate::client::OpenLibraryClient;
use crate::error::Result;
use crate::models::author::{Author, AuthorWorks};
use crate::validation::{validate_author_id, validate_limit};

impl OpenLibraryClient {
    /// Fetch an Author by their Open Library ID (e.g. `"OL23919A"`).
    ///
    /// An Author OLID has the form `OL<digits>A`.
    pub async fn get_author(&self, id: &str) -> Result<Author> {
        validate_author_id(id)?;
        let url = self.base_url.join(&format!("authors/{id}.json"))?;
        self.get_json(url).await
    }

    /// Fetch the works attributed to an Author, paginated.
    ///
    /// `limit` must be 1–1000; `offset` is zero-based. The response includes
    /// `entries` (the current page) and `links` for pagination.
    pub async fn get_author_works(
        &self,
        id: &str,
        limit: u32,
        offset: u32,
    ) -> Result<AuthorWorks> {
        validate_author_id(id)?;
        validate_limit(limit)?;
        let mut url = self.base_url.join(&format!("authors/{id}/works.json"))?;
        url.query_pairs_mut()
            .append_pair("limit", &limit.to_string())
            .append_pair("offset", &offset.to_string());
        self.get_json(url).await
    }
}