|
| 1 | +use std::io::Write; |
| 2 | +use std::path::Path; |
| 3 | + |
| 4 | +use anyhow::Result; |
| 5 | +use serde::{Deserialize, Serialize}; |
| 6 | + |
| 7 | +use crate::auth::{resolve_api_base_url, resolve_api_key}; |
| 8 | +use crate::utils::{api_get, open_db}; |
| 9 | + |
| 10 | +#[derive(Deserialize)] |
| 11 | +struct EndpointsResponse { |
| 12 | + data: Vec<EndpointItem>, |
| 13 | +} |
| 14 | + |
| 15 | +#[derive(Deserialize, Serialize)] |
| 16 | +struct EndpointItem { |
| 17 | + id: i64, |
| 18 | + method: String, |
| 19 | + path: String, |
| 20 | +} |
| 21 | + |
| 22 | +fn fetch_endpoints( |
| 23 | + api_key: &str, |
| 24 | + api_base_url: &str, |
| 25 | + app_id: i64, |
| 26 | + method: Option<&str>, |
| 27 | + path: Option<&str>, |
| 28 | +) -> Result<Vec<EndpointItem>> { |
| 29 | + let url = format!("{api_base_url}/v1/apps/{app_id}/endpoints"); |
| 30 | + let mut query: Vec<(&str, &str)> = Vec::new(); |
| 31 | + if let Some(m) = method { |
| 32 | + query.push(("method", m)); |
| 33 | + } |
| 34 | + if let Some(p) = path { |
| 35 | + query.push(("path", p)); |
| 36 | + } |
| 37 | + let mut response = api_get(&url, api_key, &query)?; |
| 38 | + let endpoints: EndpointsResponse = response.body_mut().read_json()?; |
| 39 | + Ok(endpoints.data) |
| 40 | +} |
| 41 | + |
| 42 | +pub(crate) fn ensure_endpoints_table(conn: &duckdb::Connection) -> Result<()> { |
| 43 | + conn.execute_batch( |
| 44 | + "CREATE TABLE IF NOT EXISTS endpoints ( |
| 45 | + app_id INTEGER NOT NULL, |
| 46 | + endpoint_id INTEGER NOT NULL, |
| 47 | + method TEXT NOT NULL, |
| 48 | + path TEXT NOT NULL, |
| 49 | + UNIQUE (app_id, endpoint_id) |
| 50 | + )", |
| 51 | + )?; |
| 52 | + Ok(()) |
| 53 | +} |
| 54 | + |
| 55 | +fn write_endpoints_to_db( |
| 56 | + conn: &duckdb::Connection, |
| 57 | + app_id: i64, |
| 58 | + endpoints: &[EndpointItem], |
| 59 | +) -> Result<()> { |
| 60 | + let mut stmt = conn.prepare( |
| 61 | + "INSERT OR REPLACE INTO endpoints ( |
| 62 | + app_id, endpoint_id, method, path |
| 63 | + ) VALUES (?, ?, ?, ?)", |
| 64 | + )?; |
| 65 | + for endpoint in endpoints { |
| 66 | + stmt.execute(duckdb::params![ |
| 67 | + app_id, |
| 68 | + endpoint.id, |
| 69 | + endpoint.method, |
| 70 | + endpoint.path, |
| 71 | + ])?; |
| 72 | + } |
| 73 | + Ok(()) |
| 74 | +} |
| 75 | + |
| 76 | +pub fn run( |
| 77 | + app_id: i64, |
| 78 | + method: Option<&str>, |
| 79 | + path: Option<&str>, |
| 80 | + db: Option<&Path>, |
| 81 | + api_key: Option<&str>, |
| 82 | + api_base_url: Option<&str>, |
| 83 | + mut writer: impl Write, |
| 84 | +) -> Result<()> { |
| 85 | + let api_key = resolve_api_key(api_key)?; |
| 86 | + let api_base_url = resolve_api_base_url(api_base_url); |
| 87 | + let endpoints = fetch_endpoints(&api_key, &api_base_url, app_id, method, path)?; |
| 88 | + |
| 89 | + if let Some(db_path) = db { |
| 90 | + let conn = open_db(db_path)?; |
| 91 | + ensure_endpoints_table(&conn)?; |
| 92 | + write_endpoints_to_db(&conn, app_id, &endpoints)?; |
| 93 | + eprintln!( |
| 94 | + "{} endpoints written to table 'endpoints' in {}.\nDone.", |
| 95 | + endpoints.len(), |
| 96 | + db_path.display(), |
| 97 | + ); |
| 98 | + } else { |
| 99 | + for endpoint in &endpoints { |
| 100 | + serde_json::to_writer(&mut writer, endpoint)?; |
| 101 | + writeln!(writer)?; |
| 102 | + } |
| 103 | + } |
| 104 | + |
| 105 | + Ok(()) |
| 106 | +} |
| 107 | + |
| 108 | +#[cfg(test)] |
| 109 | +mod tests { |
| 110 | + use super::*; |
| 111 | + use crate::utils::open_db; |
| 112 | + use crate::utils::test_utils::{parse_ndjson, temp_db}; |
| 113 | + |
| 114 | + fn sample_endpoints_json() -> &'static str { |
| 115 | + r#"{ |
| 116 | + "data": [ |
| 117 | + { |
| 118 | + "id": 1, |
| 119 | + "method": "POST", |
| 120 | + "path": "/v1/users" |
| 121 | + }, |
| 122 | + { |
| 123 | + "id": 2, |
| 124 | + "method": "GET", |
| 125 | + "path": "/v1/users/{user_id}" |
| 126 | + } |
| 127 | + ] |
| 128 | + }"# |
| 129 | + } |
| 130 | + |
| 131 | + fn mock_endpoints_endpoint(server: &mut mockito::Server, app_id: i64) -> mockito::Mock { |
| 132 | + let path = format!("/v1/apps/{app_id}/endpoints"); |
| 133 | + server |
| 134 | + .mock("GET", path.as_str()) |
| 135 | + .with_status(200) |
| 136 | + .with_header("content-type", "application/json") |
| 137 | + .with_body(sample_endpoints_json()) |
| 138 | + .create() |
| 139 | + } |
| 140 | + |
| 141 | + #[test] |
| 142 | + fn test_run_ndjson() { |
| 143 | + let mut server = mockito::Server::new(); |
| 144 | + let mock = mock_endpoints_endpoint(&mut server, 1); |
| 145 | + |
| 146 | + let mut buf = Vec::new(); |
| 147 | + run( |
| 148 | + 1, |
| 149 | + None, |
| 150 | + None, |
| 151 | + None, |
| 152 | + Some("test-key"), |
| 153 | + Some(&server.url()), |
| 154 | + &mut buf, |
| 155 | + ) |
| 156 | + .unwrap(); |
| 157 | + mock.assert(); |
| 158 | + |
| 159 | + let rows = parse_ndjson(&buf); |
| 160 | + assert_eq!(rows.len(), 2); |
| 161 | + assert_eq!(rows[0]["method"], "POST"); |
| 162 | + assert_eq!(rows[0]["path"], "/v1/users"); |
| 163 | + assert_eq!(rows[1]["method"], "GET"); |
| 164 | + assert_eq!(rows[1]["path"], "/v1/users/{user_id}"); |
| 165 | + } |
| 166 | + |
| 167 | + #[test] |
| 168 | + fn test_run_with_db() { |
| 169 | + let mut server = mockito::Server::new(); |
| 170 | + let mock = mock_endpoints_endpoint(&mut server, 1); |
| 171 | + let (_dir, db_path) = temp_db(); |
| 172 | + |
| 173 | + run( |
| 174 | + 1, |
| 175 | + None, |
| 176 | + None, |
| 177 | + Some(&db_path), |
| 178 | + Some("test-key"), |
| 179 | + Some(&server.url()), |
| 180 | + Vec::new(), |
| 181 | + ) |
| 182 | + .unwrap(); |
| 183 | + mock.assert(); |
| 184 | + |
| 185 | + let conn = open_db(&db_path).unwrap(); |
| 186 | + |
| 187 | + let count: i64 = conn |
| 188 | + .query_row( |
| 189 | + "SELECT count(*) FROM endpoints WHERE app_id = 1", |
| 190 | + [], |
| 191 | + |row| row.get(0), |
| 192 | + ) |
| 193 | + .unwrap(); |
| 194 | + assert_eq!(count, 2); |
| 195 | + |
| 196 | + let method: String = conn |
| 197 | + .query_row( |
| 198 | + "SELECT method FROM endpoints WHERE endpoint_id = 1", |
| 199 | + [], |
| 200 | + |row| row.get(0), |
| 201 | + ) |
| 202 | + .unwrap(); |
| 203 | + assert_eq!(method, "POST"); |
| 204 | + } |
| 205 | +} |
0 commit comments