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>>)
.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<Data>,
path: web::Path<IndexParam>,
@ -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<Data>,
path: web::Path<IndexParam>,
@ -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<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() })))
}
}
}

View File

@ -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() })))
}
}
}
@ -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() })))
}
}
}
@ -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() })))
}
}
}
@ -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<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() })))
}
}
}

View File

@ -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() })))
}
}
}

View File

@ -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<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() })))
}
}
}
@ -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<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() })))
}
}
}
@ -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() })))
}
}
}