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::{Error, Result};
use crate::models::common::VolumeIdType;
use crate::models::partner::VolumesResponse;

impl OpenLibraryClient {
    /// Look up a single volume by identifier type and value.
    ///
    /// Example: `read_volume(VolumeIdType::Isbn, "0451450523")`.
    pub async fn read_volume(
        &self,
        id_type: VolumeIdType,
        id_value: &str,
    ) -> Result<VolumesResponse> {
        if id_value.is_empty() {
            return Err(Error::InvalidInput("id_value must not be empty".into()));
        }
        let path = format!("api/volumes/brief/{}/{}.json", id_type.as_str(), id_value);
        let url = self.base_url.join(&path)?;
        self.get_json(url).await
    }

    /// Batch-look up multiple volumes.
    ///
    /// `requests` is a list of `"type/value"` strings, e.g. `["isbn/0451450523", "oclc/45883427"]`.
    pub async fn read_volumes_batch(
        &self,
        requests: &[String],
    ) -> Result<VolumesResponse> {
        if requests.is_empty() {
            return Err(Error::InvalidInput("requests list must not be empty".into()));
        }
        let joined = requests.join("|");
        let path = format!("api/volumes/brief/json/{}", joined);
        let url = self.base_url.join(&path)?;
        self.get_json(url).await
    }
}