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::edition::Edition;
use crate::validation::{validate_edition_id, validate_isbn};

impl OpenLibraryClient {
    /// Fetch an Edition by its Open Library ID (e.g. `"OL7353617M"`).
    ///
    /// An Edition OLID has the form `OL<digits>M`.
    pub async fn get_edition(&self, id: &str) -> Result<Edition> {
        validate_edition_id(id)?;
        let url = self.base_url.join(&format!("books/{id}.json"))?;
        self.get_json(url).await
    }

    /// Fetch an Edition by ISBN-10 or ISBN-13.
    ///
    /// Hyphens and spaces in the ISBN are stripped automatically. Both check
    /// digits are validated before the request is made.
    pub async fn get_edition_by_isbn(&self, isbn: &str) -> Result<Edition> {
        validate_isbn(isbn)?;
        // Strip hyphens/spaces that some callers pass in.
        let clean: String = isbn.chars().filter(|c| c.is_ascii_alphanumeric()).collect();
        let url = self.base_url.join(&format!("isbn/{clean}.json"))?;
        self.get_json(url).await
    }
}