Skip to content
Extraits de code Groupes Projets
Vérifiée Valider d969fbe6 rédigé par Kubat's avatar Kubat
Parcourir les fichiers

LEKTORD: Set the content type when we return hand-crafter json things

parent aeb27082
Aucune branche associée trouvée
Aucune étiquette associée trouvée
1 requête de fusion!197Draft: Refactor the whole code.
......@@ -9,9 +9,11 @@ use crate::*;
use anyhow::{anyhow, Error};
use axum::{
extract::{Path, State},
http::StatusCode,
http::{HeaderValue, StatusCode},
response::{IntoResponse, Response},
Json,
};
use hyper::header::CONTENT_TYPE;
use lektor_nkdb::*;
use lektor_payloads::*;
use lektor_utils::{decode_base64_json, log};
......@@ -105,11 +107,13 @@ pub(crate) async fn toggle_play_state() -> Result<(), LektordError> {
pub(crate) async fn get_kara_by_id(
State(state): State<LektorStatePtr>,
Path(id): Path<u64>,
) -> Result<String, LektordError> {
Ok(
serde_json::to_string(state.database.get_kara_by_id(id).await?)
.map_err(|err| anyhow!("{err}"))?,
)
) -> Result<Response, LektordError> {
let mut kara = serde_json::to_string(state.database.get_kara_by_id(id).await?)
.map_err(|err| anyhow!("{err}"))?
.into_response();
kara.headers_mut()
.insert(CONTENT_TYPE, HeaderValue::from_static("application/json"));
Ok(kara)
}
/// Get all the informations about a kara by its id. Returns the kara as a json object to avoid
......@@ -118,14 +122,18 @@ pub(crate) async fn get_kara_by_id(
pub(crate) async fn get_kara_by_kid(
State(state): State<LektorStatePtr>,
Path(id): Path<String>,
) -> Result<String, LektordError> {
match state.database.get_kid_from_str(&decode_base64(&id)?).await {
Some(id) => Ok(
serde_json::to_string(state.database.get_kara_by_kid(id).await?)
.map_err(|err| anyhow!("{err}"))?,
),
None => Err(anyhow!("no kara found with id {id}").into()),
}
) -> Result<Response, LektordError> {
let id = state
.database
.get_kid_from_str(&decode_base64(&id)?)
.await
.ok_or_else(|| anyhow!("no kara found with id {id}"))?;
let mut kara = serde_json::to_string(state.database.get_kara_by_kid(id).await?)
.map_err(|err| anyhow!("{err}"))?
.into_response();
kara.headers_mut()
.insert(CONTENT_TYPE, HeaderValue::from_static("application/json"));
Ok(kara)
}
/// Search some karas from a search set with a regex. We take the json argument as base64 encoded
......
0% Chargement en cours ou .
You are about to add 0 people to the discussion. Proceed with caution.
Veuillez vous inscrire ou vous pour commenter