Skip to main content

systemprompt_generator/
api.rs

1use anyhow::{anyhow, Result};
2use serde_json::Value;
3
4pub async fn fetch_content_from_api(api_url: &str, source_id: &str) -> Result<Vec<Value>> {
5    let url = format!("{api_url}/api/v1/content/{source_id}");
6
7    let response = reqwest::Client::new()
8        .get(&url)
9        .send()
10        .await
11        .map_err(|e| anyhow!("Failed to connect to {}: {}", url, e))?;
12
13    if !response.status().is_success() {
14        return Err(anyhow!(
15            "Failed to fetch {}: {} {}",
16            source_id,
17            response.status(),
18            response
19                .text()
20                .await
21                .unwrap_or_else(|_| "unknown error".to_string())
22        ));
23    }
24
25    let items: Vec<Value> = response
26        .json()
27        .await
28        .map_err(|e| anyhow!("Failed to parse response from {}: {}", source_id, e))?;
29
30    Ok(items)
31}