second-brain-cli 0.13.5

Memory layer for AI coding agents: ingest, recall, and sync across machines
use std::time::Duration;

use reqwest::blocking::{Client, RequestBuilder};
use serde_json::Value;

use crate::api;

/// Leaves room inside the hook's 1.5 s budget for process start and output,
/// so a down or hung daemon never delays the agent past it.
const TIMEOUT: Duration = Duration::from_millis(1400);

pub(super) fn get(path: &str, query: &[(&str, &str)]) -> Option<Value> {
    call(path, |client, url| client.get(url).query(query))
}

pub(super) fn post(path: &str, body: &Value) -> Option<Value> {
    call(path, |client, url| client.post(url).json(body))
}

/// `None` when the daemon cannot say: no token, no answer within `TIMEOUT`,
/// an error status or a body that is not JSON.
fn call(path: &str, request: impl FnOnce(&Client, String) -> RequestBuilder) -> Option<Value> {
    let client = api::client(TIMEOUT).ok()?;
    let resp = request(&client, format!("{}{path}", api::base_url()))
        .send()
        .ok()?;
    if !resp.status().is_success() {
        return None;
    }
    resp.json().ok()
}