open-library-api-rs 0.1.0

Async Rust client for the Open Library API
Documentation
// v0.0.1
use std::collections::HashMap;

use crate::client::OpenLibraryClient;
use crate::error::{Error, Result};
use crate::models::query::QueryResponse;
use crate::validation::validate_limit;

impl OpenLibraryClient {
    /// Execute a generic query against the `/query.json` endpoint.
    ///
    /// `type_` must be one of `/type/edition`, `/type/work`, `/type/author`, etc.
    /// `fields` is a map of field name → value to filter on.
    /// `limit` controls the number of results (max 1000).
    pub async fn query(
        &self,
        type_: &str,
        fields: &HashMap<String, String>,
        limit: u32,
        offset: u32,
    ) -> Result<QueryResponse> {
        if type_.is_empty() {
            return Err(Error::InvalidInput("query type must not be empty".into()));
        }
        validate_limit(limit)?;

        let mut url = self.base_url.join("query.json")?;
        {
            let mut qp = url.query_pairs_mut();
            qp.append_pair("type", type_);
            qp.append_pair("limit", &limit.to_string());
            qp.append_pair("offset", &offset.to_string());
            for (k, v) in fields {
                qp.append_pair(k, v);
            }
        }
        self.get_json(url).await
    }
}