Conditionally reply with bincode or json

In the process of converting all endpoints. Just interior_ref_list and merchandise_list done so far.
This commit is contained in:
Tyler Hallada 2020-11-08 03:09:40 -05:00
parent 2f69c86645
commit 6ac4b03a0a
8 changed files with 532 additions and 287 deletions

View File

@ -17,17 +17,29 @@ lazy_static! {
pub struct Caches {
pub owner_ids_by_api_key: Cache<Uuid, i32>,
pub shop: Cache<i32, CachedResponse>,
pub shop_bin: Cache<i32, CachedResponse>,
pub owner: Cache<i32, CachedResponse>,
pub owner_bin: Cache<i32, CachedResponse>,
pub interior_ref_list: Cache<i32, CachedResponse>,
pub interior_ref_list_bin: Cache<i32, CachedResponse>,
pub merchandise_list: Cache<i32, CachedResponse>,
pub merchandise_list_bin: Cache<i32, CachedResponse>,
pub transaction: Cache<i32, CachedResponse>,
pub transaction_bin: Cache<i32, CachedResponse>,
pub list_shops: Cache<ListParams, CachedResponse>,
pub list_shops_bin: Cache<ListParams, CachedResponse>,
pub list_owners: Cache<ListParams, CachedResponse>,
pub list_owners_bin: Cache<ListParams, CachedResponse>,
pub list_interior_ref_lists: Cache<ListParams, CachedResponse>,
pub list_interior_ref_lists_bin: Cache<ListParams, CachedResponse>,
pub list_merchandise_lists: Cache<ListParams, CachedResponse>,
pub list_merchandise_lists_bin: Cache<ListParams, CachedResponse>,
pub list_transactions: Cache<ListParams, CachedResponse>,
pub list_transactions_bin: Cache<ListParams, CachedResponse>,
pub list_transactions_by_shop_id: Cache<(i32, ListParams), CachedResponse>,
pub list_transactions_by_shop_id_bin: Cache<(i32, ListParams), CachedResponse>,
pub interior_ref_list_by_shop_id: Cache<i32, CachedResponse>,
pub interior_ref_list_by_shop_id_bin: Cache<i32, CachedResponse>,
pub merchandise_list_by_shop_id: Cache<i32, CachedResponse>,
pub merchandise_list_by_shop_id_bin: Cache<i32, CachedResponse>,
}
@ -37,17 +49,29 @@ impl Caches {
Caches {
owner_ids_by_api_key: Cache::new("owner_ids_by_api_key", 100).log_keys(false),
shop: Cache::new("shop", 100),
shop_bin: Cache::new("shop_bin", 100),
owner: Cache::new("owner", 100),
owner_bin: Cache::new("owner_bin", 100),
interior_ref_list: Cache::new("interior_ref_list", 100),
interior_ref_list_bin: Cache::new("interior_ref_list_bin", 100),
merchandise_list: Cache::new("merchandise_list", 100),
merchandise_list_bin: Cache::new("merchandise_list_bin", 100),
transaction: Cache::new("transaction", 100),
transaction_bin: Cache::new("transaction_bin", 100),
list_shops: Cache::new("list_shops", 100),
list_shops_bin: Cache::new("list_shops_bin", 100),
list_owners: Cache::new("list_owners", 100),
list_owners_bin: Cache::new("list_owners_bin", 100),
list_interior_ref_lists: Cache::new("list_interior_ref_lists", 100),
list_interior_ref_lists_bin: Cache::new("list_interior_ref_lists_bin", 100),
list_merchandise_lists: Cache::new("list_merchandise_lists", 100),
list_merchandise_lists_bin: Cache::new("list_merchandise_lists_bin", 100),
list_transactions: Cache::new("list_transaction", 100),
list_transactions_bin: Cache::new("list_transaction_bin", 100),
list_transactions_by_shop_id: Cache::new("list_transaction_by_shop_id", 100),
list_transactions_by_shop_id_bin: Cache::new("list_transaction_by_shop_id_bin", 100),
interior_ref_list_by_shop_id: Cache::new("interior_ref_list_by_shop_id", 100),
interior_ref_list_by_shop_id_bin: Cache::new("interior_ref_list_by_shop_id_bin", 100),
merchandise_list_by_shop_id: Cache::new("merchandise_list_by_shop_id", 100),
merchandise_list_by_shop_id_bin: Cache::new("merchandise_list_by_shop_id_bin", 100),
}

View File

@ -1,172 +1,278 @@
use anyhow::Result;
use http::StatusCode;
use mime::Mime;
use uuid::Uuid;
use warp::reply::{with_header, with_status};
use warp::{Rejection, Reply};
use crate::caches::CACHES;
use crate::caches::{Cache, CachedResponse, CACHES};
use crate::models::{InteriorRefList, ListParams};
use crate::problem::reject_anyhow;
use crate::Environment;
use super::{authenticate, check_etag, JsonWithETag};
use super::{authenticate, check_etag, AcceptHeader, Bincode, DataReply, ETagReply, Json};
pub async fn get(id: i32, etag: Option<String>, env: Environment) -> Result<impl Reply, Rejection> {
let response = CACHES
.interior_ref_list
.get_response(id, || async {
let interior_ref_list = InteriorRefList::get(&env.db, id).await?;
let reply = JsonWithETag::from_serializable(&interior_ref_list)?;
let reply = with_status(reply, StatusCode::OK);
Ok(reply)
})
.await?;
Ok(check_etag(etag, response))
pub async fn get(
id: i32,
etag: Option<String>,
accept: Option<AcceptHeader>,
env: Environment,
) -> Result<impl Reply, Rejection> {
async fn get<T: DataReply>(
id: i32,
etag: Option<String>,
env: Environment,
cache: &'static Cache<i32, CachedResponse>,
) -> Result<Box<dyn Reply>, Rejection> {
let response = cache
.get_response(id, || async {
let interior_ref_list = InteriorRefList::get(&env.db, id).await?;
let reply = T::from_serializable(&interior_ref_list)?;
let reply = with_status(reply, StatusCode::OK);
Ok(reply)
})
.await?;
Ok(Box::new(check_etag(etag, response)))
}
match accept {
Some(accept) if accept.accepts_bincode() => {
get::<ETagReply<Bincode>>(id, etag, env, &CACHES.interior_ref_list_bin).await
}
_ => get::<ETagReply<Json>>(id, etag, env, &CACHES.interior_ref_list).await,
}
}
pub async fn get_by_shop_id(
shop_id: i32,
etag: Option<String>,
accept: Option<AcceptHeader>,
env: Environment,
) -> Result<impl Reply, Rejection> {
let response = CACHES
.interior_ref_list_by_shop_id
.get_response(shop_id, || async {
let interior_ref_list = InteriorRefList::get_by_shop_id(&env.db, shop_id).await?;
let reply = JsonWithETag::from_serializable(&interior_ref_list)?;
let reply = with_status(reply, StatusCode::OK);
Ok(reply)
})
.await?;
Ok(check_etag(etag, response))
async fn get<T: DataReply>(
shop_id: i32,
etag: Option<String>,
env: Environment,
cache: &'static Cache<i32, CachedResponse>,
) -> Result<Box<dyn Reply>, Rejection> {
let response = cache
.get_response(shop_id, || async {
let interior_ref_list = InteriorRefList::get_by_shop_id(&env.db, shop_id).await?;
let reply = T::from_serializable(&interior_ref_list)?;
let reply = with_status(reply, StatusCode::OK);
Ok(reply)
})
.await?;
Ok(Box::new(check_etag(etag, response)))
}
match accept {
Some(accept) if accept.accepts_bincode() => {
get::<ETagReply<Bincode>>(shop_id, etag, env, &CACHES.interior_ref_list_by_shop_id_bin)
.await
}
_ => get::<ETagReply<Json>>(shop_id, etag, env, &CACHES.interior_ref_list_by_shop_id).await,
}
}
pub async fn list(
list_params: ListParams,
etag: Option<String>,
accept: Option<AcceptHeader>,
env: Environment,
) -> Result<impl Reply, Rejection> {
let response = CACHES
.list_interior_ref_lists
.get_response(list_params.clone(), || async {
let interior_ref_lists = InteriorRefList::list(&env.db, &list_params).await?;
let reply = JsonWithETag::from_serializable(&interior_ref_lists)?;
let reply = with_status(reply, StatusCode::OK);
Ok(reply)
})
.await?;
Ok(check_etag(etag, response))
async fn get<T: DataReply>(
list_params: ListParams,
etag: Option<String>,
env: Environment,
cache: &'static Cache<ListParams, CachedResponse>,
) -> Result<Box<dyn Reply>, Rejection> {
let response = cache
.get_response(list_params.clone(), || async {
let interior_ref_lists = InteriorRefList::list(&env.db, &list_params).await?;
let reply = T::from_serializable(&interior_ref_lists)?;
let reply = with_status(reply, StatusCode::OK);
Ok(reply)
})
.await?;
Ok(Box::new(check_etag(etag, response)))
}
match accept {
Some(accept) if accept.accepts_bincode() => {
get::<ETagReply<Bincode>>(list_params, etag, env, &CACHES.list_interior_ref_lists_bin)
.await
}
_ => get::<ETagReply<Json>>(list_params, etag, env, &CACHES.list_interior_ref_lists).await,
}
}
pub async fn create(
interior_ref_list: InteriorRefList,
api_key: Option<Uuid>,
content_type: Option<Mime>,
env: Environment,
) -> Result<impl Reply, Rejection> {
let owner_id = authenticate(&env, api_key).await.map_err(reject_anyhow)?;
let ref_list_with_owner_id = InteriorRefList {
owner_id: Some(owner_id),
..interior_ref_list
};
let saved_interior_ref_list = ref_list_with_owner_id
.create(&env.db)
.await
.map_err(reject_anyhow)?;
let url = saved_interior_ref_list
.url(&env.api_url)
.map_err(reject_anyhow)?;
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_status(reply, StatusCode::CREATED);
tokio::spawn(async move {
CACHES.list_interior_ref_lists.clear().await;
CACHES
.interior_ref_list_by_shop_id
.delete_response(saved_interior_ref_list.shop_id)
.await;
});
Ok(reply)
async fn create<'a, T: DataReply + 'a>(
interior_ref_list: InteriorRefList,
api_key: Option<Uuid>,
env: Environment,
) -> Result<Box<dyn Reply + 'a>, Rejection> {
let owner_id = authenticate(&env, api_key).await.map_err(reject_anyhow)?;
let ref_list_with_owner_id = InteriorRefList {
owner_id: Some(owner_id),
..interior_ref_list
};
let saved_interior_ref_list = ref_list_with_owner_id
.create(&env.db)
.await
.map_err(reject_anyhow)?;
let url = saved_interior_ref_list
.url(&env.api_url)
.map_err(reject_anyhow)?;
let reply = ETagReply::<Json>::from_serializable(&saved_interior_ref_list)
.map_err(reject_anyhow)?;
let reply = with_header(reply, "Location", url.as_str());
let reply = with_status(reply, StatusCode::CREATED);
tokio::spawn(async move {
CACHES.list_interior_ref_lists.clear().await;
CACHES.list_interior_ref_lists_bin.clear().await;
CACHES
.interior_ref_list_by_shop_id
.delete_response(saved_interior_ref_list.shop_id)
.await;
CACHES
.interior_ref_list_by_shop_id_bin
.delete_response(saved_interior_ref_list.shop_id)
.await;
});
Ok(Box::new(reply))
}
match content_type {
Some(content_type) if content_type == mime::APPLICATION_OCTET_STREAM => {
create::<ETagReply<Bincode>>(interior_ref_list, api_key, env).await
}
_ => create::<ETagReply<Json>>(interior_ref_list, api_key, env).await,
}
}
pub async fn update(
id: i32,
interior_ref_list: InteriorRefList,
api_key: Option<Uuid>,
content_type: Option<Mime>,
env: Environment,
) -> Result<impl Reply, Rejection> {
let owner_id = authenticate(&env, api_key).await.map_err(reject_anyhow)?;
let interior_ref_list_with_id_and_owner_id = if interior_ref_list.owner_id.is_some() {
InteriorRefList {
id: Some(id),
..interior_ref_list
async fn update<'a, T: DataReply + 'a>(
id: i32,
interior_ref_list: InteriorRefList,
api_key: Option<Uuid>,
env: Environment,
) -> Result<Box<dyn Reply + 'a>, Rejection> {
let owner_id = authenticate(&env, api_key).await.map_err(reject_anyhow)?;
let interior_ref_list_with_id_and_owner_id = if interior_ref_list.owner_id.is_some() {
InteriorRefList {
id: Some(id),
..interior_ref_list
}
} else {
InteriorRefList {
id: Some(id),
owner_id: Some(owner_id),
..interior_ref_list
}
};
let updated_interior_ref_list = interior_ref_list_with_id_and_owner_id
.update(&env.db, owner_id, id)
.await
.map_err(reject_anyhow)?;
let url = updated_interior_ref_list
.url(&env.api_url)
.map_err(reject_anyhow)?;
let reply = T::from_serializable(&updated_interior_ref_list).map_err(reject_anyhow)?;
let reply = with_header(reply, "Location", url.as_str());
let reply = with_status(reply, StatusCode::CREATED);
tokio::spawn(async move {
CACHES.interior_ref_list.delete_response(id).await;
CACHES.interior_ref_list_bin.delete_response(id).await;
CACHES
.interior_ref_list_by_shop_id
.delete_response(updated_interior_ref_list.shop_id)
.await;
CACHES
.interior_ref_list_by_shop_id_bin
.delete_response(updated_interior_ref_list.shop_id)
.await;
CACHES.list_interior_ref_lists.clear().await;
CACHES.list_interior_ref_lists_bin.clear().await;
});
Ok(Box::new(reply))
}
match content_type {
Some(content_type) if content_type == mime::APPLICATION_OCTET_STREAM => {
update::<ETagReply<Bincode>>(id, interior_ref_list, api_key, env).await
}
} else {
InteriorRefList {
id: Some(id),
owner_id: Some(owner_id),
..interior_ref_list
}
};
let updated_interior_ref_list = interior_ref_list_with_id_and_owner_id
.update(&env.db, owner_id, id)
.await
.map_err(reject_anyhow)?;
let url = updated_interior_ref_list
.url(&env.api_url)
.map_err(reject_anyhow)?;
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_status(reply, StatusCode::CREATED);
tokio::spawn(async move {
CACHES.interior_ref_list.delete_response(id).await;
CACHES
.interior_ref_list_by_shop_id
.delete_response(updated_interior_ref_list.shop_id)
.await;
CACHES.list_interior_ref_lists.clear().await;
});
Ok(reply)
_ => update::<ETagReply<Json>>(id, interior_ref_list, api_key, env).await,
}
}
pub async fn update_by_shop_id(
shop_id: i32,
interior_ref_list: InteriorRefList,
api_key: Option<Uuid>,
content_type: Option<Mime>,
env: Environment,
) -> Result<impl Reply, Rejection> {
let owner_id = authenticate(&env, api_key).await.map_err(reject_anyhow)?;
let interior_ref_list_with_owner_id = InteriorRefList {
owner_id: Some(owner_id),
..interior_ref_list
};
let updated_interior_ref_list = interior_ref_list_with_owner_id
.update_by_shop_id(&env.db, owner_id, shop_id)
.await
.map_err(reject_anyhow)?;
let url = updated_interior_ref_list
.url(&env.api_url)
.map_err(reject_anyhow)?;
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_status(reply, StatusCode::CREATED);
tokio::spawn(async move {
CACHES
.interior_ref_list
.delete_response(
updated_interior_ref_list
.id
.expect("saved interior_ref_list has no id"),
)
.await;
CACHES
.interior_ref_list_by_shop_id
.delete_response(updated_interior_ref_list.shop_id)
.await;
CACHES.list_interior_ref_lists.clear().await;
});
Ok(reply)
async fn update<'a, T: DataReply + 'a>(
shop_id: i32,
interior_ref_list: InteriorRefList,
api_key: Option<Uuid>,
env: Environment,
) -> Result<Box<dyn Reply + 'a>, Rejection> {
let owner_id = authenticate(&env, api_key).await.map_err(reject_anyhow)?;
let interior_ref_list_with_owner_id = InteriorRefList {
owner_id: Some(owner_id),
..interior_ref_list
};
let updated_interior_ref_list = interior_ref_list_with_owner_id
.update_by_shop_id(&env.db, owner_id, shop_id)
.await
.map_err(reject_anyhow)?;
let url = updated_interior_ref_list
.url(&env.api_url)
.map_err(reject_anyhow)?;
let reply = T::from_serializable(&updated_interior_ref_list).map_err(reject_anyhow)?;
let reply = with_header(reply, "Location", url.as_str());
let reply = with_status(reply, StatusCode::CREATED);
tokio::spawn(async move {
let id = updated_interior_ref_list
.id
.expect("saved interior_ref_list has no id");
CACHES.interior_ref_list.delete_response(id).await;
CACHES.interior_ref_list_bin.delete_response(id).await;
CACHES
.interior_ref_list_by_shop_id
.delete_response(updated_interior_ref_list.shop_id)
.await;
CACHES
.interior_ref_list_by_shop_id_bin
.delete_response(updated_interior_ref_list.shop_id)
.await;
CACHES.list_interior_ref_lists.clear().await;
CACHES.list_interior_ref_lists_bin.clear().await;
});
Ok(Box::new(reply))
}
match content_type {
Some(content_type) if content_type == mime::APPLICATION_OCTET_STREAM => {
update::<ETagReply<Bincode>>(shop_id, interior_ref_list, api_key, env).await
}
_ => update::<ETagReply<Json>>(shop_id, interior_ref_list, api_key, env).await,
}
}
pub async fn delete(

View File

@ -1,27 +1,46 @@
use anyhow::Result;
use http::StatusCode;
use mime::Mime;
use uuid::Uuid;
use warp::reply::{with_header, with_status};
use warp::{Rejection, Reply};
use crate::caches::CACHES;
use crate::caches::{Cache, CachedResponse, CACHES};
use crate::models::{ListParams, MerchandiseList};
use crate::problem::reject_anyhow;
use crate::Environment;
use super::{authenticate, check_etag, AcceptHeader, BincodeWithETag, JsonWithETag};
use super::{authenticate, check_etag, AcceptHeader, Bincode, DataReply, ETagReply, Json};
pub async fn get(id: i32, etag: Option<String>, env: Environment) -> Result<impl Reply, Rejection> {
let response = CACHES
.merchandise_list
.get_response(id, || async {
let merchandise_list = MerchandiseList::get(&env.db, id).await?;
let reply = JsonWithETag::from_serializable(&merchandise_list)?;
let reply = with_status(reply, StatusCode::OK);
Ok(reply)
})
.await?;
Ok(check_etag(etag, response))
pub async fn get(
id: i32,
etag: Option<String>,
accept: Option<AcceptHeader>,
env: Environment,
) -> Result<impl Reply, Rejection> {
async fn get<T: DataReply>(
id: i32,
etag: Option<String>,
env: Environment,
cache: &'static Cache<i32, CachedResponse>,
) -> Result<Box<dyn Reply>, Rejection> {
let response = cache
.get_response(id, || async {
let merchandise_list = MerchandiseList::get(&env.db, id).await?;
let reply = T::from_serializable(&merchandise_list)?;
let reply = with_status(reply, StatusCode::OK);
Ok(reply)
})
.await?;
Ok(Box::new(check_etag(etag, response)))
}
match accept {
Some(accept) if accept.accepts_bincode() => {
get::<ETagReply<Bincode>>(id, etag, env, &CACHES.merchandise_list_bin).await
}
_ => get::<ETagReply<Json>>(id, etag, env, &CACHES.merchandise_list).await,
}
}
pub async fn get_by_shop_id(
@ -30,161 +49,229 @@ pub async fn get_by_shop_id(
accept: Option<AcceptHeader>,
env: Environment,
) -> Result<impl Reply, Rejection> {
let response = match accept {
async fn get<T: DataReply>(
shop_id: i32,
etag: Option<String>,
env: Environment,
cache: &'static Cache<i32, CachedResponse>,
) -> Result<Box<dyn Reply>, Rejection> {
let response = cache
.get_response(shop_id, || async {
let merchandise_list = MerchandiseList::get_by_shop_id(&env.db, shop_id).await?;
let reply = T::from_serializable(&merchandise_list)?;
let reply = with_status(reply, StatusCode::OK);
Ok(reply)
})
.await?;
Ok(Box::new(check_etag(etag, response)))
}
match accept {
Some(accept) if accept.accepts_bincode() => {
CACHES
.merchandise_list_by_shop_id_bin
.get_response(shop_id, || async {
let merchandise_list =
MerchandiseList::get_by_shop_id(&env.db, shop_id).await?;
let reply = BincodeWithETag::from_serializable(&merchandise_list)?;
let reply = with_status(reply, StatusCode::OK);
Ok(reply)
})
.await?
get::<ETagReply<Bincode>>(shop_id, etag, env, &CACHES.merchandise_list_by_shop_id_bin)
.await
}
_ => {
CACHES
.merchandise_list_by_shop_id
.get_response(shop_id, || async {
let merchandise_list =
MerchandiseList::get_by_shop_id(&env.db, shop_id).await?;
let reply = JsonWithETag::from_serializable(&merchandise_list)?;
let reply = with_status(reply, StatusCode::OK);
Ok(reply)
})
.await?
}
};
Ok(check_etag(etag, response))
_ => get::<ETagReply<Json>>(shop_id, etag, env, &CACHES.merchandise_list_by_shop_id).await,
}
}
pub async fn list(
list_params: ListParams,
etag: Option<String>,
accept: Option<AcceptHeader>,
env: Environment,
) -> Result<impl Reply, Rejection> {
let response = CACHES
.list_merchandise_lists
.get_response(list_params.clone(), || async {
let merchandise_lists = MerchandiseList::list(&env.db, &list_params).await?;
let reply = JsonWithETag::from_serializable(&merchandise_lists)?;
let reply = with_status(reply, StatusCode::OK);
Ok(reply)
})
.await?;
Ok(check_etag(etag, response))
async fn get<T: DataReply>(
list_params: ListParams,
etag: Option<String>,
env: Environment,
cache: &'static Cache<ListParams, CachedResponse>,
) -> Result<Box<dyn Reply>, Rejection> {
let response = cache
.get_response(list_params.clone(), || async {
let merchandise_lists = MerchandiseList::list(&env.db, &list_params).await?;
let reply = T::from_serializable(&merchandise_lists)?;
let reply = with_status(reply, StatusCode::OK);
Ok(reply)
})
.await?;
Ok(Box::new(check_etag(etag, response)))
}
match accept {
Some(accept) if accept.accepts_bincode() => {
get::<ETagReply<Bincode>>(list_params, etag, env, &CACHES.list_merchandise_lists_bin)
.await
}
_ => get::<ETagReply<Json>>(list_params, etag, env, &CACHES.list_merchandise_lists).await,
}
}
pub async fn create(
merchandise_list: MerchandiseList,
api_key: Option<Uuid>,
content_type: Option<Mime>,
env: Environment,
) -> Result<impl Reply, Rejection> {
let owner_id = authenticate(&env, api_key).await.map_err(reject_anyhow)?;
let ref_list_with_owner_id = MerchandiseList {
owner_id: Some(owner_id),
..merchandise_list
};
let saved_merchandise_list = ref_list_with_owner_id
.create(&env.db)
.await
.map_err(reject_anyhow)?;
let url = saved_merchandise_list
.url(&env.api_url)
.map_err(reject_anyhow)?;
let reply = JsonWithETag::from_serializable(&saved_merchandise_list).map_err(reject_anyhow)?;
let reply = with_header(reply, "Location", url.as_str());
let reply = with_status(reply, StatusCode::CREATED);
tokio::spawn(async move {
CACHES.list_merchandise_lists.clear().await;
CACHES
.merchandise_list_by_shop_id
.delete_response(saved_merchandise_list.shop_id)
.await;
});
Ok(reply)
async fn create<'a, T: DataReply + 'a>(
merchandise_list: MerchandiseList,
api_key: Option<Uuid>,
env: Environment,
) -> Result<Box<dyn Reply + 'a>, Rejection> {
let owner_id = authenticate(&env, api_key).await.map_err(reject_anyhow)?;
let ref_list_with_owner_id = MerchandiseList {
owner_id: Some(owner_id),
..merchandise_list
};
let saved_merchandise_list = ref_list_with_owner_id
.create(&env.db)
.await
.map_err(reject_anyhow)?;
let url = saved_merchandise_list
.url(&env.api_url)
.map_err(reject_anyhow)?;
let reply = T::from_serializable(&saved_merchandise_list).map_err(reject_anyhow)?;
let reply = with_header(reply, "Location", url.as_str());
let reply = with_status(reply, StatusCode::CREATED);
tokio::spawn(async move {
CACHES.list_merchandise_lists.clear().await;
CACHES.list_merchandise_lists_bin.clear().await;
CACHES
.merchandise_list_by_shop_id
.delete_response(saved_merchandise_list.shop_id)
.await;
CACHES
.merchandise_list_by_shop_id_bin
.delete_response(saved_merchandise_list.shop_id)
.await;
});
Ok(Box::new(reply))
}
match content_type {
Some(content_type) if content_type == mime::APPLICATION_OCTET_STREAM => {
create::<ETagReply<Bincode>>(merchandise_list, api_key, env).await
}
_ => create::<ETagReply<Json>>(merchandise_list, api_key, env).await,
}
}
pub async fn update(
id: i32,
merchandise_list: MerchandiseList,
api_key: Option<Uuid>,
content_type: Option<Mime>,
env: Environment,
) -> Result<impl Reply, Rejection> {
let owner_id = authenticate(&env, api_key).await.map_err(reject_anyhow)?;
let merchandise_list_with_id_and_owner_id = if merchandise_list.owner_id.is_some() {
MerchandiseList {
id: Some(id),
..merchandise_list
async fn update<'a, T: DataReply + 'a>(
id: i32,
merchandise_list: MerchandiseList,
api_key: Option<Uuid>,
env: Environment,
) -> Result<Box<dyn Reply + 'a>, Rejection> {
let owner_id = authenticate(&env, api_key).await.map_err(reject_anyhow)?;
let merchandise_list_with_id_and_owner_id = if merchandise_list.owner_id.is_some() {
MerchandiseList {
id: Some(id),
..merchandise_list
}
} else {
MerchandiseList {
id: Some(id),
owner_id: Some(owner_id),
..merchandise_list
}
};
let updated_merchandise_list = merchandise_list_with_id_and_owner_id
.update(&env.db, owner_id, id)
.await
.map_err(reject_anyhow)?;
let url = updated_merchandise_list
.url(&env.api_url)
.map_err(reject_anyhow)?;
let reply = T::from_serializable(&updated_merchandise_list).map_err(reject_anyhow)?;
let reply = with_header(reply, "Location", url.as_str());
let reply = with_status(reply, StatusCode::CREATED);
tokio::spawn(async move {
CACHES.merchandise_list.delete_response(id).await;
CACHES.merchandise_list_bin.delete_response(id).await;
CACHES
.merchandise_list_by_shop_id
.delete_response(updated_merchandise_list.shop_id)
.await;
CACHES
.merchandise_list_by_shop_id_bin
.delete_response(updated_merchandise_list.shop_id)
.await;
CACHES.list_merchandise_lists.clear().await;
CACHES.list_merchandise_lists_bin.clear().await;
});
Ok(Box::new(reply))
}
match content_type {
Some(content_type) if content_type == mime::APPLICATION_OCTET_STREAM => {
update::<ETagReply<Bincode>>(id, merchandise_list, api_key, env).await
}
} else {
MerchandiseList {
id: Some(id),
owner_id: Some(owner_id),
..merchandise_list
}
};
let updated_merchandise_list = merchandise_list_with_id_and_owner_id
.update(&env.db, owner_id, id)
.await
.map_err(reject_anyhow)?;
let url = updated_merchandise_list
.url(&env.api_url)
.map_err(reject_anyhow)?;
let reply =
JsonWithETag::from_serializable(&updated_merchandise_list).map_err(reject_anyhow)?;
let reply = with_header(reply, "Location", url.as_str());
let reply = with_status(reply, StatusCode::CREATED);
tokio::spawn(async move {
CACHES.merchandise_list.delete_response(id).await;
CACHES
.merchandise_list_by_shop_id
.delete_response(updated_merchandise_list.shop_id)
.await;
CACHES.list_merchandise_lists.clear().await;
});
Ok(reply)
_ => update::<ETagReply<Json>>(id, merchandise_list, api_key, env).await,
}
}
pub async fn update_by_shop_id(
shop_id: i32,
merchandise_list: MerchandiseList,
api_key: Option<Uuid>,
content_type: Option<Mime>,
env: Environment,
) -> Result<impl Reply, Rejection> {
let owner_id = authenticate(&env, api_key).await.map_err(reject_anyhow)?;
let merchandise_list_with_owner_id = MerchandiseList {
owner_id: Some(owner_id),
..merchandise_list
};
let updated_merchandise_list = merchandise_list_with_owner_id
.update_by_shop_id(&env.db, owner_id, shop_id)
.await
.map_err(reject_anyhow)?;
let url = updated_merchandise_list
.url(&env.api_url)
.map_err(reject_anyhow)?;
let reply =
JsonWithETag::from_serializable(&updated_merchandise_list).map_err(reject_anyhow)?;
let reply = with_header(reply, "Location", url.as_str());
let reply = with_status(reply, StatusCode::CREATED);
tokio::spawn(async move {
CACHES
.merchandise_list
.delete_response(
updated_merchandise_list
.id
.expect("saved merchandise_list has no id"),
)
.await;
CACHES
.merchandise_list_by_shop_id
.delete_response(updated_merchandise_list.shop_id)
.await;
CACHES.list_merchandise_lists.clear().await;
});
Ok(reply)
async fn update<'a, T: DataReply + 'a>(
shop_id: i32,
merchandise_list: MerchandiseList,
api_key: Option<Uuid>,
env: Environment,
) -> Result<Box<dyn Reply + 'a>, Rejection> {
let owner_id = authenticate(&env, api_key).await.map_err(reject_anyhow)?;
let merchandise_list_with_owner_id = MerchandiseList {
owner_id: Some(owner_id),
..merchandise_list
};
let updated_merchandise_list = merchandise_list_with_owner_id
.update_by_shop_id(&env.db, owner_id, shop_id)
.await
.map_err(reject_anyhow)?;
let url = updated_merchandise_list
.url(&env.api_url)
.map_err(reject_anyhow)?;
let reply = ETagReply::<Json>::from_serializable(&updated_merchandise_list)
.map_err(reject_anyhow)?;
let reply = with_header(reply, "Location", url.as_str());
let reply = with_status(reply, StatusCode::CREATED);
tokio::spawn(async move {
let id = updated_merchandise_list
.id
.expect("saved merchandise_list has no id");
CACHES.merchandise_list.delete_response(id).await;
CACHES.merchandise_list_bin.delete_response(id).await;
CACHES
.merchandise_list_by_shop_id
.delete_response(updated_merchandise_list.shop_id)
.await;
CACHES
.merchandise_list_by_shop_id_bin
.delete_response(updated_merchandise_list.shop_id)
.await;
CACHES.list_merchandise_lists.clear().await;
CACHES.list_merchandise_lists_bin.clear().await;
});
Ok(Box::new(reply))
}
match content_type {
Some(content_type) if content_type == mime::APPLICATION_OCTET_STREAM => {
update::<ETagReply<Bincode>>(shop_id, merchandise_list, api_key, env).await
}
_ => update::<ETagReply<Json>>(shop_id, merchandise_list, api_key, env).await,
}
}
pub async fn delete(
@ -201,11 +288,17 @@ pub async fn delete(
.map_err(reject_anyhow)?;
tokio::spawn(async move {
CACHES.merchandise_list.delete_response(id).await;
CACHES.merchandise_list_bin.delete_response(id).await;
CACHES
.merchandise_list_by_shop_id
.delete_response(merchandise_list.shop_id)
.await;
CACHES
.merchandise_list_by_shop_id_bin
.delete_response(merchandise_list.shop_id)
.await;
CACHES.list_merchandise_lists.clear().await;
CACHES.list_merchandise_lists_bin.clear().await;
});
Ok(StatusCode::NO_CONTENT)
}

View File

@ -1,3 +1,4 @@
use std::marker::PhantomData;
use std::str::FromStr;
use anyhow::{anyhow, Error, Result};
@ -50,12 +51,20 @@ pub async fn authenticate(env: &Environment, api_key: Option<Uuid>) -> Result<i3
// Similar to `warp::reply::Json`, but stores hash of body content for the ETag header created in `into_response`.
// Also, it does not store a serialize `Result`. Instead it returns the error to the caller immediately in `from_serializable`.
// It's purpose is to avoid serializing the body content twice and to encapsulate ETag logic in one place.
pub struct JsonWithETag {
pub struct ETagReply<T> {
body: Vec<u8>,
etag: String,
content_type: PhantomData<T>,
}
impl Reply for JsonWithETag {
pub trait DataReply: Reply + Sized {
fn from_serializable<T: Serialize>(val: &T) -> Result<Self>;
}
pub struct Json {}
pub struct Bincode {}
impl Reply for ETagReply<Json> {
fn into_response(self) -> Response {
let mut res = Response::new(self.body.into());
res.headers_mut()
@ -70,8 +79,8 @@ impl Reply for JsonWithETag {
}
}
impl JsonWithETag {
pub fn from_serializable<T: Serialize>(val: &T) -> Result<Self> {
impl DataReply for ETagReply<Json> {
fn from_serializable<T: Serialize>(val: &T) -> Result<Self> {
let bytes = serde_json::to_vec(val).map_err(|err| {
error!("Failed to serialize database value to JSON: {}", err);
anyhow!(HttpApiProblem::with_title_and_type_from_status(
@ -83,16 +92,15 @@ impl JsonWithETag {
)))
})?;
let etag = format!("{:x}", hash(&bytes));
Ok(Self { body: bytes, etag })
Ok(Self {
body: bytes,
etag,
content_type: PhantomData,
})
}
}
pub struct BincodeWithETag {
body: Vec<u8>,
etag: String,
}
impl Reply for BincodeWithETag {
impl Reply for ETagReply<Bincode> {
fn into_response(self) -> Response {
let mut res = Response::new(self.body.into());
res.headers_mut().insert(
@ -109,8 +117,8 @@ impl Reply for BincodeWithETag {
}
}
impl BincodeWithETag {
pub fn from_serializable<T: Serialize>(val: &T) -> Result<Self> {
impl DataReply for ETagReply<Bincode> {
fn from_serializable<T: Serialize>(val: &T) -> Result<Self> {
let bytes = bincode::serialize(val).map_err(|err| {
error!("Failed to serialize database value to bincode: {}", err);
anyhow!(HttpApiProblem::with_title_and_type_from_status(
@ -122,7 +130,11 @@ impl BincodeWithETag {
)))
})?;
let etag = format!("{:x}", hash(&bytes));
Ok(Self { body: bytes, etag })
Ok(Self {
body: bytes,
etag,
content_type: PhantomData,
})
}
}

View File

@ -11,14 +11,14 @@ use crate::models::{ListParams, Owner};
use crate::problem::{reject_anyhow, unauthorized_no_api_key};
use crate::Environment;
use super::{authenticate, check_etag, JsonWithETag};
use super::{authenticate, check_etag, DataReply, ETagReply, Json};
pub async fn get(id: i32, etag: Option<String>, env: Environment) -> Result<impl Reply, Rejection> {
let response = CACHES
.owner
.get_response(id, || async {
let owner = Owner::get(&env.db, id).await?;
let reply = JsonWithETag::from_serializable(&owner)?;
let reply = ETagReply::<Json>::from_serializable(&owner)?;
let reply = with_status(reply, StatusCode::OK);
Ok(reply)
})
@ -35,7 +35,7 @@ pub async fn list(
.list_owners
.get_response(list_params.clone(), || async {
let owners = Owner::list(&env.db, &list_params).await?;
let reply = JsonWithETag::from_serializable(&owners)?;
let reply = ETagReply::<Json>::from_serializable(&owners)?;
let reply = with_status(reply, StatusCode::OK);
Ok(reply)
})
@ -68,7 +68,7 @@ pub async fn create(
.await
.map_err(reject_anyhow)?;
let url = saved_owner.url(&env.api_url).map_err(reject_anyhow)?;
let reply = JsonWithETag::from_serializable(&saved_owner).map_err(reject_anyhow)?;
let reply = ETagReply::<Json>::from_serializable(&saved_owner).map_err(reject_anyhow)?;
let reply = with_header(reply, "Location", url.as_str());
let reply = with_status(reply, StatusCode::CREATED);
tokio::spawn(async move {
@ -96,7 +96,7 @@ pub async fn update(
.await
.map_err(reject_anyhow)?;
let url = updated_owner.url(&env.api_url).map_err(reject_anyhow)?;
let reply = JsonWithETag::from_serializable(&updated_owner).map_err(reject_anyhow)?;
let reply = ETagReply::<Json>::from_serializable(&updated_owner).map_err(reject_anyhow)?;
let reply = with_header(reply, "Location", url.as_str());
let reply = with_status(reply, StatusCode::CREATED);
tokio::spawn(async move {

View File

@ -1,6 +1,5 @@
use anyhow::Result;
use http::StatusCode;
use sqlx::types::Json;
use uuid::Uuid;
use warp::reply::{with_header, with_status};
use warp::{Rejection, Reply};
@ -10,14 +9,14 @@ use crate::models::{InteriorRefList, ListParams, MerchandiseList, Shop};
use crate::problem::reject_anyhow;
use crate::Environment;
use super::{authenticate, check_etag, JsonWithETag};
use super::{authenticate, check_etag, DataReply, ETagReply, Json};
pub async fn get(id: i32, etag: Option<String>, env: Environment) -> Result<impl Reply, Rejection> {
let response = CACHES
.shop
.get_response(id, || async {
let shop = Shop::get(&env.db, id).await?;
let reply = JsonWithETag::from_serializable(&shop)?;
let reply = ETagReply::<Json>::from_serializable(&shop)?;
let reply = with_status(reply, StatusCode::OK);
Ok(reply)
})
@ -34,7 +33,7 @@ pub async fn list(
.list_shops
.get_response(list_params.clone(), || async {
let shops = Shop::list(&env.db, &list_params).await?;
let reply = JsonWithETag::from_serializable(&shops)?;
let reply = ETagReply::<Json>::from_serializable(&shops)?;
let reply = with_status(reply, StatusCode::OK);
Ok(reply)
})
@ -63,7 +62,7 @@ pub async fn create(
id: None,
shop_id,
owner_id: Some(owner_id),
ref_list: Json::default(),
ref_list: sqlx::types::Json::default(),
created_at: None,
updated_at: None,
};
@ -75,7 +74,7 @@ pub async fn create(
id: None,
shop_id,
owner_id: Some(owner_id),
form_list: Json::default(),
form_list: sqlx::types::Json::default(),
created_at: None,
updated_at: None,
};
@ -86,7 +85,7 @@ pub async fn create(
}
let url = saved_shop.url(&env.api_url).map_err(reject_anyhow)?;
let reply = JsonWithETag::from_serializable(&saved_shop).map_err(reject_anyhow)?;
let reply = ETagReply::<Json>::from_serializable(&saved_shop).map_err(reject_anyhow)?;
let reply = with_header(reply, "Location", url.as_str());
let reply = with_status(reply, StatusCode::CREATED);
tokio::spawn(async move {
@ -120,7 +119,7 @@ pub async fn update(
.await
.map_err(reject_anyhow)?;
let url = updated_shop.url(&env.api_url).map_err(reject_anyhow)?;
let reply = JsonWithETag::from_serializable(&updated_shop).map_err(reject_anyhow)?;
let reply = ETagReply::<Json>::from_serializable(&updated_shop).map_err(reject_anyhow)?;
let reply = with_header(reply, "Location", url.as_str());
let reply = with_status(reply, StatusCode::CREATED);
tokio::spawn(async move {

View File

@ -9,14 +9,14 @@ use crate::models::{ListParams, MerchandiseList, Transaction};
use crate::problem::reject_anyhow;
use crate::Environment;
use super::{authenticate, check_etag, JsonWithETag};
use super::{authenticate, check_etag, DataReply, ETagReply, Json};
pub async fn get(id: i32, etag: Option<String>, env: Environment) -> Result<impl Reply, Rejection> {
let response = CACHES
.transaction
.get_response(id, || async {
let transaction = Transaction::get(&env.db, id).await?;
let reply = JsonWithETag::from_serializable(&transaction)?;
let reply = ETagReply::<Json>::from_serializable(&transaction)?;
let reply = with_status(reply, StatusCode::OK);
Ok(reply)
})
@ -33,7 +33,7 @@ pub async fn list(
.list_transactions
.get_response(list_params.clone(), || async {
let transactions = Transaction::list(&env.db, &list_params).await?;
let reply = JsonWithETag::from_serializable(&transactions)?;
let reply = ETagReply::<Json>::from_serializable(&transactions)?;
let reply = with_status(reply, StatusCode::OK);
Ok(reply)
})
@ -51,7 +51,7 @@ pub async fn list_by_shop_id(
.list_transactions_by_shop_id
.get_response((shop_id, list_params.clone()), || async {
let transactions = Transaction::list_by_shop_id(&env.db, shop_id, &list_params).await?;
let reply = JsonWithETag::from_serializable(&transactions)?;
let reply = ETagReply::<Json>::from_serializable(&transactions)?;
let reply = with_status(reply, StatusCode::OK);
Ok(reply)
})
@ -99,7 +99,7 @@ pub async fn create(
.await
.map_err(|error| reject_anyhow(anyhow!(error)))?;
let url = saved_transaction.url(&env.api_url).map_err(reject_anyhow)?;
let reply = JsonWithETag::from_serializable(&saved_transaction).map_err(reject_anyhow)?;
let reply = ETagReply::<Json>::from_serializable(&saved_transaction).map_err(reject_anyhow)?;
let reply = with_header(reply, "Location", url.as_str());
let reply = with_status(reply, StatusCode::CREATED);
tokio::spawn(async move {

View File

@ -166,6 +166,7 @@ async fn main() -> Result<()> {
.and(warp::path::end())
.and(warp::get())
.and(warp::header::optional("if-none-match"))
.and(warp::header::optional("accept"))
.and(with_env(env.clone()))
.and_then(handlers::interior_ref_list::get),
);
@ -174,6 +175,7 @@ async fn main() -> Result<()> {
.and(warp::post())
.and(json_body::<InteriorRefList>())
.and(warp::header::optional("api-key"))
.and(warp::header::optional("content-type"))
.and(with_env(env.clone()))
.and_then(handlers::interior_ref_list::create),
);
@ -191,6 +193,7 @@ async fn main() -> Result<()> {
.and(warp::patch())
.and(json_body::<InteriorRefList>())
.and(warp::header::optional("api-key"))
.and(warp::header::optional("content-type"))
.and(with_env(env.clone()))
.and_then(handlers::interior_ref_list::update),
);
@ -201,6 +204,7 @@ async fn main() -> Result<()> {
.and(warp::patch())
.and(json_body::<InteriorRefList>())
.and(warp::header::optional("api-key"))
.and(warp::header::optional("content-type"))
.and(with_env(env.clone()))
.and_then(handlers::interior_ref_list::update_by_shop_id),
);
@ -209,6 +213,7 @@ async fn main() -> Result<()> {
.and(warp::get())
.and(warp::query::<ListParams>())
.and(warp::header::optional("if-none-match"))
.and(warp::header::optional("accept"))
.and(with_env(env.clone()))
.and_then(handlers::interior_ref_list::list),
);
@ -218,6 +223,7 @@ async fn main() -> Result<()> {
.and(warp::path::end())
.and(warp::get())
.and(warp::header::optional("if-none-match"))
.and(warp::header::optional("accept"))
.and(with_env(env.clone()))
.and_then(handlers::interior_ref_list::get_by_shop_id),
);
@ -226,6 +232,7 @@ async fn main() -> Result<()> {
.and(warp::path::end())
.and(warp::get())
.and(warp::header::optional("if-none-match"))
.and(warp::header::optional("accept"))
.and(with_env(env.clone()))
.and_then(handlers::merchandise_list::get),
);
@ -234,6 +241,7 @@ async fn main() -> Result<()> {
.and(warp::post())
.and(json_body::<MerchandiseList>())
.and(warp::header::optional("api-key"))
.and(warp::header::optional("content-type"))
.and(with_env(env.clone()))
.and_then(handlers::merchandise_list::create),
);
@ -251,6 +259,7 @@ async fn main() -> Result<()> {
.and(warp::patch())
.and(json_body::<MerchandiseList>())
.and(warp::header::optional("api-key"))
.and(warp::header::optional("content-type"))
.and(with_env(env.clone()))
.and_then(handlers::merchandise_list::update),
);
@ -261,6 +270,7 @@ async fn main() -> Result<()> {
.and(warp::patch())
.and(json_body::<MerchandiseList>())
.and(warp::header::optional("api-key"))
.and(warp::header::optional("content-type"))
.and(with_env(env.clone()))
.and_then(handlers::merchandise_list::update_by_shop_id),
);
@ -269,6 +279,7 @@ async fn main() -> Result<()> {
.and(warp::get())
.and(warp::query::<ListParams>())
.and(warp::header::optional("if-none-match"))
.and(warp::header::optional("accept"))
.and(with_env(env.clone()))
.and_then(handlers::merchandise_list::list),
);