replace body with json

This commit is contained in:
mpostma 2021-03-16 16:09:14 +01:00
parent 13c5289ff1
commit 3c25ab0d50
No known key found for this signature in database
GPG Key ID: CBC8A7C1D7A28C3A
4 changed files with 46 additions and 118 deletions

View File

@ -65,12 +65,9 @@ async fn get_document(
.retrieve_document(index, id, None as Option<Vec<String>>) .retrieve_document(index, id, None as Option<Vec<String>>)
.await .await
{ {
Ok(document) => { Ok(document) => Ok(HttpResponse::Ok().json(document)),
let json = serde_json::to_string(&document).unwrap();
Ok(HttpResponse::Ok().body(json))
}
Err(e) => { 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()]) .delete_documents(path.index_uid.clone(), vec![path.document_id.clone()])
.await .await
{ {
Ok(result) => { Ok(result) => Ok(HttpResponse::Ok().json(result)),
let json = serde_json::to_string(&result).unwrap();
Ok(HttpResponse::Ok().body(json))
}
Err(e) => { 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 .await
{ {
Ok(docs) => { Ok(docs) => Ok(HttpResponse::Ok().json(docs)),
let json = serde_json::to_string(&docs).unwrap();
Ok(HttpResponse::Ok().body(json))
}
Err(e) => { 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" /// Route used when the payload type is "application/json"
#[post( #[post("/indexes/{index_uid}/documents", wrap = "Authentication::Private")]
"/indexes/{index_uid}/documents",
wrap = "Authentication::Private",
)]
async fn add_documents( async fn add_documents(
data: web::Data<Data>, data: web::Data<Data>,
path: web::Path<IndexParam>, path: web::Path<IndexParam>,
@ -163,13 +151,9 @@ async fn add_documents(
.await; .await;
match addition_result { match addition_result {
Ok(update) => { Ok(update) => Ok(HttpResponse::Ok().json(update)),
let value = serde_json::to_string(&update).unwrap();
let response = HttpResponse::Ok().body(value);
Ok(response)
}
Err(e) => { 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!() todo!()
} }
#[put( #[put("/indexes/{index_uid}/documents", wrap = "Authentication::Private")]
"/indexes/{index_uid}/documents",
wrap = "Authentication::Private",
)]
async fn update_documents( async fn update_documents(
data: web::Data<Data>, data: web::Data<Data>,
path: web::Path<IndexParam>, path: web::Path<IndexParam>,
@ -219,13 +200,9 @@ async fn update_documents(
.await; .await;
match addition_result { match addition_result {
Ok(update) => { Ok(update) => Ok(HttpResponse::Ok().json(update)),
let value = serde_json::to_string(&update).unwrap();
let response = HttpResponse::Ok().body(value);
Ok(response)
}
Err(e) => { 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(); .collect();
match data.delete_documents(path.index_uid.clone(), ids).await { match data.delete_documents(path.index_uid.clone(), ids).await {
Ok(result) => { Ok(result) => Ok(HttpResponse::Ok().json(result)),
let json = serde_json::to_string(&result).unwrap();
Ok(HttpResponse::Ok().body(json))
}
Err(e) => { 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<IndexParam>, path: web::Path<IndexParam>,
) -> Result<HttpResponse, ResponseError> { ) -> Result<HttpResponse, ResponseError> {
match data.clear_documents(path.index_uid.clone()).await { match data.clear_documents(path.index_uid.clone()).await {
Ok(update) => { Ok(update) => Ok(HttpResponse::Ok().json(update)),
let json = serde_json::to_string(&update).unwrap();
Ok(HttpResponse::Ok().body(json))
}
Err(e) => { Err(e) => {
Ok(HttpResponse::BadRequest().body(serde_json::json!({ "error": e.to_string() }))) Ok(HttpResponse::BadRequest().json(serde_json::json!({ "error": e.to_string() })))
} }
} }
} }

View File

@ -21,12 +21,9 @@ pub fn services(cfg: &mut web::ServiceConfig) {
#[get("/indexes", wrap = "Authentication::Private")] #[get("/indexes", wrap = "Authentication::Private")]
async fn list_indexes(data: web::Data<Data>) -> Result<HttpResponse, ResponseError> { async fn list_indexes(data: web::Data<Data>) -> Result<HttpResponse, ResponseError> {
match data.list_indexes().await { match data.list_indexes().await {
Ok(indexes) => { Ok(indexes) => Ok(HttpResponse::Ok().json(indexes)),
let json = serde_json::to_string(&indexes).unwrap();
Ok(HttpResponse::Ok().body(&json))
}
Err(e) => { 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<IndexParam>, path: web::Path<IndexParam>,
) -> Result<HttpResponse, ResponseError> { ) -> Result<HttpResponse, ResponseError> {
match data.index(path.index_uid.clone()).await { match data.index(path.index_uid.clone()).await {
Ok(meta) => { Ok(meta) => Ok(HttpResponse::Ok().json(meta)),
let json = serde_json::to_string(&meta).unwrap();
Ok(HttpResponse::Ok().body(json))
}
Err(e) => { 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<HttpResponse, ResponseError> { ) -> Result<HttpResponse, ResponseError> {
let body = body.into_inner(); let body = body.into_inner();
match data.create_index(body.uid, body.primary_key).await { match data.create_index(body.uid, body.primary_key).await {
Ok(meta) => { Ok(meta) => Ok(HttpResponse::Ok().json(meta)),
let json = serde_json::to_string(&meta).unwrap();
Ok(HttpResponse::Ok().body(json))
}
Err(e) => { 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) .update_index(path.into_inner().index_uid, body.primary_key, body.uid)
.await .await
{ {
Ok(meta) => { Ok(meta) => Ok(HttpResponse::Ok().json(meta)),
let json = serde_json::to_string(&meta).unwrap();
Ok(HttpResponse::Ok().body(json))
}
Err(e) => { 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 { match data.delete_index(path.index_uid.clone()).await {
Ok(_) => Ok(HttpResponse::NoContent().finish()), Ok(_) => Ok(HttpResponse::NoContent().finish()),
Err(e) => { 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) .get_update_status(params.index_uid, params.update_id)
.await; .await;
match result { match result {
Ok(meta) => { Ok(meta) => Ok(HttpResponse::Ok().json(meta)),
let json = serde_json::to_string(&meta).unwrap();
Ok(HttpResponse::Ok().body(json))
}
Err(e) => { 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<HttpResponse, ResponseError> { ) -> Result<HttpResponse, ResponseError> {
let result = data.get_updates_status(path.into_inner().index_uid).await; let result = data.get_updates_status(path.into_inner().index_uid).await;
match result { match result {
Ok(metas) => { Ok(metas) => Ok(HttpResponse::Ok().json(metas)),
let json = serde_json::to_string(&metas).unwrap();
Ok(HttpResponse::Ok().body(json))
}
Err(e) => { Err(e) => {
Ok(HttpResponse::BadRequest().body(serde_json::json!({ "error": e.to_string() }))) Ok(HttpResponse::BadRequest().json(serde_json::json!({ "error": e.to_string() })))
} }
} }
} }

View File

@ -81,18 +81,15 @@ async fn search_with_url_query(
Ok(q) => q, Ok(q) => q,
Err(e) => { Err(e) => {
return Ok( 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; let search_result = data.search(path.into_inner().index_uid, query).await;
match search_result { match search_result {
Ok(docs) => { Ok(docs) => Ok(HttpResponse::Ok().json(docs)),
let docs = serde_json::to_string(&docs).unwrap();
Ok(HttpResponse::Ok().body(docs))
}
Err(e) => { 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()) .search(path.into_inner().index_uid, params.into_inner())
.await; .await;
match search_result { match search_result {
Ok(docs) => { Ok(docs) => Ok(HttpResponse::Ok().json(docs)),
let docs = serde_json::to_string(&docs).unwrap();
Ok(HttpResponse::Ok().body(docs))
}
Err(e) => { Err(e) => {
Ok(HttpResponse::BadRequest().body(serde_json::json!({ "error": e.to_string() }))) Ok(HttpResponse::BadRequest().json(serde_json::json!({ "error": e.to_string() })))
} }
} }
} }

View File

@ -27,12 +27,9 @@ macro_rules! make_setting_route {
..Default::default() ..Default::default()
}; };
match data.update_settings(index_uid.into_inner(), settings, false).await { match data.update_settings(index_uid.into_inner(), settings, false).await {
Ok(update_status) => { Ok(update_status) => Ok(HttpResponse::Ok().json(update_status)),
let json = serde_json::to_string(&update_status).unwrap();
Ok(HttpResponse::Ok().body(json))
}
Err(e) => { 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 { match data.update_settings(index_uid.into_inner(), settings, true).await {
Ok(update_status) => { Ok(update_status) => Ok(HttpResponse::Ok().json(update_status)),
let json = serde_json::to_string(&update_status).unwrap();
Ok(HttpResponse::Ok().body(json))
}
Err(e) => { 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<String>, index_uid: actix_web::web::Path<String>,
) -> std::result::Result<HttpResponse, ResponseError> { ) -> std::result::Result<HttpResponse, ResponseError> {
match data.settings(index_uid.into_inner()).await { match data.settings(index_uid.into_inner()).await {
Ok(settings) => { Ok(settings) => Ok(HttpResponse::Ok().json(settings.$attr)),
let setting = settings.$attr;
let json = serde_json::to_string(&setting).unwrap();
Ok(HttpResponse::Ok().body(json))
}
Err(e) => { 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) .update_settings(index_uid.into_inner(), body.into_inner(), true)
.await .await
{ {
Ok(update_result) => { Ok(update_result) => Ok(HttpResponse::Ok().json(update_result)),
let json = serde_json::to_string(&update_result).unwrap();
Ok(HttpResponse::Ok().body(json))
}
Err(e) => { 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<String>, index_uid: web::Path<String>,
) -> Result<HttpResponse, ResponseError> { ) -> Result<HttpResponse, ResponseError> {
match data.settings(index_uid.into_inner()).await { match data.settings(index_uid.into_inner()).await {
Ok(settings) => { Ok(settings) => Ok(HttpResponse::Ok().json(settings)),
let json = serde_json::to_string(&settings).unwrap();
Ok(HttpResponse::Ok().body(json))
}
Err(e) => { 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) .update_settings(index_uid.into_inner(), settings, false)
.await .await
{ {
Ok(update_result) => { Ok(update_result) => Ok(HttpResponse::Ok().json(update_result)),
let json = serde_json::to_string(&update_result).unwrap();
Ok(HttpResponse::Ok().body(json))
}
Err(e) => { Err(e) => {
Ok(HttpResponse::BadRequest().body(serde_json::json!({ "error": e.to_string() }))) Ok(HttpResponse::BadRequest().json(serde_json::json!({ "error": e.to_string() })))
} }
} }
} }