Skip to content

Commit

Permalink
Add CSV export to frontend API
Browse files Browse the repository at this point in the history
  • Loading branch information
flosse committed Nov 8, 2024
1 parent d43864f commit 031588f
Show file tree
Hide file tree
Showing 4 changed files with 28 additions and 8 deletions.
7 changes: 7 additions & 0 deletions ofdb-frontend-api/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -48,3 +50,8 @@ where
Err(response.json::<ofdb_boundary::Error>().await?.into())
}
}

pub(crate) fn bbox_string(bbox: &MapBbox) -> String {
let MapBbox { sw, ne } = bbox;
format!("{},{},{},{}", sw.lat, sw.lng, ne.lat, ne.lng)
}
7 changes: 1 addition & 6 deletions ofdb-frontend-api/src/public.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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)]
Expand Down Expand Up @@ -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)
}
21 changes: 19 additions & 2 deletions ofdb-frontend-api/src/user.rs
Original file line number Diff line number Diff line change
@@ -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)]
Expand Down Expand Up @@ -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<String> {
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)
}
}
1 change: 1 addition & 0 deletions ofdb-frontend-api/src/util.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,3 +18,4 @@ where
Err(response.json::<ofdb_boundary::Error>().await?.into())
}
}

0 comments on commit 031588f

Please sign in to comment.