|
| 1 | +//! API client for communicating with the flowctl daemon. |
| 2 | +//! |
| 3 | +//! Uses `gloo-net` on WASM (client-side) for fetch requests. |
| 4 | +//! On the server side (SSR), these functions won't be called directly. |
| 5 | +
|
| 6 | +use serde::{Deserialize, Serialize}; |
| 7 | + |
| 8 | +/// API base URL — defaults to same origin. |
| 9 | +#[allow(dead_code)] |
| 10 | +fn api_base() -> String { |
| 11 | + // In the browser, use relative URLs (same origin). |
| 12 | + // Can be overridden via window.__FLOWCTL_API for dev. |
| 13 | + String::new() |
| 14 | +} |
| 15 | + |
| 16 | +/// Epic summary from the /api/v1/epics endpoint. |
| 17 | +#[derive(Debug, Clone, Serialize, Deserialize)] |
| 18 | +pub struct EpicSummary { |
| 19 | + pub id: String, |
| 20 | + pub title: String, |
| 21 | + pub status: String, |
| 22 | + pub tasks: usize, |
| 23 | + pub done: usize, |
| 24 | +} |
| 25 | + |
| 26 | +/// Epics list response. |
| 27 | +#[derive(Debug, Clone, Serialize, Deserialize)] |
| 28 | +pub struct EpicsResponse { |
| 29 | + pub epics: Vec<EpicSummary>, |
| 30 | + pub count: usize, |
| 31 | + pub success: bool, |
| 32 | +} |
| 33 | + |
| 34 | +/// Task from the /api/v1/tasks endpoint. |
| 35 | +#[derive(Debug, Clone, Serialize, Deserialize)] |
| 36 | +pub struct TaskItem { |
| 37 | + pub id: String, |
| 38 | + pub title: String, |
| 39 | + pub status: String, |
| 40 | + pub epic: Option<String>, |
| 41 | + #[serde(default)] |
| 42 | + pub depends_on: Vec<String>, |
| 43 | + #[serde(default)] |
| 44 | + pub domain: String, |
| 45 | +} |
| 46 | + |
| 47 | +/// Fetch all epics from the daemon API. |
| 48 | +#[cfg(feature = "hydrate")] |
| 49 | +pub async fn fetch_epics() -> Result<Vec<EpicSummary>, String> { |
| 50 | + let url = format!("{}/api/v1/epics", api_base()); |
| 51 | + let resp = gloo_net::http::Request::get(&url) |
| 52 | + .send() |
| 53 | + .await |
| 54 | + .map_err(|e| format!("fetch error: {e}"))?; |
| 55 | + |
| 56 | + if !resp.ok() { |
| 57 | + return Err(format!("HTTP {}", resp.status())); |
| 58 | + } |
| 59 | + |
| 60 | + let data: serde_json::Value = resp.json().await.map_err(|e| format!("json error: {e}"))?; |
| 61 | + |
| 62 | + // The epics endpoint returns a JSON array or {epics: [...]} |
| 63 | + if let Some(arr) = data.as_array() { |
| 64 | + serde_json::from_value(serde_json::Value::Array(arr.clone())) |
| 65 | + .map_err(|e| format!("parse error: {e}")) |
| 66 | + } else if let Some(epics) = data.get("epics") { |
| 67 | + serde_json::from_value(epics.clone()) |
| 68 | + .map_err(|e| format!("parse error: {e}")) |
| 69 | + } else { |
| 70 | + Err("unexpected response format".to_string()) |
| 71 | + } |
| 72 | +} |
| 73 | + |
| 74 | +/// Fetch tasks for an epic. |
| 75 | +#[cfg(feature = "hydrate")] |
| 76 | +pub async fn fetch_tasks(epic_id: &str) -> Result<Vec<TaskItem>, String> { |
| 77 | + let url = format!("{}/api/v1/tasks?epic_id={}", api_base(), epic_id); |
| 78 | + let resp = gloo_net::http::Request::get(&url) |
| 79 | + .send() |
| 80 | + .await |
| 81 | + .map_err(|e| format!("fetch error: {e}"))?; |
| 82 | + |
| 83 | + if !resp.ok() { |
| 84 | + return Err(format!("HTTP {}", resp.status())); |
| 85 | + } |
| 86 | + |
| 87 | + let data: serde_json::Value = resp.json().await.map_err(|e| format!("json error: {e}"))?; |
| 88 | + |
| 89 | + if let Some(arr) = data.as_array() { |
| 90 | + serde_json::from_value(serde_json::Value::Array(arr.clone())) |
| 91 | + .map_err(|e| format!("parse error: {e}")) |
| 92 | + } else { |
| 93 | + serde_json::from_value(data).map_err(|e| format!("parse error: {e}")) |
| 94 | + } |
| 95 | +} |
| 96 | + |
| 97 | +/// Start a task via POST. |
| 98 | +#[cfg(feature = "hydrate")] |
| 99 | +pub async fn start_task(task_id: &str) -> Result<(), String> { |
| 100 | + let url = format!("{}/api/v1/tasks/start", api_base()); |
| 101 | + let body = serde_json::json!({"task_id": task_id}); |
| 102 | + let resp = gloo_net::http::Request::post(&url) |
| 103 | + .json(&body) |
| 104 | + .map_err(|e| format!("json error: {e}"))? |
| 105 | + .send() |
| 106 | + .await |
| 107 | + .map_err(|e| format!("fetch error: {e}"))?; |
| 108 | + |
| 109 | + if resp.ok() { Ok(()) } else { Err(format!("HTTP {}", resp.status())) } |
| 110 | +} |
| 111 | + |
| 112 | +/// Complete a task via POST. |
| 113 | +#[cfg(feature = "hydrate")] |
| 114 | +pub async fn done_task(task_id: &str) -> Result<(), String> { |
| 115 | + let url = format!("{}/api/v1/tasks/done", api_base()); |
| 116 | + let body = serde_json::json!({"task_id": task_id}); |
| 117 | + let resp = gloo_net::http::Request::post(&url) |
| 118 | + .json(&body) |
| 119 | + .map_err(|e| format!("json error: {e}"))? |
| 120 | + .send() |
| 121 | + .await |
| 122 | + .map_err(|e| format!("fetch error: {e}"))?; |
| 123 | + |
| 124 | + if resp.ok() { Ok(()) } else { Err(format!("HTTP {}", resp.status())) } |
| 125 | +} |
| 126 | + |
| 127 | +// SSR stubs — these won't be called on the server but need to exist for compilation. |
| 128 | +#[cfg(not(feature = "hydrate"))] |
| 129 | +pub async fn fetch_epics() -> Result<Vec<EpicSummary>, String> { Ok(vec![]) } |
| 130 | +#[cfg(not(feature = "hydrate"))] |
| 131 | +pub async fn fetch_tasks(_epic_id: &str) -> Result<Vec<TaskItem>, String> { Ok(vec![]) } |
| 132 | +#[cfg(not(feature = "hydrate"))] |
| 133 | +pub async fn start_task(_task_id: &str) -> Result<(), String> { Ok(()) } |
| 134 | +#[cfg(not(feature = "hydrate"))] |
| 135 | +pub async fn done_task(_task_id: &str) -> Result<(), String> { Ok(()) } |
0 commit comments