From 031588f5d0476fcd94271aa064af8eef35869abf Mon Sep 17 00:00:00 2001 From: Markus Kohlhase Date: Fri, 8 Nov 2024 14:09:46 +0100 Subject: [PATCH] Add CSV export to frontend API --- ofdb-frontend-api/src/lib.rs | 7 +++++++ ofdb-frontend-api/src/public.rs | 7 +------ ofdb-frontend-api/src/user.rs | 21 +++++++++++++++++++-- ofdb-frontend-api/src/util.rs | 1 + 4 files changed, 28 insertions(+), 8 deletions(-) diff --git a/ofdb-frontend-api/src/lib.rs b/ofdb-frontend-api/src/lib.rs index e364a952..2e5c1cea 100644 --- a/ofdb-frontend-api/src/lib.rs +++ b/ofdb-frontend-api/src/lib.rs @@ -2,6 +2,8 @@ use gloo_net::http::Response; use serde::de::DeserializeOwned; use thiserror::Error; +use ofdb_boundary::MapBbox; + mod clearance; mod public; mod user; @@ -48,3 +50,8 @@ where Err(response.json::().await?.into()) } } + +pub(crate) fn bbox_string(bbox: &MapBbox) -> String { + let MapBbox { sw, ne } = bbox; + format!("{},{},{},{}", sw.lat, sw.lng, ne.lat, ne.lng) +} diff --git a/ofdb-frontend-api/src/public.rs b/ofdb-frontend-api/src/public.rs index 104d1ac5..ae1d5478 100644 --- a/ofdb-frontend-api/src/public.rs +++ b/ofdb-frontend-api/src/public.rs @@ -7,7 +7,7 @@ use ofdb_boundary::{ TagFrequency, UpdatePlace, }; -use crate::{into_json, Result, UserApi}; +use crate::{bbox_string, into_json, Result, UserApi}; /// Public OpenFairDB API #[derive(Clone, Copy)] @@ -212,8 +212,3 @@ impl PublicApi { into_json(response).await } } - -fn bbox_string(bbox: &MapBbox) -> String { - let MapBbox { sw, ne } = bbox; - format!("{},{},{},{}", sw.lat, sw.lng, ne.lat, ne.lng) -} diff --git a/ofdb-frontend-api/src/user.rs b/ofdb-frontend-api/src/user.rs index b7ef5589..8bb6aac5 100644 --- a/ofdb-frontend-api/src/user.rs +++ b/ofdb-frontend-api/src/user.rs @@ -1,10 +1,11 @@ use gloo_net::http::{Request, RequestBuilder}; +use percent_encoding::{utf8_percent_encode, NON_ALPHANUMERIC}; use serde::{de::DeserializeOwned, Serialize}; use web_sys::RequestCredentials; -use ofdb_boundary::{BboxSubscription, JwtToken, Review, User}; +use ofdb_boundary::{BboxSubscription, JwtToken, MapBbox, Review, User}; -use crate::{into_json, Result}; +use crate::{bbox_string, into_json, Result}; /// Authorized OpenFairDB API #[derive(Clone)] @@ -76,4 +77,20 @@ impl UserApi { let request = Request::post(&url); self.send_json(request, review).await } + + // TODO: add other options like `limit`, `tags`, etc. + pub async fn export_csv(&self, bbox: &MapBbox, search_term: Option<&str>) -> Result { + let bbox_string = bbox_string(bbox); + let search = search_term + .map(|term| { + let encoded_txt = utf8_percent_encode(term, NON_ALPHANUMERIC); + format!("&text={encoded_txt}") + }) + .unwrap_or_default(); + let url = format!("{}/export/entries.csv?bbox={bbox_string}{search}", self.url); + let request = self.add_auth_headers(Request::get(&url)); + let response = request.send().await?; + let csv_string = response.text().await?; + Ok(csv_string) + } } diff --git a/ofdb-frontend-api/src/util.rs b/ofdb-frontend-api/src/util.rs index d4959f3a..5967a29d 100644 --- a/ofdb-frontend-api/src/util.rs +++ b/ofdb-frontend-api/src/util.rs @@ -18,3 +18,4 @@ where Err(response.json::().await?.into()) } } +