From 3c25ab0d508efbc204daa4d725225ac983257882 Mon Sep 17 00:00:00 2001 From: mpostma Date: Tue, 16 Mar 2021 16:09:14 +0100 Subject: [PATCH] replace body with json --- meilisearch-http/src/routes/document.rs | 61 ++++++--------------- meilisearch-http/src/routes/index.rs | 44 +++++---------- meilisearch-http/src/routes/search.rs | 16 ++---- meilisearch-http/src/routes/settings/mod.rs | 43 ++++----------- 4 files changed, 46 insertions(+), 118 deletions(-) diff --git a/meilisearch-http/src/routes/document.rs b/meilisearch-http/src/routes/document.rs index a5c071ac3..8e337d275 100644 --- a/meilisearch-http/src/routes/document.rs +++ b/meilisearch-http/src/routes/document.rs @@ -65,12 +65,9 @@ async fn get_document( .retrieve_document(index, id, None as Option>) .await { - Ok(document) => { - let json = serde_json::to_string(&document).unwrap(); - Ok(HttpResponse::Ok().body(json)) - } + Ok(document) => Ok(HttpResponse::Ok().json(document)), Err(e) => { - Ok(HttpResponse::BadRequest().body(serde_json::json!({ "error": e.to_string() }))) + Ok(HttpResponse::BadRequest().json(serde_json::json!({ "error": e.to_string() }))) } } } @@ -87,12 +84,9 @@ async fn delete_document( .delete_documents(path.index_uid.clone(), vec![path.document_id.clone()]) .await { - Ok(result) => { - let json = serde_json::to_string(&result).unwrap(); - Ok(HttpResponse::Ok().body(json)) - } + Ok(result) => Ok(HttpResponse::Ok().json(result)), Err(e) => { - Ok(HttpResponse::BadRequest().body(serde_json::json!({ "error": e.to_string() }))) + Ok(HttpResponse::BadRequest().json(serde_json::json!({ "error": e.to_string() }))) } } } @@ -125,12 +119,9 @@ async fn get_all_documents( ) .await { - Ok(docs) => { - let json = serde_json::to_string(&docs).unwrap(); - Ok(HttpResponse::Ok().body(json)) - } + Ok(docs) => Ok(HttpResponse::Ok().json(docs)), Err(e) => { - Ok(HttpResponse::BadRequest().body(serde_json::json!({ "error": e.to_string() }))) + Ok(HttpResponse::BadRequest().json(serde_json::json!({ "error": e.to_string() }))) } } } @@ -142,10 +133,7 @@ struct UpdateDocumentsQuery { } /// Route used when the payload type is "application/json" -#[post( - "/indexes/{index_uid}/documents", - wrap = "Authentication::Private", -)] +#[post("/indexes/{index_uid}/documents", wrap = "Authentication::Private")] async fn add_documents( data: web::Data, path: web::Path, @@ -163,13 +151,9 @@ async fn add_documents( .await; match addition_result { - Ok(update) => { - let value = serde_json::to_string(&update).unwrap(); - let response = HttpResponse::Ok().body(value); - Ok(response) - } + Ok(update) => Ok(HttpResponse::Ok().json(update)), Err(e) => { - Ok(HttpResponse::BadRequest().body(serde_json::json!({ "error": e.to_string() }))) + Ok(HttpResponse::BadRequest().json(serde_json::json!({ "error": e.to_string() }))) } } } @@ -198,10 +182,7 @@ async fn update_documents_default( todo!() } -#[put( - "/indexes/{index_uid}/documents", - wrap = "Authentication::Private", -)] +#[put("/indexes/{index_uid}/documents", wrap = "Authentication::Private")] async fn update_documents( data: web::Data, path: web::Path, @@ -219,13 +200,9 @@ async fn update_documents( .await; match addition_result { - Ok(update) => { - let value = serde_json::to_string(&update).unwrap(); - let response = HttpResponse::Ok().body(value); - Ok(response) - } + Ok(update) => Ok(HttpResponse::Ok().json(update)), Err(e) => { - Ok(HttpResponse::BadRequest().body(serde_json::json!({ "error": e.to_string() }))) + Ok(HttpResponse::BadRequest().json(serde_json::json!({ "error": e.to_string() }))) } } } @@ -249,12 +226,9 @@ async fn delete_documents( .collect(); match data.delete_documents(path.index_uid.clone(), ids).await { - Ok(result) => { - let json = serde_json::to_string(&result).unwrap(); - Ok(HttpResponse::Ok().body(json)) - } + Ok(result) => Ok(HttpResponse::Ok().json(result)), Err(e) => { - Ok(HttpResponse::BadRequest().body(serde_json::json!({ "error": e.to_string() }))) + Ok(HttpResponse::BadRequest().json(serde_json::json!({ "error": e.to_string() }))) } } } @@ -265,12 +239,9 @@ async fn clear_all_documents( path: web::Path, ) -> Result { match data.clear_documents(path.index_uid.clone()).await { - Ok(update) => { - let json = serde_json::to_string(&update).unwrap(); - Ok(HttpResponse::Ok().body(json)) - } + Ok(update) => Ok(HttpResponse::Ok().json(update)), Err(e) => { - Ok(HttpResponse::BadRequest().body(serde_json::json!({ "error": e.to_string() }))) + Ok(HttpResponse::BadRequest().json(serde_json::json!({ "error": e.to_string() }))) } } } diff --git a/meilisearch-http/src/routes/index.rs b/meilisearch-http/src/routes/index.rs index 29ba3d0d8..d5fac5170 100644 --- a/meilisearch-http/src/routes/index.rs +++ b/meilisearch-http/src/routes/index.rs @@ -21,12 +21,9 @@ pub fn services(cfg: &mut web::ServiceConfig) { #[get("/indexes", wrap = "Authentication::Private")] async fn list_indexes(data: web::Data) -> Result { match data.list_indexes().await { - Ok(indexes) => { - let json = serde_json::to_string(&indexes).unwrap(); - Ok(HttpResponse::Ok().body(&json)) - } + Ok(indexes) => Ok(HttpResponse::Ok().json(indexes)), Err(e) => { - Ok(HttpResponse::BadRequest().body(serde_json::json!({ "error": e.to_string() }))) + Ok(HttpResponse::BadRequest().json(serde_json::json!({ "error": e.to_string() }))) } } } @@ -37,12 +34,9 @@ async fn get_index( path: web::Path, ) -> Result { match data.index(path.index_uid.clone()).await { - Ok(meta) => { - let json = serde_json::to_string(&meta).unwrap(); - Ok(HttpResponse::Ok().body(json)) - } + Ok(meta) => Ok(HttpResponse::Ok().json(meta)), Err(e) => { - Ok(HttpResponse::BadRequest().body(serde_json::json!({ "error": e.to_string() }))) + Ok(HttpResponse::BadRequest().json(serde_json::json!({ "error": e.to_string() }))) } } } @@ -61,12 +55,9 @@ async fn create_index( ) -> Result { let body = body.into_inner(); match data.create_index(body.uid, body.primary_key).await { - Ok(meta) => { - let json = serde_json::to_string(&meta).unwrap(); - Ok(HttpResponse::Ok().body(json)) - } + Ok(meta) => Ok(HttpResponse::Ok().json(meta)), Err(e) => { - Ok(HttpResponse::BadRequest().body(serde_json::json!({ "error": e.to_string() }))) + Ok(HttpResponse::BadRequest().json(serde_json::json!({ "error": e.to_string() }))) } } } @@ -99,12 +90,9 @@ async fn update_index( .update_index(path.into_inner().index_uid, body.primary_key, body.uid) .await { - Ok(meta) => { - let json = serde_json::to_string(&meta).unwrap(); - Ok(HttpResponse::Ok().body(json)) - } + Ok(meta) => Ok(HttpResponse::Ok().json(meta)), Err(e) => { - Ok(HttpResponse::BadRequest().body(serde_json::json!({ "error": e.to_string() }))) + Ok(HttpResponse::BadRequest().json(serde_json::json!({ "error": e.to_string() }))) } } } @@ -117,7 +105,7 @@ async fn delete_index( match data.delete_index(path.index_uid.clone()).await { Ok(_) => Ok(HttpResponse::NoContent().finish()), Err(e) => { - Ok(HttpResponse::BadRequest().body(serde_json::json!({ "error": e.to_string() }))) + Ok(HttpResponse::BadRequest().json(serde_json::json!({ "error": e.to_string() }))) } } } @@ -141,12 +129,9 @@ async fn get_update_status( .get_update_status(params.index_uid, params.update_id) .await; match result { - Ok(meta) => { - let json = serde_json::to_string(&meta).unwrap(); - Ok(HttpResponse::Ok().body(json)) - } + Ok(meta) => Ok(HttpResponse::Ok().json(meta)), Err(e) => { - Ok(HttpResponse::BadRequest().body(serde_json::json!({ "error": e.to_string() }))) + Ok(HttpResponse::BadRequest().json(serde_json::json!({ "error": e.to_string() }))) } } } @@ -158,12 +143,9 @@ async fn get_all_updates_status( ) -> Result { let result = data.get_updates_status(path.into_inner().index_uid).await; match result { - Ok(metas) => { - let json = serde_json::to_string(&metas).unwrap(); - Ok(HttpResponse::Ok().body(json)) - } + Ok(metas) => Ok(HttpResponse::Ok().json(metas)), Err(e) => { - Ok(HttpResponse::BadRequest().body(serde_json::json!({ "error": e.to_string() }))) + Ok(HttpResponse::BadRequest().json(serde_json::json!({ "error": e.to_string() }))) } } } diff --git a/meilisearch-http/src/routes/search.rs b/meilisearch-http/src/routes/search.rs index c98a5ac51..ae2caeb30 100644 --- a/meilisearch-http/src/routes/search.rs +++ b/meilisearch-http/src/routes/search.rs @@ -81,18 +81,15 @@ async fn search_with_url_query( Ok(q) => q, Err(e) => { return Ok( - HttpResponse::BadRequest().body(serde_json::json!({ "error": e.to_string() })) + HttpResponse::BadRequest().json(serde_json::json!({ "error": e.to_string() })) ) } }; let search_result = data.search(path.into_inner().index_uid, query).await; match search_result { - Ok(docs) => { - let docs = serde_json::to_string(&docs).unwrap(); - Ok(HttpResponse::Ok().body(docs)) - } + Ok(docs) => Ok(HttpResponse::Ok().json(docs)), Err(e) => { - Ok(HttpResponse::BadRequest().body(serde_json::json!({ "error": e.to_string() }))) + Ok(HttpResponse::BadRequest().json(serde_json::json!({ "error": e.to_string() }))) } } } @@ -107,12 +104,9 @@ async fn search_with_post( .search(path.into_inner().index_uid, params.into_inner()) .await; match search_result { - Ok(docs) => { - let docs = serde_json::to_string(&docs).unwrap(); - Ok(HttpResponse::Ok().body(docs)) - } + Ok(docs) => Ok(HttpResponse::Ok().json(docs)), Err(e) => { - Ok(HttpResponse::BadRequest().body(serde_json::json!({ "error": e.to_string() }))) + Ok(HttpResponse::BadRequest().json(serde_json::json!({ "error": e.to_string() }))) } } } diff --git a/meilisearch-http/src/routes/settings/mod.rs b/meilisearch-http/src/routes/settings/mod.rs index 5a6246f8c..6a7bfce48 100644 --- a/meilisearch-http/src/routes/settings/mod.rs +++ b/meilisearch-http/src/routes/settings/mod.rs @@ -27,12 +27,9 @@ macro_rules! make_setting_route { ..Default::default() }; match data.update_settings(index_uid.into_inner(), settings, false).await { - Ok(update_status) => { - let json = serde_json::to_string(&update_status).unwrap(); - Ok(HttpResponse::Ok().body(json)) - } + Ok(update_status) => Ok(HttpResponse::Ok().json(update_status)), Err(e) => { - Ok(HttpResponse::BadRequest().body(serde_json::json!({ "error": e.to_string() }))) + Ok(HttpResponse::BadRequest().json(serde_json::json!({ "error": e.to_string() }))) } } } @@ -49,12 +46,9 @@ macro_rules! make_setting_route { }; match data.update_settings(index_uid.into_inner(), settings, true).await { - Ok(update_status) => { - let json = serde_json::to_string(&update_status).unwrap(); - Ok(HttpResponse::Ok().body(json)) - } + Ok(update_status) => Ok(HttpResponse::Ok().json(update_status)), Err(e) => { - Ok(HttpResponse::BadRequest().body(serde_json::json!({ "error": e.to_string() }))) + Ok(HttpResponse::BadRequest().json(serde_json::json!({ "error": e.to_string() }))) } } } @@ -65,13 +59,9 @@ macro_rules! make_setting_route { index_uid: actix_web::web::Path, ) -> std::result::Result { match data.settings(index_uid.into_inner()).await { - Ok(settings) => { - let setting = settings.$attr; - let json = serde_json::to_string(&setting).unwrap(); - Ok(HttpResponse::Ok().body(json)) - } + Ok(settings) => Ok(HttpResponse::Ok().json(settings.$attr)), Err(e) => { - Ok(HttpResponse::BadRequest().body(serde_json::json!({ "error": e.to_string() }))) + Ok(HttpResponse::BadRequest().json(serde_json::json!({ "error": e.to_string() }))) } } } @@ -141,12 +131,9 @@ async fn update_all( .update_settings(index_uid.into_inner(), body.into_inner(), true) .await { - Ok(update_result) => { - let json = serde_json::to_string(&update_result).unwrap(); - Ok(HttpResponse::Ok().body(json)) - } + Ok(update_result) => Ok(HttpResponse::Ok().json(update_result)), Err(e) => { - Ok(HttpResponse::BadRequest().body(serde_json::json!({ "error": e.to_string() }))) + Ok(HttpResponse::BadRequest().json(serde_json::json!({ "error": e.to_string() }))) } } } @@ -157,12 +144,9 @@ async fn get_all( index_uid: web::Path, ) -> Result { match data.settings(index_uid.into_inner()).await { - Ok(settings) => { - let json = serde_json::to_string(&settings).unwrap(); - Ok(HttpResponse::Ok().body(json)) - } + Ok(settings) => Ok(HttpResponse::Ok().json(settings)), Err(e) => { - Ok(HttpResponse::BadRequest().body(serde_json::json!({ "error": e.to_string() }))) + Ok(HttpResponse::BadRequest().json(serde_json::json!({ "error": e.to_string() }))) } } } @@ -177,12 +161,9 @@ async fn delete_all( .update_settings(index_uid.into_inner(), settings, false) .await { - Ok(update_result) => { - let json = serde_json::to_string(&update_result).unwrap(); - Ok(HttpResponse::Ok().body(json)) - } + Ok(update_result) => Ok(HttpResponse::Ok().json(update_result)), Err(e) => { - Ok(HttpResponse::BadRequest().body(serde_json::json!({ "error": e.to_string() }))) + Ok(HttpResponse::BadRequest().json(serde_json::json!({ "error": e.to_string() }))) } } }