Skip to main content

open_library_api_rs/api/
editions.rs

1// v0.0.1
2use crate::client::OpenLibraryClient;
3use crate::error::Result;
4use crate::models::edition::Edition;
5use crate::validation::{validate_edition_id, validate_isbn};
6
7impl OpenLibraryClient {
8    /// Fetch an Edition by its Open Library ID (e.g. `"OL7353617M"`).
9    ///
10    /// An Edition OLID has the form `OL<digits>M`.
11    pub async fn get_edition(&self, id: &str) -> Result<Edition> {
12        validate_edition_id(id)?;
13        let url = self.base_url.join(&format!("books/{id}.json"))?;
14        self.get_json(url).await
15    }
16
17    /// Fetch an Edition by ISBN-10 or ISBN-13.
18    ///
19    /// Hyphens and spaces in the ISBN are stripped automatically. Both check
20    /// digits are validated before the request is made.
21    pub async fn get_edition_by_isbn(&self, isbn: &str) -> Result<Edition> {
22        validate_isbn(isbn)?;
23        // Strip hyphens/spaces that some callers pass in.
24        let clean: String = isbn.chars().filter(|c| c.is_ascii_alphanumeric()).collect();
25        let url = self.base_url.join(&format!("isbn/{clean}.json"))?;
26        self.get_json(url).await
27    }
28}