Skip to content
This repository has been archived by the owner on Jul 1, 2021. It is now read-only.

replace body with json #98

Merged
merged 1 commit into from
Mar 16, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
61 changes: 16 additions & 45 deletions meilisearch-http/src/routes/document.rs
Original file line number Diff line number Diff line change
Expand Up @@ -65,12 +65,9 @@ async fn get_document(
.retrieve_document(index, id, None as Option<Vec<String>>)
.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() })))
}
}
}
Expand All @@ -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() })))
}
}
}
Expand Down Expand Up @@ -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() })))
}
}
}
Expand All @@ -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<Data>,
path: web::Path<IndexParam>,
Expand All @@ -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() })))
}
}
}
Expand Down Expand Up @@ -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<Data>,
path: web::Path<IndexParam>,
Expand All @@ -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() })))
}
}
}
Expand All @@ -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() })))
}
}
}
Expand All @@ -265,12 +239,9 @@ async fn clear_all_documents(
path: web::Path<IndexParam>,
) -> Result<HttpResponse, ResponseError> {
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() })))
}
}
}
44 changes: 13 additions & 31 deletions meilisearch-http/src/routes/index.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,12 +21,9 @@ pub fn services(cfg: &mut web::ServiceConfig) {
#[get("/indexes", wrap = "Authentication::Private")]
async fn list_indexes(data: web::Data<Data>) -> Result<HttpResponse, ResponseError> {
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() })))
}
}
}
Expand All @@ -37,12 +34,9 @@ async fn get_index(
path: web::Path<IndexParam>,
) -> Result<HttpResponse, ResponseError> {
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() })))
}
}
}
Expand All @@ -61,12 +55,9 @@ async fn create_index(
) -> Result<HttpResponse, ResponseError> {
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() })))
}
}
}
Expand Down Expand Up @@ -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() })))
}
}
}
Expand All @@ -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() })))
}
}
}
Expand All @@ -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() })))
}
}
}
Expand All @@ -158,12 +143,9 @@ async fn get_all_updates_status(
) -> Result<HttpResponse, ResponseError> {
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() })))
}
}
}
16 changes: 5 additions & 11 deletions meilisearch-http/src/routes/search.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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() })))
}
}
}
Expand All @@ -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() })))
}
}
}
43 changes: 12 additions & 31 deletions meilisearch-http/src/routes/settings/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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() })))
}
}
}
Expand All @@ -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() })))
}
}
}
Expand All @@ -65,13 +59,9 @@ macro_rules! make_setting_route {
index_uid: actix_web::web::Path<String>,
) -> std::result::Result<HttpResponse, ResponseError> {
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() })))
}
}
}
Expand Down Expand Up @@ -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() })))
}
}
}
Expand All @@ -157,12 +144,9 @@ async fn get_all(
index_uid: web::Path<String>,
) -> Result<HttpResponse, ResponseError> {
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() })))
}
}
}
Expand All @@ -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() })))
}
}
}