faucet-stream 0.1.4

A declarative, config-driven REST API client with pluggable authentication, pagination, and JSONPath extraction
Documentation
//! Offset/limit pagination.

use crate::error::FaucetError;
use jsonpath_rust::JsonPath;
use serde_json::Value;
use std::collections::HashMap;

pub fn apply_params(
    params: &mut HashMap<String, String>,
    offset_param: &str,
    limit_param: &str,
    offset: usize,
    limit: usize,
) {
    params.insert(offset_param.to_string(), offset.to_string());
    params.insert(limit_param.to_string(), limit.to_string());
}

pub fn advance(
    body: &Value,
    offset: &mut usize,
    record_count: usize,
    limit: usize,
    total_path: Option<&str>,
) -> Result<bool, FaucetError> {
    *offset += record_count;
    if let Some(tp) = total_path {
        let results = body
            .query(tp)
            .map_err(|e| FaucetError::JsonPath(format!("{e}")))?;
        if let Some(total_val) = results.first() {
            let total = match total_val.as_u64() {
                Some(n) => n as usize,
                None => {
                    tracing::warn!(
                        "total_path '{tp}' resolved to non-numeric value {total_val}; \
                         falling back to record-count heuristic"
                    );
                    return Ok(record_count >= limit);
                }
            };
            return Ok(*offset < total);
        }
    }
    Ok(record_count >= limit)
}