Return etag in create & update responses

This commit is contained in:
Tyler Hallada 2020-11-05 18:09:43 -05:00
parent 8cb76d6ff4
commit 0980d01640
5 changed files with 20 additions and 16 deletions

View File

@ -1,7 +1,7 @@
use anyhow::Result; use anyhow::Result;
use http::StatusCode; use http::StatusCode;
use uuid::Uuid; use uuid::Uuid;
use warp::reply::{json, with_header, with_status}; use warp::reply::{with_header, with_status};
use warp::{Rejection, Reply}; use warp::{Rejection, Reply};
use crate::models::{InteriorRefList, ListParams}; use crate::models::{InteriorRefList, ListParams};
@ -77,7 +77,7 @@ pub async fn create(
let url = saved_interior_ref_list let url = saved_interior_ref_list
.url(&env.api_url) .url(&env.api_url)
.map_err(reject_anyhow)?; .map_err(reject_anyhow)?;
let reply = json(&saved_interior_ref_list); let reply = JsonWithETag::from_serializable(&saved_interior_ref_list).map_err(reject_anyhow)?;
let reply = with_header(reply, "Location", url.as_str()); let reply = with_header(reply, "Location", url.as_str());
let reply = with_status(reply, StatusCode::CREATED); let reply = with_status(reply, StatusCode::CREATED);
env.caches.list_interior_ref_lists.clear().await; env.caches.list_interior_ref_lists.clear().await;
@ -114,7 +114,8 @@ pub async fn update(
let url = updated_interior_ref_list let url = updated_interior_ref_list
.url(&env.api_url) .url(&env.api_url)
.map_err(reject_anyhow)?; .map_err(reject_anyhow)?;
let reply = json(&updated_interior_ref_list); let reply =
JsonWithETag::from_serializable(&updated_interior_ref_list).map_err(reject_anyhow)?;
let reply = with_header(reply, "Location", url.as_str()); let reply = with_header(reply, "Location", url.as_str());
let reply = with_status(reply, StatusCode::CREATED); let reply = with_status(reply, StatusCode::CREATED);
env.caches.interior_ref_list.delete_response(id).await; env.caches.interior_ref_list.delete_response(id).await;
@ -144,7 +145,8 @@ pub async fn update_by_shop_id(
let url = updated_interior_ref_list let url = updated_interior_ref_list
.url(&env.api_url) .url(&env.api_url)
.map_err(reject_anyhow)?; .map_err(reject_anyhow)?;
let reply = json(&updated_interior_ref_list); let reply =
JsonWithETag::from_serializable(&updated_interior_ref_list).map_err(reject_anyhow)?;
let reply = with_header(reply, "Location", url.as_str()); let reply = with_header(reply, "Location", url.as_str());
let reply = with_status(reply, StatusCode::CREATED); let reply = with_status(reply, StatusCode::CREATED);
env.caches env.caches

View File

@ -1,7 +1,7 @@
use anyhow::Result; use anyhow::Result;
use http::StatusCode; use http::StatusCode;
use uuid::Uuid; use uuid::Uuid;
use warp::reply::{json, with_header, with_status}; use warp::reply::{with_header, with_status};
use warp::{Rejection, Reply}; use warp::{Rejection, Reply};
use crate::models::{ListParams, MerchandiseList}; use crate::models::{ListParams, MerchandiseList};
@ -77,7 +77,7 @@ pub async fn create(
let url = saved_merchandise_list let url = saved_merchandise_list
.url(&env.api_url) .url(&env.api_url)
.map_err(reject_anyhow)?; .map_err(reject_anyhow)?;
let reply = json(&saved_merchandise_list); let reply = JsonWithETag::from_serializable(&saved_merchandise_list).map_err(reject_anyhow)?;
let reply = with_header(reply, "Location", url.as_str()); let reply = with_header(reply, "Location", url.as_str());
let reply = with_status(reply, StatusCode::CREATED); let reply = with_status(reply, StatusCode::CREATED);
env.caches.list_merchandise_lists.clear().await; env.caches.list_merchandise_lists.clear().await;
@ -114,7 +114,8 @@ pub async fn update(
let url = updated_merchandise_list let url = updated_merchandise_list
.url(&env.api_url) .url(&env.api_url)
.map_err(reject_anyhow)?; .map_err(reject_anyhow)?;
let reply = json(&updated_merchandise_list); let reply =
JsonWithETag::from_serializable(&updated_merchandise_list).map_err(reject_anyhow)?;
let reply = with_header(reply, "Location", url.as_str()); let reply = with_header(reply, "Location", url.as_str());
let reply = with_status(reply, StatusCode::CREATED); let reply = with_status(reply, StatusCode::CREATED);
env.caches.merchandise_list.delete_response(id).await; env.caches.merchandise_list.delete_response(id).await;
@ -144,7 +145,8 @@ pub async fn update_by_shop_id(
let url = updated_merchandise_list let url = updated_merchandise_list
.url(&env.api_url) .url(&env.api_url)
.map_err(reject_anyhow)?; .map_err(reject_anyhow)?;
let reply = json(&updated_merchandise_list); let reply =
JsonWithETag::from_serializable(&updated_merchandise_list).map_err(reject_anyhow)?;
let reply = with_header(reply, "Location", url.as_str()); let reply = with_header(reply, "Location", url.as_str());
let reply = with_status(reply, StatusCode::CREATED); let reply = with_status(reply, StatusCode::CREATED);
env.caches env.caches

View File

@ -3,7 +3,7 @@ use http::StatusCode;
use ipnetwork::IpNetwork; use ipnetwork::IpNetwork;
use std::net::SocketAddr; use std::net::SocketAddr;
use uuid::Uuid; use uuid::Uuid;
use warp::reply::{json, with_header, with_status}; use warp::reply::{with_header, with_status};
use warp::{Rejection, Reply}; use warp::{Rejection, Reply};
use crate::models::{ListParams, Owner}; use crate::models::{ListParams, Owner};
@ -69,7 +69,7 @@ pub async fn create(
.await .await
.map_err(reject_anyhow)?; .map_err(reject_anyhow)?;
let url = saved_owner.url(&env.api_url).map_err(reject_anyhow)?; let url = saved_owner.url(&env.api_url).map_err(reject_anyhow)?;
let reply = json(&saved_owner); let reply = JsonWithETag::from_serializable(&saved_owner).map_err(reject_anyhow)?;
let reply = with_header(reply, "Location", url.as_str()); let reply = with_header(reply, "Location", url.as_str());
let reply = with_status(reply, StatusCode::CREATED); let reply = with_status(reply, StatusCode::CREATED);
env.caches.list_owners.clear().await; env.caches.list_owners.clear().await;
@ -95,7 +95,7 @@ pub async fn update(
.await .await
.map_err(reject_anyhow)?; .map_err(reject_anyhow)?;
let url = updated_owner.url(&env.api_url).map_err(reject_anyhow)?; let url = updated_owner.url(&env.api_url).map_err(reject_anyhow)?;
let reply = json(&updated_owner); let reply = JsonWithETag::from_serializable(&updated_owner).map_err(reject_anyhow)?;
let reply = with_header(reply, "Location", url.as_str()); let reply = with_header(reply, "Location", url.as_str());
let reply = with_status(reply, StatusCode::CREATED); let reply = with_status(reply, StatusCode::CREATED);
env.caches.owner.delete_response(id).await; env.caches.owner.delete_response(id).await;

View File

@ -2,7 +2,7 @@ use anyhow::Result;
use http::StatusCode; use http::StatusCode;
use sqlx::types::Json; use sqlx::types::Json;
use uuid::Uuid; use uuid::Uuid;
use warp::reply::{json, with_header, with_status}; use warp::reply::{with_header, with_status};
use warp::{Rejection, Reply}; use warp::{Rejection, Reply};
use crate::models::{InteriorRefList, ListParams, MerchandiseList, Shop}; use crate::models::{InteriorRefList, ListParams, MerchandiseList, Shop};
@ -87,7 +87,7 @@ pub async fn create(
} }
let url = saved_shop.url(&env.api_url).map_err(reject_anyhow)?; let url = saved_shop.url(&env.api_url).map_err(reject_anyhow)?;
let reply = json(&saved_shop); let reply = JsonWithETag::from_serializable(&saved_shop).map_err(reject_anyhow)?;
let reply = with_header(reply, "Location", url.as_str()); let reply = with_header(reply, "Location", url.as_str());
let reply = with_status(reply, StatusCode::CREATED); let reply = with_status(reply, StatusCode::CREATED);
env.caches.list_shops.clear().await; env.caches.list_shops.clear().await;
@ -119,7 +119,7 @@ pub async fn update(
.await .await
.map_err(reject_anyhow)?; .map_err(reject_anyhow)?;
let url = updated_shop.url(&env.api_url).map_err(reject_anyhow)?; let url = updated_shop.url(&env.api_url).map_err(reject_anyhow)?;
let reply = json(&updated_shop); let reply = JsonWithETag::from_serializable(&updated_shop).map_err(reject_anyhow)?;
let reply = with_header(reply, "Location", url.as_str()); let reply = with_header(reply, "Location", url.as_str());
let reply = with_status(reply, StatusCode::CREATED); let reply = with_status(reply, StatusCode::CREATED);
env.caches.shop.delete_response(id).await; env.caches.shop.delete_response(id).await;

View File

@ -1,7 +1,7 @@
use anyhow::{anyhow, Result}; use anyhow::{anyhow, Result};
use http::StatusCode; use http::StatusCode;
use uuid::Uuid; use uuid::Uuid;
use warp::reply::{json, with_header, with_status}; use warp::reply::{with_header, with_status};
use warp::{Rejection, Reply}; use warp::{Rejection, Reply};
use crate::models::{ListParams, MerchandiseList, Transaction}; use crate::models::{ListParams, MerchandiseList, Transaction};
@ -101,7 +101,7 @@ pub async fn create(
.await .await
.map_err(|error| reject_anyhow(anyhow!(error)))?; .map_err(|error| reject_anyhow(anyhow!(error)))?;
let url = saved_transaction.url(&env.api_url).map_err(reject_anyhow)?; let url = saved_transaction.url(&env.api_url).map_err(reject_anyhow)?;
let reply = json(&saved_transaction); let reply = JsonWithETag::from_serializable(&saved_transaction).map_err(reject_anyhow)?;
let reply = with_header(reply, "Location", url.as_str()); let reply = with_header(reply, "Location", url.as_str());
let reply = with_status(reply, StatusCode::CREATED); let reply = with_status(reply, StatusCode::CREATED);
// TODO: will this make these caches effectively useless? // TODO: will this make these caches effectively useless?