From 2b3c91aabd0fb2a016a71fcb1bc4074f14266e10 Mon Sep 17 00:00:00 2001 From: Quentin de Quelen Date: Tue, 19 Nov 2019 17:39:30 +0100 Subject: [PATCH] Update get_index_schema to allow raw response --- meilidb-http/src/routes/index.rs | 27 ++++++++++++++++++--------- meilidb-schema/src/lib.rs | 2 +- 2 files changed, 19 insertions(+), 10 deletions(-) diff --git a/meilidb-http/src/routes/index.rs b/meilidb-http/src/routes/index.rs index b31a9b301..540900768 100644 --- a/meilidb-http/src/routes/index.rs +++ b/meilidb-http/src/routes/index.rs @@ -1,9 +1,11 @@ -use meilidb_schema::Schema; +use chrono::{DateTime, Utc}; use http::StatusCode; use meilidb_core::ProcessedUpdateResult; +use meilidb_schema::Schema; use rand::seq::SliceRandom; use serde::{Deserialize, Serialize}; use serde_json::json; +use tide::querystring::ContextExt as QSContextExt; use tide::response::IntoResponse; use tide::{Context, Response}; use chrono::{DateTime, Utc}; @@ -39,7 +41,6 @@ pub async fn list_indexes(ctx: Context) -> SResult { struct GetIndexResponse { name: String, uid: String, - schema: Option, created_at: DateTime, updated_at: DateTime, } @@ -56,9 +57,6 @@ pub async fn get_index(ctx: Context) -> SResult { let name = index.main.name(&mut reader) .map_err(ResponseError::internal)? .ok_or(ResponseError::internal("Name not found"))?; - let schema = index.main.schema(&mut reader) - .map_err(ResponseError::internal)? - .map(|schema| SchemaBody::from(schema)); let created_at = index.main.created_at(&mut reader) .map_err(ResponseError::internal)? .ok_or(ResponseError::internal("Created date not found"))?; @@ -69,7 +67,6 @@ pub async fn get_index(ctx: Context) -> SResult { let response_body = GetIndexResponse { name, uid, - schema, created_at, updated_at, }; @@ -77,23 +74,35 @@ pub async fn get_index(ctx: Context) -> SResult { Ok(tide::response::json(response_body)) } +#[derive(Default, Deserialize)] +#[serde(rename_all = "camelCase")] +struct GetSchemaParams { + raw: bool, +} + pub async fn get_index_schema(ctx: Context) -> SResult { ctx.is_allowed(IndexesRead)?; let index = ctx.index()?; + // Tide doesn't support "no query param" + let params: GetSchemaParams = ctx.url_query().unwrap_or_default(); + let env = &ctx.state().db.env; let reader = env.read_txn().map_err(ResponseError::internal)?; let schema = index .main .schema(&reader) - .map_err(ResponseError::create_index)?; + .map_err(ResponseError::open_index)?; match schema { Some(schema) => { - let schema = SchemaBody::from(schema); - Ok(tide::response::json(schema)) + if params.raw { + Ok(tide::response::json(schema.to_builder())) + } else { + Ok(tide::response::json(SchemaBody::from(schema))) + } } None => Ok( tide::response::json(json!({ "message": "missing index schema" })) diff --git a/meilidb-schema/src/lib.rs b/meilidb-schema/src/lib.rs index 929316d41..a7125e434 100644 --- a/meilidb-schema/src/lib.rs +++ b/meilidb-schema/src/lib.rs @@ -145,7 +145,7 @@ struct InnerSchema { } impl Schema { - fn to_builder(&self) -> SchemaBuilder { + pub fn to_builder(&self) -> SchemaBuilder { let identifier = self.inner.identifier.clone(); let attributes = self.attributes_ordered(); SchemaBuilder {