qualia_core_db/domains/geospatial/adapters/
ckan_adapter.rs1use crate::domains::geospatial::adapters::AdapterHttpRequest;
2use crate::net::disclosure::NetworkDisclosureRegistry;
3
4pub struct CkanAdapter {
6 pub id: &'static str,
7 pub api_endpoint: String,
8}
9
10impl CkanAdapter {
11 pub fn new(id: &'static str, api_endpoint: &str) -> Self {
13 Self {
14 id,
15 api_endpoint: api_endpoint.to_string(),
16 }
17 }
18}
19
20impl super::DataAdapter for CkanAdapter {
21 fn adapter_id(&self) -> &'static str {
22 self.id
23 }
24
25 fn build_fetch_request(
26 &self,
27 bbox: (f64, f64, f64, f64),
28 _time_range: (u64, u64),
29 registry: &NetworkDisclosureRegistry,
30 ) -> Result<AdapterHttpRequest, String> {
31 let search_endpoint = format!("{}/action/package_search", self.api_endpoint);
32
33 if !registry.check_egress_consent(self.adapter_id(), &search_endpoint) {
34 return Err(format!(
35 "Consent denied or unregistered for CKAN endpoint {} by adapter {}",
36 search_endpoint,
37 self.adapter_id()
38 ));
39 }
40
41 let query = format!(
42 "{}?ext_bbox={},{},{},{}&rows=50",
43 search_endpoint, bbox.0, bbox.1, bbox.2, bbox.3
44 );
45
46 Ok(AdapterHttpRequest::get(query, "CKAN"))
47 }
48
49 fn needs_fetch_body(&self) -> bool {
50 true
51 }
52
53 fn handle_fetch_body(&self, body: &str) -> Result<(), String> {
54 let json: serde_json::Value = serde_json::from_str(body).map_err(|e| e.to_string())?;
55
56 let results = json
57 .get("result")
58 .and_then(|r| r.get("results"))
59 .and_then(|r| r.as_array())
60 .ok_or_else(|| "Failed to parse CKAN results array".to_string())?;
61
62 for dataset in results {
63 let title = dataset
64 .get("title")
65 .and_then(|v| v.as_str())
66 .unwrap_or("Untitled");
67 let license = dataset
68 .get("license_title")
69 .and_then(|v| v.as_str())
70 .unwrap_or("Unknown");
71 let created = dataset
72 .get("metadata_created")
73 .and_then(|v| v.as_str())
74 .unwrap_or("");
75
76 if let Some(resources) = dataset.get("resources").and_then(|v| v.as_array()) {
77 for res in resources {
78 let format = res.get("format").and_then(|v| v.as_str()).unwrap_or("");
79 let url = res.get("url").and_then(|v| v.as_str()).unwrap_or("");
80 println!("Discovered CKAN Dataset '{}' | Format: {} | License: {} | Created: {} | URL: {}",
82 title, format, license, created, url);
83 }
84 }
85 }
86
87 Ok(())
88 }
89
90 fn primary_endpoint(&self) -> &str {
91 &self.api_endpoint
92 }
93
94 fn estimate_tile_count(&self, _bbox: (f64, f64, f64, f64)) -> u32 {
95 1
97 }
98}
99
100#[cfg(test)]
101mod tests {
102 use super::*;
103 use crate::domains::geospatial::adapters::DataAdapter;
104
105 #[test]
106 fn test_ckan_adapter_egress() {
107 let adapter = CkanAdapter::new("ckan_test", "https://data.gov.au/data/api/3");
108 let mut registry = NetworkDisclosureRegistry::new();
109
110 let res1 = adapter.fetch_region((144.9, -37.9, 145.0, -37.8), (0, 0), ®istry);
111 assert!(res1.is_err());
112
113 registry.register_egress(
114 adapter.adapter_id(),
115 "https://data.gov.au/data/api/3/action/package_search",
116 "Query federated CKAN repository for datasets",
117 "User executes spatial search",
118 );
119
120 let res2 = adapter.fetch_region((144.9, -37.9, 145.0, -37.8), (0, 0), ®istry);
121 if let Err(e) = res2 {
123 assert!(
124 !e.contains("Consent denied"),
125 "Failed on consent when it should have been granted: {}",
126 e
127 );
128 }
129 }
130}