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:
parent
2f69c86645
commit
6ac4b03a0a
@ -17,17 +17,29 @@ lazy_static! {
|
|||||||
pub struct Caches {
|
pub struct Caches {
|
||||||
pub owner_ids_by_api_key: Cache<Uuid, i32>,
|
pub owner_ids_by_api_key: Cache<Uuid, i32>,
|
||||||
pub shop: Cache<i32, CachedResponse>,
|
pub shop: Cache<i32, CachedResponse>,
|
||||||
|
pub shop_bin: Cache<i32, CachedResponse>,
|
||||||
pub owner: 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: Cache<i32, CachedResponse>,
|
||||||
|
pub interior_ref_list_bin: Cache<i32, CachedResponse>,
|
||||||
pub merchandise_list: Cache<i32, CachedResponse>,
|
pub merchandise_list: Cache<i32, CachedResponse>,
|
||||||
|
pub merchandise_list_bin: Cache<i32, CachedResponse>,
|
||||||
pub transaction: Cache<i32, CachedResponse>,
|
pub transaction: Cache<i32, CachedResponse>,
|
||||||
|
pub transaction_bin: Cache<i32, CachedResponse>,
|
||||||
pub list_shops: Cache<ListParams, CachedResponse>,
|
pub list_shops: Cache<ListParams, CachedResponse>,
|
||||||
|
pub list_shops_bin: Cache<ListParams, CachedResponse>,
|
||||||
pub list_owners: 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: Cache<ListParams, CachedResponse>,
|
||||||
|
pub list_interior_ref_lists_bin: Cache<ListParams, CachedResponse>,
|
||||||
pub list_merchandise_lists: 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: 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: 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: 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: Cache<i32, CachedResponse>,
|
||||||
pub merchandise_list_by_shop_id_bin: Cache<i32, CachedResponse>,
|
pub merchandise_list_by_shop_id_bin: Cache<i32, CachedResponse>,
|
||||||
}
|
}
|
||||||
@ -37,17 +49,29 @@ impl Caches {
|
|||||||
Caches {
|
Caches {
|
||||||
owner_ids_by_api_key: Cache::new("owner_ids_by_api_key", 100).log_keys(false),
|
owner_ids_by_api_key: Cache::new("owner_ids_by_api_key", 100).log_keys(false),
|
||||||
shop: Cache::new("shop", 100),
|
shop: Cache::new("shop", 100),
|
||||||
|
shop_bin: Cache::new("shop_bin", 100),
|
||||||
owner: Cache::new("owner", 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: 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: Cache::new("merchandise_list", 100),
|
||||||
|
merchandise_list_bin: Cache::new("merchandise_list_bin", 100),
|
||||||
transaction: Cache::new("transaction", 100),
|
transaction: Cache::new("transaction", 100),
|
||||||
|
transaction_bin: Cache::new("transaction_bin", 100),
|
||||||
list_shops: Cache::new("list_shops", 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: 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: 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: 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: 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: 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: 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: Cache::new("merchandise_list_by_shop_id", 100),
|
||||||
merchandise_list_by_shop_id_bin: Cache::new("merchandise_list_by_shop_id_bin", 100),
|
merchandise_list_by_shop_id_bin: Cache::new("merchandise_list_by_shop_id_bin", 100),
|
||||||
}
|
}
|
||||||
|
@ -1,68 +1,124 @@
|
|||||||
use anyhow::Result;
|
use anyhow::Result;
|
||||||
use http::StatusCode;
|
use http::StatusCode;
|
||||||
|
use mime::Mime;
|
||||||
use uuid::Uuid;
|
use uuid::Uuid;
|
||||||
use warp::reply::{with_header, with_status};
|
use warp::reply::{with_header, with_status};
|
||||||
use warp::{Rejection, Reply};
|
use warp::{Rejection, Reply};
|
||||||
|
|
||||||
use crate::caches::CACHES;
|
use crate::caches::{Cache, CachedResponse, CACHES};
|
||||||
use crate::models::{InteriorRefList, ListParams};
|
use crate::models::{InteriorRefList, ListParams};
|
||||||
use crate::problem::reject_anyhow;
|
use crate::problem::reject_anyhow;
|
||||||
use crate::Environment;
|
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> {
|
pub async fn get(
|
||||||
let response = CACHES
|
id: i32,
|
||||||
.interior_ref_list
|
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 {
|
.get_response(id, || async {
|
||||||
let interior_ref_list = InteriorRefList::get(&env.db, id).await?;
|
let interior_ref_list = InteriorRefList::get(&env.db, id).await?;
|
||||||
let reply = JsonWithETag::from_serializable(&interior_ref_list)?;
|
let reply = T::from_serializable(&interior_ref_list)?;
|
||||||
let reply = with_status(reply, StatusCode::OK);
|
let reply = with_status(reply, StatusCode::OK);
|
||||||
Ok(reply)
|
Ok(reply)
|
||||||
})
|
})
|
||||||
.await?;
|
.await?;
|
||||||
Ok(check_etag(etag, response))
|
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(
|
pub async fn get_by_shop_id(
|
||||||
shop_id: i32,
|
shop_id: i32,
|
||||||
etag: Option<String>,
|
etag: Option<String>,
|
||||||
|
accept: Option<AcceptHeader>,
|
||||||
env: Environment,
|
env: Environment,
|
||||||
) -> Result<impl Reply, Rejection> {
|
) -> Result<impl Reply, Rejection> {
|
||||||
let response = CACHES
|
async fn get<T: DataReply>(
|
||||||
.interior_ref_list_by_shop_id
|
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 {
|
.get_response(shop_id, || async {
|
||||||
let interior_ref_list = InteriorRefList::get_by_shop_id(&env.db, shop_id).await?;
|
let interior_ref_list = InteriorRefList::get_by_shop_id(&env.db, shop_id).await?;
|
||||||
let reply = JsonWithETag::from_serializable(&interior_ref_list)?;
|
let reply = T::from_serializable(&interior_ref_list)?;
|
||||||
let reply = with_status(reply, StatusCode::OK);
|
let reply = with_status(reply, StatusCode::OK);
|
||||||
Ok(reply)
|
Ok(reply)
|
||||||
})
|
})
|
||||||
.await?;
|
.await?;
|
||||||
Ok(check_etag(etag, response))
|
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(
|
pub async fn list(
|
||||||
list_params: ListParams,
|
list_params: ListParams,
|
||||||
etag: Option<String>,
|
etag: Option<String>,
|
||||||
|
accept: Option<AcceptHeader>,
|
||||||
env: Environment,
|
env: Environment,
|
||||||
) -> Result<impl Reply, Rejection> {
|
) -> Result<impl Reply, Rejection> {
|
||||||
let response = CACHES
|
async fn get<T: DataReply>(
|
||||||
.list_interior_ref_lists
|
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 {
|
.get_response(list_params.clone(), || async {
|
||||||
let interior_ref_lists = InteriorRefList::list(&env.db, &list_params).await?;
|
let interior_ref_lists = InteriorRefList::list(&env.db, &list_params).await?;
|
||||||
let reply = JsonWithETag::from_serializable(&interior_ref_lists)?;
|
let reply = T::from_serializable(&interior_ref_lists)?;
|
||||||
let reply = with_status(reply, StatusCode::OK);
|
let reply = with_status(reply, StatusCode::OK);
|
||||||
Ok(reply)
|
Ok(reply)
|
||||||
})
|
})
|
||||||
.await?;
|
.await?;
|
||||||
Ok(check_etag(etag, response))
|
|
||||||
|
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(
|
pub async fn create(
|
||||||
interior_ref_list: InteriorRefList,
|
interior_ref_list: InteriorRefList,
|
||||||
api_key: Option<Uuid>,
|
api_key: Option<Uuid>,
|
||||||
|
content_type: Option<Mime>,
|
||||||
env: Environment,
|
env: Environment,
|
||||||
) -> Result<impl Reply, Rejection> {
|
) -> Result<impl Reply, Rejection> {
|
||||||
|
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 owner_id = authenticate(&env, api_key).await.map_err(reject_anyhow)?;
|
||||||
let ref_list_with_owner_id = InteriorRefList {
|
let ref_list_with_owner_id = InteriorRefList {
|
||||||
owner_id: Some(owner_id),
|
owner_id: Some(owner_id),
|
||||||
@ -75,25 +131,46 @@ 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 = JsonWithETag::from_serializable(&saved_interior_ref_list).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_header(reply, "Location", url.as_str());
|
||||||
let reply = with_status(reply, StatusCode::CREATED);
|
let reply = with_status(reply, StatusCode::CREATED);
|
||||||
tokio::spawn(async move {
|
tokio::spawn(async move {
|
||||||
CACHES.list_interior_ref_lists.clear().await;
|
CACHES.list_interior_ref_lists.clear().await;
|
||||||
|
CACHES.list_interior_ref_lists_bin.clear().await;
|
||||||
CACHES
|
CACHES
|
||||||
.interior_ref_list_by_shop_id
|
.interior_ref_list_by_shop_id
|
||||||
.delete_response(saved_interior_ref_list.shop_id)
|
.delete_response(saved_interior_ref_list.shop_id)
|
||||||
.await;
|
.await;
|
||||||
|
CACHES
|
||||||
|
.interior_ref_list_by_shop_id_bin
|
||||||
|
.delete_response(saved_interior_ref_list.shop_id)
|
||||||
|
.await;
|
||||||
});
|
});
|
||||||
Ok(reply)
|
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(
|
pub async fn update(
|
||||||
id: i32,
|
id: i32,
|
||||||
interior_ref_list: InteriorRefList,
|
interior_ref_list: InteriorRefList,
|
||||||
api_key: Option<Uuid>,
|
api_key: Option<Uuid>,
|
||||||
|
content_type: Option<Mime>,
|
||||||
env: Environment,
|
env: Environment,
|
||||||
) -> Result<impl Reply, Rejection> {
|
) -> Result<impl Reply, Rejection> {
|
||||||
|
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 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() {
|
let interior_ref_list_with_id_and_owner_id = if interior_ref_list.owner_id.is_some() {
|
||||||
InteriorRefList {
|
InteriorRefList {
|
||||||
@ -114,27 +191,47 @@ 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 =
|
let reply = T::from_serializable(&updated_interior_ref_list).map_err(reject_anyhow)?;
|
||||||
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);
|
||||||
tokio::spawn(async move {
|
tokio::spawn(async move {
|
||||||
CACHES.interior_ref_list.delete_response(id).await;
|
CACHES.interior_ref_list.delete_response(id).await;
|
||||||
|
CACHES.interior_ref_list_bin.delete_response(id).await;
|
||||||
CACHES
|
CACHES
|
||||||
.interior_ref_list_by_shop_id
|
.interior_ref_list_by_shop_id
|
||||||
.delete_response(updated_interior_ref_list.shop_id)
|
.delete_response(updated_interior_ref_list.shop_id)
|
||||||
.await;
|
.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.clear().await;
|
||||||
|
CACHES.list_interior_ref_lists_bin.clear().await;
|
||||||
});
|
});
|
||||||
Ok(reply)
|
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
|
||||||
|
}
|
||||||
|
_ => update::<ETagReply<Json>>(id, interior_ref_list, api_key, env).await,
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
pub async fn update_by_shop_id(
|
pub async fn update_by_shop_id(
|
||||||
shop_id: i32,
|
shop_id: i32,
|
||||||
interior_ref_list: InteriorRefList,
|
interior_ref_list: InteriorRefList,
|
||||||
api_key: Option<Uuid>,
|
api_key: Option<Uuid>,
|
||||||
|
content_type: Option<Mime>,
|
||||||
env: Environment,
|
env: Environment,
|
||||||
) -> Result<impl Reply, Rejection> {
|
) -> Result<impl Reply, Rejection> {
|
||||||
|
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 owner_id = authenticate(&env, api_key).await.map_err(reject_anyhow)?;
|
||||||
let interior_ref_list_with_owner_id = InteriorRefList {
|
let interior_ref_list_with_owner_id = InteriorRefList {
|
||||||
owner_id: Some(owner_id),
|
owner_id: Some(owner_id),
|
||||||
@ -147,26 +244,35 @@ 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 =
|
let reply = T::from_serializable(&updated_interior_ref_list).map_err(reject_anyhow)?;
|
||||||
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);
|
||||||
tokio::spawn(async move {
|
tokio::spawn(async move {
|
||||||
CACHES
|
let id = updated_interior_ref_list
|
||||||
.interior_ref_list
|
|
||||||
.delete_response(
|
|
||||||
updated_interior_ref_list
|
|
||||||
.id
|
.id
|
||||||
.expect("saved interior_ref_list has no id"),
|
.expect("saved interior_ref_list has no id");
|
||||||
)
|
CACHES.interior_ref_list.delete_response(id).await;
|
||||||
.await;
|
CACHES.interior_ref_list_bin.delete_response(id).await;
|
||||||
CACHES
|
CACHES
|
||||||
.interior_ref_list_by_shop_id
|
.interior_ref_list_by_shop_id
|
||||||
.delete_response(updated_interior_ref_list.shop_id)
|
.delete_response(updated_interior_ref_list.shop_id)
|
||||||
.await;
|
.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.clear().await;
|
||||||
|
CACHES.list_interior_ref_lists_bin.clear().await;
|
||||||
});
|
});
|
||||||
Ok(reply)
|
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(
|
pub async fn delete(
|
||||||
|
@ -1,27 +1,46 @@
|
|||||||
use anyhow::Result;
|
use anyhow::Result;
|
||||||
use http::StatusCode;
|
use http::StatusCode;
|
||||||
|
use mime::Mime;
|
||||||
use uuid::Uuid;
|
use uuid::Uuid;
|
||||||
use warp::reply::{with_header, with_status};
|
use warp::reply::{with_header, with_status};
|
||||||
use warp::{Rejection, Reply};
|
use warp::{Rejection, Reply};
|
||||||
|
|
||||||
use crate::caches::CACHES;
|
use crate::caches::{Cache, CachedResponse, CACHES};
|
||||||
use crate::models::{ListParams, MerchandiseList};
|
use crate::models::{ListParams, MerchandiseList};
|
||||||
use crate::problem::reject_anyhow;
|
use crate::problem::reject_anyhow;
|
||||||
use crate::Environment;
|
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> {
|
pub async fn get(
|
||||||
let response = CACHES
|
id: i32,
|
||||||
.merchandise_list
|
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 {
|
.get_response(id, || async {
|
||||||
let merchandise_list = MerchandiseList::get(&env.db, id).await?;
|
let merchandise_list = MerchandiseList::get(&env.db, id).await?;
|
||||||
let reply = JsonWithETag::from_serializable(&merchandise_list)?;
|
let reply = T::from_serializable(&merchandise_list)?;
|
||||||
let reply = with_status(reply, StatusCode::OK);
|
let reply = with_status(reply, StatusCode::OK);
|
||||||
Ok(reply)
|
Ok(reply)
|
||||||
})
|
})
|
||||||
.await?;
|
.await?;
|
||||||
Ok(check_etag(etag, response))
|
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(
|
pub async fn get_by_shop_id(
|
||||||
@ -30,57 +49,75 @@ pub async fn get_by_shop_id(
|
|||||||
accept: Option<AcceptHeader>,
|
accept: Option<AcceptHeader>,
|
||||||
env: Environment,
|
env: Environment,
|
||||||
) -> Result<impl Reply, Rejection> {
|
) -> 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() => {
|
Some(accept) if accept.accepts_bincode() => {
|
||||||
CACHES
|
get::<ETagReply<Bincode>>(shop_id, etag, env, &CACHES.merchandise_list_by_shop_id_bin)
|
||||||
.merchandise_list_by_shop_id_bin
|
.await
|
||||||
.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<Json>>(shop_id, etag, env, &CACHES.merchandise_list_by_shop_id).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))
|
|
||||||
}
|
}
|
||||||
|
|
||||||
pub async fn list(
|
pub async fn list(
|
||||||
list_params: ListParams,
|
list_params: ListParams,
|
||||||
etag: Option<String>,
|
etag: Option<String>,
|
||||||
|
accept: Option<AcceptHeader>,
|
||||||
env: Environment,
|
env: Environment,
|
||||||
) -> Result<impl Reply, Rejection> {
|
) -> Result<impl Reply, Rejection> {
|
||||||
let response = CACHES
|
async fn get<T: DataReply>(
|
||||||
.list_merchandise_lists
|
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 {
|
.get_response(list_params.clone(), || async {
|
||||||
let merchandise_lists = MerchandiseList::list(&env.db, &list_params).await?;
|
let merchandise_lists = MerchandiseList::list(&env.db, &list_params).await?;
|
||||||
let reply = JsonWithETag::from_serializable(&merchandise_lists)?;
|
let reply = T::from_serializable(&merchandise_lists)?;
|
||||||
let reply = with_status(reply, StatusCode::OK);
|
let reply = with_status(reply, StatusCode::OK);
|
||||||
Ok(reply)
|
Ok(reply)
|
||||||
})
|
})
|
||||||
.await?;
|
.await?;
|
||||||
Ok(check_etag(etag, response))
|
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(
|
pub async fn create(
|
||||||
merchandise_list: MerchandiseList,
|
merchandise_list: MerchandiseList,
|
||||||
api_key: Option<Uuid>,
|
api_key: Option<Uuid>,
|
||||||
|
content_type: Option<Mime>,
|
||||||
env: Environment,
|
env: Environment,
|
||||||
) -> Result<impl Reply, Rejection> {
|
) -> Result<impl Reply, Rejection> {
|
||||||
|
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 owner_id = authenticate(&env, api_key).await.map_err(reject_anyhow)?;
|
||||||
let ref_list_with_owner_id = MerchandiseList {
|
let ref_list_with_owner_id = MerchandiseList {
|
||||||
owner_id: Some(owner_id),
|
owner_id: Some(owner_id),
|
||||||
@ -93,25 +130,45 @@ 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 = JsonWithETag::from_serializable(&saved_merchandise_list).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_header(reply, "Location", url.as_str());
|
||||||
let reply = with_status(reply, StatusCode::CREATED);
|
let reply = with_status(reply, StatusCode::CREATED);
|
||||||
tokio::spawn(async move {
|
tokio::spawn(async move {
|
||||||
CACHES.list_merchandise_lists.clear().await;
|
CACHES.list_merchandise_lists.clear().await;
|
||||||
|
CACHES.list_merchandise_lists_bin.clear().await;
|
||||||
CACHES
|
CACHES
|
||||||
.merchandise_list_by_shop_id
|
.merchandise_list_by_shop_id
|
||||||
.delete_response(saved_merchandise_list.shop_id)
|
.delete_response(saved_merchandise_list.shop_id)
|
||||||
.await;
|
.await;
|
||||||
|
CACHES
|
||||||
|
.merchandise_list_by_shop_id_bin
|
||||||
|
.delete_response(saved_merchandise_list.shop_id)
|
||||||
|
.await;
|
||||||
});
|
});
|
||||||
Ok(reply)
|
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(
|
pub async fn update(
|
||||||
id: i32,
|
id: i32,
|
||||||
merchandise_list: MerchandiseList,
|
merchandise_list: MerchandiseList,
|
||||||
api_key: Option<Uuid>,
|
api_key: Option<Uuid>,
|
||||||
|
content_type: Option<Mime>,
|
||||||
env: Environment,
|
env: Environment,
|
||||||
) -> Result<impl Reply, Rejection> {
|
) -> Result<impl Reply, Rejection> {
|
||||||
|
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 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() {
|
let merchandise_list_with_id_and_owner_id = if merchandise_list.owner_id.is_some() {
|
||||||
MerchandiseList {
|
MerchandiseList {
|
||||||
@ -132,27 +189,47 @@ 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 =
|
let reply = T::from_serializable(&updated_merchandise_list).map_err(reject_anyhow)?;
|
||||||
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);
|
||||||
tokio::spawn(async move {
|
tokio::spawn(async move {
|
||||||
CACHES.merchandise_list.delete_response(id).await;
|
CACHES.merchandise_list.delete_response(id).await;
|
||||||
|
CACHES.merchandise_list_bin.delete_response(id).await;
|
||||||
CACHES
|
CACHES
|
||||||
.merchandise_list_by_shop_id
|
.merchandise_list_by_shop_id
|
||||||
.delete_response(updated_merchandise_list.shop_id)
|
.delete_response(updated_merchandise_list.shop_id)
|
||||||
.await;
|
.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.clear().await;
|
||||||
|
CACHES.list_merchandise_lists_bin.clear().await;
|
||||||
});
|
});
|
||||||
Ok(reply)
|
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
|
||||||
|
}
|
||||||
|
_ => update::<ETagReply<Json>>(id, merchandise_list, api_key, env).await,
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
pub async fn update_by_shop_id(
|
pub async fn update_by_shop_id(
|
||||||
shop_id: i32,
|
shop_id: i32,
|
||||||
merchandise_list: MerchandiseList,
|
merchandise_list: MerchandiseList,
|
||||||
api_key: Option<Uuid>,
|
api_key: Option<Uuid>,
|
||||||
|
content_type: Option<Mime>,
|
||||||
env: Environment,
|
env: Environment,
|
||||||
) -> Result<impl Reply, Rejection> {
|
) -> Result<impl Reply, Rejection> {
|
||||||
|
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 owner_id = authenticate(&env, api_key).await.map_err(reject_anyhow)?;
|
||||||
let merchandise_list_with_owner_id = MerchandiseList {
|
let merchandise_list_with_owner_id = MerchandiseList {
|
||||||
owner_id: Some(owner_id),
|
owner_id: Some(owner_id),
|
||||||
@ -165,26 +242,36 @@ 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 =
|
let reply = ETagReply::<Json>::from_serializable(&updated_merchandise_list)
|
||||||
JsonWithETag::from_serializable(&updated_merchandise_list).map_err(reject_anyhow)?;
|
.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);
|
||||||
tokio::spawn(async move {
|
tokio::spawn(async move {
|
||||||
CACHES
|
let id = updated_merchandise_list
|
||||||
.merchandise_list
|
|
||||||
.delete_response(
|
|
||||||
updated_merchandise_list
|
|
||||||
.id
|
.id
|
||||||
.expect("saved merchandise_list has no id"),
|
.expect("saved merchandise_list has no id");
|
||||||
)
|
CACHES.merchandise_list.delete_response(id).await;
|
||||||
.await;
|
CACHES.merchandise_list_bin.delete_response(id).await;
|
||||||
CACHES
|
CACHES
|
||||||
.merchandise_list_by_shop_id
|
.merchandise_list_by_shop_id
|
||||||
.delete_response(updated_merchandise_list.shop_id)
|
.delete_response(updated_merchandise_list.shop_id)
|
||||||
.await;
|
.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.clear().await;
|
||||||
|
CACHES.list_merchandise_lists_bin.clear().await;
|
||||||
});
|
});
|
||||||
Ok(reply)
|
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(
|
pub async fn delete(
|
||||||
@ -201,11 +288,17 @@ pub async fn delete(
|
|||||||
.map_err(reject_anyhow)?;
|
.map_err(reject_anyhow)?;
|
||||||
tokio::spawn(async move {
|
tokio::spawn(async move {
|
||||||
CACHES.merchandise_list.delete_response(id).await;
|
CACHES.merchandise_list.delete_response(id).await;
|
||||||
|
CACHES.merchandise_list_bin.delete_response(id).await;
|
||||||
CACHES
|
CACHES
|
||||||
.merchandise_list_by_shop_id
|
.merchandise_list_by_shop_id
|
||||||
.delete_response(merchandise_list.shop_id)
|
.delete_response(merchandise_list.shop_id)
|
||||||
.await;
|
.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.clear().await;
|
||||||
|
CACHES.list_merchandise_lists_bin.clear().await;
|
||||||
});
|
});
|
||||||
Ok(StatusCode::NO_CONTENT)
|
Ok(StatusCode::NO_CONTENT)
|
||||||
}
|
}
|
||||||
|
@ -1,3 +1,4 @@
|
|||||||
|
use std::marker::PhantomData;
|
||||||
use std::str::FromStr;
|
use std::str::FromStr;
|
||||||
|
|
||||||
use anyhow::{anyhow, Error, Result};
|
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`.
|
// 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`.
|
// 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.
|
// 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>,
|
body: Vec<u8>,
|
||||||
etag: String,
|
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 {
|
fn into_response(self) -> Response {
|
||||||
let mut res = Response::new(self.body.into());
|
let mut res = Response::new(self.body.into());
|
||||||
res.headers_mut()
|
res.headers_mut()
|
||||||
@ -70,8 +79,8 @@ impl Reply for JsonWithETag {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl JsonWithETag {
|
impl DataReply for ETagReply<Json> {
|
||||||
pub fn from_serializable<T: Serialize>(val: &T) -> Result<Self> {
|
fn from_serializable<T: Serialize>(val: &T) -> Result<Self> {
|
||||||
let bytes = serde_json::to_vec(val).map_err(|err| {
|
let bytes = serde_json::to_vec(val).map_err(|err| {
|
||||||
error!("Failed to serialize database value to JSON: {}", err);
|
error!("Failed to serialize database value to JSON: {}", err);
|
||||||
anyhow!(HttpApiProblem::with_title_and_type_from_status(
|
anyhow!(HttpApiProblem::with_title_and_type_from_status(
|
||||||
@ -83,16 +92,15 @@ impl JsonWithETag {
|
|||||||
)))
|
)))
|
||||||
})?;
|
})?;
|
||||||
let etag = format!("{:x}", hash(&bytes));
|
let etag = format!("{:x}", hash(&bytes));
|
||||||
Ok(Self { body: bytes, etag })
|
Ok(Self {
|
||||||
|
body: bytes,
|
||||||
|
etag,
|
||||||
|
content_type: PhantomData,
|
||||||
|
})
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
pub struct BincodeWithETag {
|
impl Reply for ETagReply<Bincode> {
|
||||||
body: Vec<u8>,
|
|
||||||
etag: String,
|
|
||||||
}
|
|
||||||
|
|
||||||
impl Reply for BincodeWithETag {
|
|
||||||
fn into_response(self) -> Response {
|
fn into_response(self) -> Response {
|
||||||
let mut res = Response::new(self.body.into());
|
let mut res = Response::new(self.body.into());
|
||||||
res.headers_mut().insert(
|
res.headers_mut().insert(
|
||||||
@ -109,8 +117,8 @@ impl Reply for BincodeWithETag {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl BincodeWithETag {
|
impl DataReply for ETagReply<Bincode> {
|
||||||
pub fn from_serializable<T: Serialize>(val: &T) -> Result<Self> {
|
fn from_serializable<T: Serialize>(val: &T) -> Result<Self> {
|
||||||
let bytes = bincode::serialize(val).map_err(|err| {
|
let bytes = bincode::serialize(val).map_err(|err| {
|
||||||
error!("Failed to serialize database value to bincode: {}", err);
|
error!("Failed to serialize database value to bincode: {}", err);
|
||||||
anyhow!(HttpApiProblem::with_title_and_type_from_status(
|
anyhow!(HttpApiProblem::with_title_and_type_from_status(
|
||||||
@ -122,7 +130,11 @@ impl BincodeWithETag {
|
|||||||
)))
|
)))
|
||||||
})?;
|
})?;
|
||||||
let etag = format!("{:x}", hash(&bytes));
|
let etag = format!("{:x}", hash(&bytes));
|
||||||
Ok(Self { body: bytes, etag })
|
Ok(Self {
|
||||||
|
body: bytes,
|
||||||
|
etag,
|
||||||
|
content_type: PhantomData,
|
||||||
|
})
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -11,14 +11,14 @@ use crate::models::{ListParams, Owner};
|
|||||||
use crate::problem::{reject_anyhow, unauthorized_no_api_key};
|
use crate::problem::{reject_anyhow, unauthorized_no_api_key};
|
||||||
use crate::Environment;
|
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> {
|
pub async fn get(id: i32, etag: Option<String>, env: Environment) -> Result<impl Reply, Rejection> {
|
||||||
let response = CACHES
|
let response = CACHES
|
||||||
.owner
|
.owner
|
||||||
.get_response(id, || async {
|
.get_response(id, || async {
|
||||||
let owner = Owner::get(&env.db, id).await?;
|
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);
|
let reply = with_status(reply, StatusCode::OK);
|
||||||
Ok(reply)
|
Ok(reply)
|
||||||
})
|
})
|
||||||
@ -35,7 +35,7 @@ pub async fn list(
|
|||||||
.list_owners
|
.list_owners
|
||||||
.get_response(list_params.clone(), || async {
|
.get_response(list_params.clone(), || async {
|
||||||
let owners = Owner::list(&env.db, &list_params).await?;
|
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);
|
let reply = with_status(reply, StatusCode::OK);
|
||||||
Ok(reply)
|
Ok(reply)
|
||||||
})
|
})
|
||||||
@ -68,7 +68,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 = 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_header(reply, "Location", url.as_str());
|
||||||
let reply = with_status(reply, StatusCode::CREATED);
|
let reply = with_status(reply, StatusCode::CREATED);
|
||||||
tokio::spawn(async move {
|
tokio::spawn(async move {
|
||||||
@ -96,7 +96,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 = 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_header(reply, "Location", url.as_str());
|
||||||
let reply = with_status(reply, StatusCode::CREATED);
|
let reply = with_status(reply, StatusCode::CREATED);
|
||||||
tokio::spawn(async move {
|
tokio::spawn(async move {
|
||||||
|
@ -1,6 +1,5 @@
|
|||||||
use anyhow::Result;
|
use anyhow::Result;
|
||||||
use http::StatusCode;
|
use http::StatusCode;
|
||||||
use sqlx::types::Json;
|
|
||||||
use uuid::Uuid;
|
use uuid::Uuid;
|
||||||
use warp::reply::{with_header, with_status};
|
use warp::reply::{with_header, with_status};
|
||||||
use warp::{Rejection, Reply};
|
use warp::{Rejection, Reply};
|
||||||
@ -10,14 +9,14 @@ use crate::models::{InteriorRefList, ListParams, MerchandiseList, Shop};
|
|||||||
use crate::problem::reject_anyhow;
|
use crate::problem::reject_anyhow;
|
||||||
use crate::Environment;
|
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> {
|
pub async fn get(id: i32, etag: Option<String>, env: Environment) -> Result<impl Reply, Rejection> {
|
||||||
let response = CACHES
|
let response = CACHES
|
||||||
.shop
|
.shop
|
||||||
.get_response(id, || async {
|
.get_response(id, || async {
|
||||||
let shop = Shop::get(&env.db, id).await?;
|
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);
|
let reply = with_status(reply, StatusCode::OK);
|
||||||
Ok(reply)
|
Ok(reply)
|
||||||
})
|
})
|
||||||
@ -34,7 +33,7 @@ pub async fn list(
|
|||||||
.list_shops
|
.list_shops
|
||||||
.get_response(list_params.clone(), || async {
|
.get_response(list_params.clone(), || async {
|
||||||
let shops = Shop::list(&env.db, &list_params).await?;
|
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);
|
let reply = with_status(reply, StatusCode::OK);
|
||||||
Ok(reply)
|
Ok(reply)
|
||||||
})
|
})
|
||||||
@ -63,7 +62,7 @@ pub async fn create(
|
|||||||
id: None,
|
id: None,
|
||||||
shop_id,
|
shop_id,
|
||||||
owner_id: Some(owner_id),
|
owner_id: Some(owner_id),
|
||||||
ref_list: Json::default(),
|
ref_list: sqlx::types::Json::default(),
|
||||||
created_at: None,
|
created_at: None,
|
||||||
updated_at: None,
|
updated_at: None,
|
||||||
};
|
};
|
||||||
@ -75,7 +74,7 @@ pub async fn create(
|
|||||||
id: None,
|
id: None,
|
||||||
shop_id,
|
shop_id,
|
||||||
owner_id: Some(owner_id),
|
owner_id: Some(owner_id),
|
||||||
form_list: Json::default(),
|
form_list: sqlx::types::Json::default(),
|
||||||
created_at: None,
|
created_at: None,
|
||||||
updated_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 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_header(reply, "Location", url.as_str());
|
||||||
let reply = with_status(reply, StatusCode::CREATED);
|
let reply = with_status(reply, StatusCode::CREATED);
|
||||||
tokio::spawn(async move {
|
tokio::spawn(async move {
|
||||||
@ -120,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 = 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_header(reply, "Location", url.as_str());
|
||||||
let reply = with_status(reply, StatusCode::CREATED);
|
let reply = with_status(reply, StatusCode::CREATED);
|
||||||
tokio::spawn(async move {
|
tokio::spawn(async move {
|
||||||
|
@ -9,14 +9,14 @@ use crate::models::{ListParams, MerchandiseList, Transaction};
|
|||||||
use crate::problem::reject_anyhow;
|
use crate::problem::reject_anyhow;
|
||||||
use crate::Environment;
|
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> {
|
pub async fn get(id: i32, etag: Option<String>, env: Environment) -> Result<impl Reply, Rejection> {
|
||||||
let response = CACHES
|
let response = CACHES
|
||||||
.transaction
|
.transaction
|
||||||
.get_response(id, || async {
|
.get_response(id, || async {
|
||||||
let transaction = Transaction::get(&env.db, id).await?;
|
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);
|
let reply = with_status(reply, StatusCode::OK);
|
||||||
Ok(reply)
|
Ok(reply)
|
||||||
})
|
})
|
||||||
@ -33,7 +33,7 @@ pub async fn list(
|
|||||||
.list_transactions
|
.list_transactions
|
||||||
.get_response(list_params.clone(), || async {
|
.get_response(list_params.clone(), || async {
|
||||||
let transactions = Transaction::list(&env.db, &list_params).await?;
|
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);
|
let reply = with_status(reply, StatusCode::OK);
|
||||||
Ok(reply)
|
Ok(reply)
|
||||||
})
|
})
|
||||||
@ -51,7 +51,7 @@ pub async fn list_by_shop_id(
|
|||||||
.list_transactions_by_shop_id
|
.list_transactions_by_shop_id
|
||||||
.get_response((shop_id, list_params.clone()), || async {
|
.get_response((shop_id, list_params.clone()), || async {
|
||||||
let transactions = Transaction::list_by_shop_id(&env.db, shop_id, &list_params).await?;
|
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);
|
let reply = with_status(reply, StatusCode::OK);
|
||||||
Ok(reply)
|
Ok(reply)
|
||||||
})
|
})
|
||||||
@ -99,7 +99,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 = 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_header(reply, "Location", url.as_str());
|
||||||
let reply = with_status(reply, StatusCode::CREATED);
|
let reply = with_status(reply, StatusCode::CREATED);
|
||||||
tokio::spawn(async move {
|
tokio::spawn(async move {
|
||||||
|
11
src/main.rs
11
src/main.rs
@ -166,6 +166,7 @@ async fn main() -> Result<()> {
|
|||||||
.and(warp::path::end())
|
.and(warp::path::end())
|
||||||
.and(warp::get())
|
.and(warp::get())
|
||||||
.and(warp::header::optional("if-none-match"))
|
.and(warp::header::optional("if-none-match"))
|
||||||
|
.and(warp::header::optional("accept"))
|
||||||
.and(with_env(env.clone()))
|
.and(with_env(env.clone()))
|
||||||
.and_then(handlers::interior_ref_list::get),
|
.and_then(handlers::interior_ref_list::get),
|
||||||
);
|
);
|
||||||
@ -174,6 +175,7 @@ async fn main() -> Result<()> {
|
|||||||
.and(warp::post())
|
.and(warp::post())
|
||||||
.and(json_body::<InteriorRefList>())
|
.and(json_body::<InteriorRefList>())
|
||||||
.and(warp::header::optional("api-key"))
|
.and(warp::header::optional("api-key"))
|
||||||
|
.and(warp::header::optional("content-type"))
|
||||||
.and(with_env(env.clone()))
|
.and(with_env(env.clone()))
|
||||||
.and_then(handlers::interior_ref_list::create),
|
.and_then(handlers::interior_ref_list::create),
|
||||||
);
|
);
|
||||||
@ -191,6 +193,7 @@ async fn main() -> Result<()> {
|
|||||||
.and(warp::patch())
|
.and(warp::patch())
|
||||||
.and(json_body::<InteriorRefList>())
|
.and(json_body::<InteriorRefList>())
|
||||||
.and(warp::header::optional("api-key"))
|
.and(warp::header::optional("api-key"))
|
||||||
|
.and(warp::header::optional("content-type"))
|
||||||
.and(with_env(env.clone()))
|
.and(with_env(env.clone()))
|
||||||
.and_then(handlers::interior_ref_list::update),
|
.and_then(handlers::interior_ref_list::update),
|
||||||
);
|
);
|
||||||
@ -201,6 +204,7 @@ async fn main() -> Result<()> {
|
|||||||
.and(warp::patch())
|
.and(warp::patch())
|
||||||
.and(json_body::<InteriorRefList>())
|
.and(json_body::<InteriorRefList>())
|
||||||
.and(warp::header::optional("api-key"))
|
.and(warp::header::optional("api-key"))
|
||||||
|
.and(warp::header::optional("content-type"))
|
||||||
.and(with_env(env.clone()))
|
.and(with_env(env.clone()))
|
||||||
.and_then(handlers::interior_ref_list::update_by_shop_id),
|
.and_then(handlers::interior_ref_list::update_by_shop_id),
|
||||||
);
|
);
|
||||||
@ -209,6 +213,7 @@ async fn main() -> Result<()> {
|
|||||||
.and(warp::get())
|
.and(warp::get())
|
||||||
.and(warp::query::<ListParams>())
|
.and(warp::query::<ListParams>())
|
||||||
.and(warp::header::optional("if-none-match"))
|
.and(warp::header::optional("if-none-match"))
|
||||||
|
.and(warp::header::optional("accept"))
|
||||||
.and(with_env(env.clone()))
|
.and(with_env(env.clone()))
|
||||||
.and_then(handlers::interior_ref_list::list),
|
.and_then(handlers::interior_ref_list::list),
|
||||||
);
|
);
|
||||||
@ -218,6 +223,7 @@ async fn main() -> Result<()> {
|
|||||||
.and(warp::path::end())
|
.and(warp::path::end())
|
||||||
.and(warp::get())
|
.and(warp::get())
|
||||||
.and(warp::header::optional("if-none-match"))
|
.and(warp::header::optional("if-none-match"))
|
||||||
|
.and(warp::header::optional("accept"))
|
||||||
.and(with_env(env.clone()))
|
.and(with_env(env.clone()))
|
||||||
.and_then(handlers::interior_ref_list::get_by_shop_id),
|
.and_then(handlers::interior_ref_list::get_by_shop_id),
|
||||||
);
|
);
|
||||||
@ -226,6 +232,7 @@ async fn main() -> Result<()> {
|
|||||||
.and(warp::path::end())
|
.and(warp::path::end())
|
||||||
.and(warp::get())
|
.and(warp::get())
|
||||||
.and(warp::header::optional("if-none-match"))
|
.and(warp::header::optional("if-none-match"))
|
||||||
|
.and(warp::header::optional("accept"))
|
||||||
.and(with_env(env.clone()))
|
.and(with_env(env.clone()))
|
||||||
.and_then(handlers::merchandise_list::get),
|
.and_then(handlers::merchandise_list::get),
|
||||||
);
|
);
|
||||||
@ -234,6 +241,7 @@ async fn main() -> Result<()> {
|
|||||||
.and(warp::post())
|
.and(warp::post())
|
||||||
.and(json_body::<MerchandiseList>())
|
.and(json_body::<MerchandiseList>())
|
||||||
.and(warp::header::optional("api-key"))
|
.and(warp::header::optional("api-key"))
|
||||||
|
.and(warp::header::optional("content-type"))
|
||||||
.and(with_env(env.clone()))
|
.and(with_env(env.clone()))
|
||||||
.and_then(handlers::merchandise_list::create),
|
.and_then(handlers::merchandise_list::create),
|
||||||
);
|
);
|
||||||
@ -251,6 +259,7 @@ async fn main() -> Result<()> {
|
|||||||
.and(warp::patch())
|
.and(warp::patch())
|
||||||
.and(json_body::<MerchandiseList>())
|
.and(json_body::<MerchandiseList>())
|
||||||
.and(warp::header::optional("api-key"))
|
.and(warp::header::optional("api-key"))
|
||||||
|
.and(warp::header::optional("content-type"))
|
||||||
.and(with_env(env.clone()))
|
.and(with_env(env.clone()))
|
||||||
.and_then(handlers::merchandise_list::update),
|
.and_then(handlers::merchandise_list::update),
|
||||||
);
|
);
|
||||||
@ -261,6 +270,7 @@ async fn main() -> Result<()> {
|
|||||||
.and(warp::patch())
|
.and(warp::patch())
|
||||||
.and(json_body::<MerchandiseList>())
|
.and(json_body::<MerchandiseList>())
|
||||||
.and(warp::header::optional("api-key"))
|
.and(warp::header::optional("api-key"))
|
||||||
|
.and(warp::header::optional("content-type"))
|
||||||
.and(with_env(env.clone()))
|
.and(with_env(env.clone()))
|
||||||
.and_then(handlers::merchandise_list::update_by_shop_id),
|
.and_then(handlers::merchandise_list::update_by_shop_id),
|
||||||
);
|
);
|
||||||
@ -269,6 +279,7 @@ async fn main() -> Result<()> {
|
|||||||
.and(warp::get())
|
.and(warp::get())
|
||||||
.and(warp::query::<ListParams>())
|
.and(warp::query::<ListParams>())
|
||||||
.and(warp::header::optional("if-none-match"))
|
.and(warp::header::optional("if-none-match"))
|
||||||
|
.and(warp::header::optional("accept"))
|
||||||
.and(with_env(env.clone()))
|
.and(with_env(env.clone()))
|
||||||
.and_then(handlers::merchandise_list::list),
|
.and_then(handlers::merchandise_list::list),
|
||||||
);
|
);
|
||||||
|
Loading…
Reference in New Issue
Block a user