less json overhead

This commit is contained in:
Shautvast 2026-04-03 18:07:11 +02:00
parent 2e326b8cd7
commit ea1a4a4a31
4 changed files with 27 additions and 34 deletions

View file

@ -83,7 +83,7 @@ pub async fn route(
} }
// Call OSRM // Call OSRM
let json = osrm let (body, osrm_code) = osrm
.route( .route(
&profile, &profile,
&coordinates, &coordinates,
@ -95,7 +95,7 @@ pub async fn route(
.await?; .await?;
// Check OSRM response code // Check OSRM response code
if let Some(code) = json.get("code").and_then(|c| c.as_str()) { if let Some(code) = osrm_code.as_deref() {
match map_osrm_code(code) { match map_osrm_code(code) {
OsrmCodeMapping::Ok => {} // Success, continue OsrmCodeMapping::Ok => {} // Success, continue
OsrmCodeMapping::NoRoute => { OsrmCodeMapping::NoRoute => {
@ -119,9 +119,6 @@ pub async fn route(
} }
} }
let body = serde_json::to_string(&json)
.map_err(|e| AppError::InternalError(format!("JSON serialization failed: {e}")))?;
// Cache for 30 minutes // Cache for 30 minutes
cache.set_json(&cache_key, &body, 1800).await; cache.set_json(&cache_key, &body, 1800).await;

View file

@ -88,13 +88,10 @@ pub async fn search(
} }
// Call Photon // Call Photon
let json = photon let body = photon
.search(&q_clean, query.lat, query.lon, limit, lang, query.bbox.as_deref()) .search(&q_clean, query.lat, query.lon, limit, lang, query.bbox.as_deref())
.await?; .await?;
let body = serde_json::to_string(&json)
.map_err(|e| AppError::InternalError(format!("JSON serialization failed: {e}")))?;
// Cache the result // Cache the result
cache.set_json(&cache_key, &body, 3600).await; cache.set_json(&cache_key, &body, 3600).await;
@ -165,9 +162,7 @@ pub async fn reverse(
.body(cached)); .body(cached));
} }
let json = photon.reverse(lat, lon, limit, lang).await?; let body = photon.reverse(lat, lon, limit, lang).await?;
let body = serde_json::to_string(&json)
.map_err(|e| AppError::InternalError(format!("JSON serialization failed: {e}")))?;
cache.set_json(&cache_key, &body, 3600).await; cache.set_json(&cache_key, &body, 3600).await;

View file

@ -32,7 +32,7 @@ impl OsrmService {
} }
/// Request a route from the appropriate OSRM instance. /// Request a route from the appropriate OSRM instance.
/// Returns raw JSON from OSRM. /// Returns the raw JSON body and the OSRM `code` field for error mapping.
pub async fn route( pub async fn route(
&self, &self,
profile: &str, profile: &str,
@ -41,7 +41,7 @@ impl OsrmService {
steps: bool, steps: bool,
geometries: &str, geometries: &str,
overview: &str, overview: &str,
) -> Result<serde_json::Value, AppError> { ) -> Result<(String, Option<String>), AppError> {
let base = self.base_url_for_profile(profile).ok_or_else(|| { let base = self.base_url_for_profile(profile).ok_or_else(|| {
AppError::InvalidParameter(format!("Invalid profile: {profile}")) AppError::InvalidParameter(format!("Invalid profile: {profile}"))
})?; })?;
@ -64,12 +64,17 @@ impl OsrmService {
))); )));
} }
let json: serde_json::Value = resp.json().await.map_err(|e| { let body = resp.text().await.map_err(|e| {
tracing::error!(error = %e, "Failed to parse OSRM response"); tracing::error!(error = %e, "Failed to read OSRM response");
AppError::UpstreamError("Failed to parse routing response".into()) AppError::UpstreamError("Failed to read routing response".into())
})?; })?;
Ok(json) // Extract only the `code` field to avoid a full parse.
#[derive(serde::Deserialize)]
struct OsrmCode { code: Option<String> }
let code = serde_json::from_str::<OsrmCode>(&body).ok().and_then(|r| r.code);
Ok((body, code))
} }
/// Health probe for a specific OSRM profile. /// Health probe for a specific OSRM profile.

View file

@ -18,7 +18,7 @@ impl PhotonService {
} }
} }
/// Forward geocoding search. Returns raw JSON from Photon. /// Forward geocoding search. Returns the raw JSON body from Photon.
pub async fn search( pub async fn search(
&self, &self,
q: &str, q: &str,
@ -27,7 +27,7 @@ impl PhotonService {
limit: u32, limit: u32,
lang: &str, lang: &str,
bbox: Option<&str>, bbox: Option<&str>,
) -> Result<serde_json::Value, AppError> { ) -> Result<String, AppError> {
let mut url = format!("{}/api?q={}&limit={}&lang={}", self.base_url, urlencod(q), limit, lang); let mut url = format!("{}/api?q={}&limit={}&lang={}", self.base_url, urlencod(q), limit, lang);
if let (Some(lat), Some(lon)) = (lat, lon) { if let (Some(lat), Some(lon)) = (lat, lon) {
@ -50,22 +50,20 @@ impl PhotonService {
))); )));
} }
let json: serde_json::Value = resp.json().await.map_err(|e| { resp.text().await.map_err(|e| {
tracing::error!(error = %e, "Failed to parse Photon response"); tracing::error!(error = %e, "Failed to read Photon response");
AppError::UpstreamError("Failed to parse search response".into()) AppError::UpstreamError("Failed to read search response".into())
})?; })
Ok(json)
} }
/// Reverse geocoding. Returns raw JSON from Photon. /// Reverse geocoding. Returns the raw JSON body from Photon.
pub async fn reverse( pub async fn reverse(
&self, &self,
lat: f64, lat: f64,
lon: f64, lon: f64,
limit: u32, limit: u32,
lang: &str, lang: &str,
) -> Result<serde_json::Value, AppError> { ) -> Result<String, AppError> {
let url = format!( let url = format!(
"{}/reverse?lat={lat}&lon={lon}&limit={limit}&lang={lang}", "{}/reverse?lat={lat}&lon={lon}&limit={limit}&lang={lang}",
self.base_url self.base_url
@ -83,12 +81,10 @@ impl PhotonService {
))); )));
} }
let json: serde_json::Value = resp.json().await.map_err(|e| { resp.text().await.map_err(|e| {
tracing::error!(error = %e, "Failed to parse Photon reverse response"); tracing::error!(error = %e, "Failed to read Photon reverse response");
AppError::UpstreamError("Failed to parse reverse geocoding response".into()) AppError::UpstreamError("Failed to read reverse geocoding response".into())
})?; })
Ok(json)
} }
/// Health probe. /// Health probe.