Update all endpoints to accept and send bincode
This commit is contained in:
parent
6ac4b03a0a
commit
e0cc81c97e
@ -289,11 +289,17 @@ pub async fn delete(
|
|||||||
.map_err(reject_anyhow)?;
|
.map_err(reject_anyhow)?;
|
||||||
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.list_interior_ref_lists.clear().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(interior_ref_list.shop_id)
|
.delete_response(interior_ref_list.shop_id)
|
||||||
.await;
|
.await;
|
||||||
|
CACHES
|
||||||
|
.interior_ref_list_by_shop_id_bin
|
||||||
|
.delete_response(interior_ref_list.shop_id)
|
||||||
|
.await;
|
||||||
|
CACHES.list_interior_ref_lists.clear().await;
|
||||||
|
CACHES.list_interior_ref_lists_bin.clear().await;
|
||||||
});
|
});
|
||||||
Ok(StatusCode::NO_CONTENT)
|
Ok(StatusCode::NO_CONTENT)
|
||||||
}
|
}
|
||||||
|
@ -1,46 +1,79 @@
|
|||||||
use anyhow::Result;
|
use anyhow::Result;
|
||||||
use http::StatusCode;
|
use http::StatusCode;
|
||||||
use ipnetwork::IpNetwork;
|
use ipnetwork::IpNetwork;
|
||||||
|
use mime::Mime;
|
||||||
use std::net::SocketAddr;
|
use std::net::SocketAddr;
|
||||||
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, Owner};
|
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, DataReply, ETagReply, Json};
|
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,
|
||||||
.owner
|
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 owner = Owner::get(&env.db, id).await?;
|
let owner = Owner::get(&env.db, id).await?;
|
||||||
let reply = ETagReply::<Json>::from_serializable(&owner)?;
|
let reply = T::from_serializable(&owner)?;
|
||||||
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.owner_bin).await
|
||||||
|
}
|
||||||
|
_ => get::<ETagReply<Json>>(id, etag, env, &CACHES.owner).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_owners
|
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 owners = Owner::list(&env.db, &list_params).await?;
|
let owners = Owner::list(&env.db, &list_params).await?;
|
||||||
let reply = ETagReply::<Json>::from_serializable(&owners)?;
|
let reply = T::from_serializable(&owners)?;
|
||||||
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_owners_bin).await
|
||||||
|
}
|
||||||
|
_ => get::<ETagReply<Json>>(list_params, etag, env, &CACHES.list_owners).await,
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
pub async fn create(
|
pub async fn create(
|
||||||
@ -48,9 +81,16 @@ pub async fn create(
|
|||||||
remote_addr: Option<SocketAddr>,
|
remote_addr: Option<SocketAddr>,
|
||||||
api_key: Option<Uuid>,
|
api_key: Option<Uuid>,
|
||||||
real_ip: Option<IpNetwork>,
|
real_ip: Option<IpNetwork>,
|
||||||
|
content_type: Option<Mime>,
|
||||||
env: Environment,
|
env: Environment,
|
||||||
) -> Result<impl Reply, Rejection> {
|
) -> Result<impl Reply, Rejection> {
|
||||||
if let Some(api_key) = api_key {
|
async fn create<'a, T: DataReply + 'a>(
|
||||||
|
owner: Owner,
|
||||||
|
remote_addr: Option<SocketAddr>,
|
||||||
|
api_key: Uuid,
|
||||||
|
real_ip: Option<IpNetwork>,
|
||||||
|
env: Environment,
|
||||||
|
) -> Result<Box<dyn Reply + 'a>, Rejection> {
|
||||||
let owner_with_ip_and_key = match remote_addr {
|
let owner_with_ip_and_key = match remote_addr {
|
||||||
Some(addr) => Owner {
|
Some(addr) => Owner {
|
||||||
api_key: Some(api_key),
|
api_key: Some(api_key),
|
||||||
@ -68,13 +108,23 @@ 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 = ETagReply::<Json>::from_serializable(&saved_owner).map_err(reject_anyhow)?;
|
let reply = T::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 {
|
||||||
CACHES.list_owners.clear().await;
|
CACHES.list_owners.clear().await;
|
||||||
|
CACHES.list_owners_bin.clear().await;
|
||||||
});
|
});
|
||||||
Ok(reply)
|
Ok(Box::new(reply))
|
||||||
|
}
|
||||||
|
|
||||||
|
if let Some(api_key) = api_key {
|
||||||
|
match content_type {
|
||||||
|
Some(content_type) if content_type == mime::APPLICATION_OCTET_STREAM => {
|
||||||
|
create::<ETagReply<Bincode>>(owner, remote_addr, api_key, real_ip, env).await
|
||||||
|
}
|
||||||
|
_ => create::<ETagReply<Json>>(owner, remote_addr, api_key, real_ip, env).await,
|
||||||
|
}
|
||||||
} else {
|
} else {
|
||||||
Err(reject_anyhow(unauthorized_no_api_key()))
|
Err(reject_anyhow(unauthorized_no_api_key()))
|
||||||
}
|
}
|
||||||
@ -84,8 +134,15 @@ pub async fn update(
|
|||||||
id: i32,
|
id: i32,
|
||||||
owner: Owner,
|
owner: Owner,
|
||||||
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,
|
||||||
|
owner: Owner,
|
||||||
|
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 owner_with_id = Owner {
|
let owner_with_id = Owner {
|
||||||
id: Some(id),
|
id: Some(id),
|
||||||
@ -96,14 +153,24 @@ 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 = ETagReply::<Json>::from_serializable(&updated_owner).map_err(reject_anyhow)?;
|
let reply = T::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 {
|
||||||
CACHES.owner.delete_response(id).await;
|
CACHES.owner.delete_response(id).await;
|
||||||
|
CACHES.owner_bin.delete_response(id).await;
|
||||||
CACHES.list_owners.clear().await;
|
CACHES.list_owners.clear().await;
|
||||||
|
CACHES.list_owners_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, owner, api_key, env).await
|
||||||
|
}
|
||||||
|
_ => update::<ETagReply<Json>>(id, owner, api_key, env).await,
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
pub async fn delete(
|
pub async fn delete(
|
||||||
@ -116,12 +183,12 @@ pub async fn delete(
|
|||||||
.await
|
.await
|
||||||
.map_err(reject_anyhow)?;
|
.map_err(reject_anyhow)?;
|
||||||
tokio::spawn(async move {
|
tokio::spawn(async move {
|
||||||
|
let api_key = api_key.expect("api-key has been validated during authenticate");
|
||||||
CACHES.owner.delete_response(id).await;
|
CACHES.owner.delete_response(id).await;
|
||||||
CACHES
|
CACHES.owner_bin.delete_response(id).await;
|
||||||
.owner_ids_by_api_key
|
CACHES.owner_ids_by_api_key.delete(api_key).await;
|
||||||
.delete(api_key.expect("api-key has been validated during authenticate"))
|
|
||||||
.await;
|
|
||||||
CACHES.list_owners.clear().await;
|
CACHES.list_owners.clear().await;
|
||||||
|
CACHES.list_owners_bin.clear().await;
|
||||||
});
|
});
|
||||||
Ok(StatusCode::NO_CONTENT)
|
Ok(StatusCode::NO_CONTENT)
|
||||||
}
|
}
|
||||||
|
@ -1,51 +1,90 @@
|
|||||||
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, MerchandiseList, Shop};
|
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, DataReply, ETagReply, Json};
|
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,
|
||||||
.shop
|
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 shop = Shop::get(&env.db, id).await?;
|
let shop = Shop::get(&env.db, id).await?;
|
||||||
let reply = ETagReply::<Json>::from_serializable(&shop)?;
|
let reply = T::from_serializable(&shop)?;
|
||||||
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.shop_bin).await
|
||||||
|
}
|
||||||
|
_ => get::<ETagReply<Json>>(id, etag, env, &CACHES.shop).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_shops
|
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 shops = Shop::list(&env.db, &list_params).await?;
|
let shops = Shop::list(&env.db, &list_params).await?;
|
||||||
let reply = ETagReply::<Json>::from_serializable(&shops)?;
|
let reply = T::from_serializable(&shops)?;
|
||||||
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_shops_bin).await
|
||||||
|
}
|
||||||
|
_ => get::<ETagReply<Json>>(list_params, etag, env, &CACHES.list_shops).await,
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
pub async fn create(
|
pub async fn create(
|
||||||
shop: Shop,
|
shop: Shop,
|
||||||
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>(
|
||||||
|
shop: Shop,
|
||||||
|
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 shop_with_owner_id = Shop {
|
let shop_with_owner_id = Shop {
|
||||||
owner_id: Some(owner_id),
|
owner_id: Some(owner_id),
|
||||||
@ -57,6 +96,7 @@ pub async fn create(
|
|||||||
.map_err(reject_anyhow)?;
|
.map_err(reject_anyhow)?;
|
||||||
|
|
||||||
// also save empty interior_ref_list and merchandise_list rows
|
// also save empty interior_ref_list and merchandise_list rows
|
||||||
|
// TODO: do this in a transaction with shop.create
|
||||||
if let Some(shop_id) = saved_shop.id {
|
if let Some(shop_id) = saved_shop.id {
|
||||||
let interior_ref_list = InteriorRefList {
|
let interior_ref_list = InteriorRefList {
|
||||||
id: None,
|
id: None,
|
||||||
@ -85,21 +125,37 @@ 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 = ETagReply::<Json>::from_serializable(&saved_shop).map_err(reject_anyhow)?;
|
let reply = T::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 {
|
||||||
CACHES.list_shops.clear().await;
|
CACHES.list_shops.clear().await;
|
||||||
|
CACHES.list_shops_bin.clear().await;
|
||||||
});
|
});
|
||||||
Ok(reply)
|
Ok(Box::new(reply))
|
||||||
|
}
|
||||||
|
|
||||||
|
match content_type {
|
||||||
|
Some(content_type) if content_type == mime::APPLICATION_OCTET_STREAM => {
|
||||||
|
create::<ETagReply<Bincode>>(shop, api_key, env).await
|
||||||
|
}
|
||||||
|
_ => create::<ETagReply<Json>>(shop, api_key, env).await,
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
pub async fn update(
|
pub async fn update(
|
||||||
id: i32,
|
id: i32,
|
||||||
shop: Shop,
|
shop: Shop,
|
||||||
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,
|
||||||
|
shop: Shop,
|
||||||
|
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 shop_with_id_and_owner_id = if shop.owner_id.is_some() {
|
let shop_with_id_and_owner_id = if shop.owner_id.is_some() {
|
||||||
// allows an owner to transfer ownership of shop to another owner
|
// allows an owner to transfer ownership of shop to another owner
|
||||||
@ -119,14 +175,24 @@ 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 = ETagReply::<Json>::from_serializable(&updated_shop).map_err(reject_anyhow)?;
|
let reply = T::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 {
|
||||||
CACHES.shop.delete_response(id).await;
|
CACHES.shop.delete_response(id).await;
|
||||||
|
CACHES.shop_bin.delete_response(id).await;
|
||||||
CACHES.list_shops.clear().await;
|
CACHES.list_shops.clear().await;
|
||||||
|
CACHES.list_shops_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, shop, api_key, env).await
|
||||||
|
}
|
||||||
|
_ => update::<ETagReply<Json>>(id, shop, api_key, env).await,
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
pub async fn delete(
|
pub async fn delete(
|
||||||
@ -140,12 +206,22 @@ pub async fn delete(
|
|||||||
.map_err(reject_anyhow)?;
|
.map_err(reject_anyhow)?;
|
||||||
tokio::spawn(async move {
|
tokio::spawn(async move {
|
||||||
CACHES.shop.delete_response(id).await;
|
CACHES.shop.delete_response(id).await;
|
||||||
|
CACHES.shop_bin.delete_response(id).await;
|
||||||
CACHES.list_shops.clear().await;
|
CACHES.list_shops.clear().await;
|
||||||
|
CACHES.list_shops_bin.clear().await;
|
||||||
CACHES
|
CACHES
|
||||||
.interior_ref_list_by_shop_id
|
.interior_ref_list_by_shop_id
|
||||||
.delete_response(id)
|
.delete_response(id)
|
||||||
.await;
|
.await;
|
||||||
|
CACHES
|
||||||
|
.interior_ref_list_by_shop_id_bin
|
||||||
|
.delete_response(id)
|
||||||
|
.await;
|
||||||
CACHES.merchandise_list_by_shop_id.delete_response(id).await;
|
CACHES.merchandise_list_by_shop_id.delete_response(id).await;
|
||||||
|
CACHES
|
||||||
|
.merchandise_list_by_shop_id_bin
|
||||||
|
.delete_response(id)
|
||||||
|
.await;
|
||||||
});
|
});
|
||||||
Ok(StatusCode::NO_CONTENT)
|
Ok(StatusCode::NO_CONTENT)
|
||||||
}
|
}
|
||||||
|
@ -1,69 +1,140 @@
|
|||||||
use anyhow::{anyhow, Result};
|
use anyhow::{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, Transaction};
|
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, DataReply, ETagReply, Json};
|
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,
|
||||||
.transaction
|
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 transaction = Transaction::get(&env.db, id).await?;
|
let transaction = Transaction::get(&env.db, id).await?;
|
||||||
let reply = ETagReply::<Json>::from_serializable(&transaction)?;
|
let reply = T::from_serializable(&transaction)?;
|
||||||
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.transaction_bin).await
|
||||||
|
}
|
||||||
|
_ => get::<ETagReply<Json>>(id, etag, env, &CACHES.transaction).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_transactions
|
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 transactions = Transaction::list(&env.db, &list_params).await?;
|
let transactions = Transaction::list(&env.db, &list_params).await?;
|
||||||
let reply = ETagReply::<Json>::from_serializable(&transactions)?;
|
let reply = T::from_serializable(&transactions)?;
|
||||||
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_transactions_bin).await
|
||||||
|
}
|
||||||
|
_ => get::<ETagReply<Json>>(list_params, etag, env, &CACHES.list_transactions).await,
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
pub async fn list_by_shop_id(
|
pub async fn list_by_shop_id(
|
||||||
shop_id: i32,
|
shop_id: i32,
|
||||||
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_transactions_by_shop_id
|
shop_id: i32,
|
||||||
|
list_params: ListParams,
|
||||||
|
etag: Option<String>,
|
||||||
|
env: Environment,
|
||||||
|
cache: &'static Cache<(i32, ListParams), CachedResponse>,
|
||||||
|
) -> Result<Box<dyn Reply>, Rejection> {
|
||||||
|
let response = cache
|
||||||
.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 =
|
||||||
let reply = ETagReply::<Json>::from_serializable(&transactions)?;
|
Transaction::list_by_shop_id(&env.db, shop_id, &list_params).await?;
|
||||||
|
let reply = T::from_serializable(&transactions)?;
|
||||||
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,
|
||||||
|
list_params,
|
||||||
|
etag,
|
||||||
|
env,
|
||||||
|
&CACHES.list_transactions_by_shop_id_bin,
|
||||||
|
)
|
||||||
|
.await
|
||||||
|
}
|
||||||
|
_ => {
|
||||||
|
get::<ETagReply<Json>>(
|
||||||
|
shop_id,
|
||||||
|
list_params,
|
||||||
|
etag,
|
||||||
|
env,
|
||||||
|
&CACHES.list_transactions_by_shop_id,
|
||||||
|
)
|
||||||
|
.await
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
pub async fn create(
|
pub async fn create(
|
||||||
transaction: Transaction,
|
transaction: Transaction,
|
||||||
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>(
|
||||||
|
transaction: Transaction,
|
||||||
|
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 transaction_with_owner_id = Transaction {
|
let transaction_with_owner_id = Transaction {
|
||||||
owner_id: Some(owner_id),
|
owner_id: Some(owner_id),
|
||||||
@ -99,28 +170,40 @@ 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 = ETagReply::<Json>::from_serializable(&saved_transaction).map_err(reject_anyhow)?;
|
let reply = T::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 {
|
||||||
// TODO: will this make these caches effectively useless?
|
// TODO: will this make these caches effectively useless?
|
||||||
CACHES.list_transactions.clear().await;
|
let merch_id = updated_merchandise_list
|
||||||
CACHES.list_transactions_by_shop_id.clear().await;
|
|
||||||
CACHES
|
|
||||||
.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(merch_id).await;
|
||||||
.await;
|
CACHES.merchandise_list_bin.delete_response(merch_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_transactions.clear().await;
|
||||||
|
CACHES.list_transactions_bin.clear().await;
|
||||||
|
CACHES.list_transactions_by_shop_id.clear().await;
|
||||||
|
CACHES.list_transactions_by_shop_id_bin.clear().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 => {
|
||||||
|
create::<ETagReply<Bincode>>(transaction, api_key, env).await
|
||||||
|
}
|
||||||
|
_ => create::<ETagReply<Json>>(transaction, api_key, env).await,
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
pub async fn delete(
|
pub async fn delete(
|
||||||
@ -134,8 +217,11 @@ pub async fn delete(
|
|||||||
.map_err(reject_anyhow)?;
|
.map_err(reject_anyhow)?;
|
||||||
tokio::spawn(async move {
|
tokio::spawn(async move {
|
||||||
CACHES.transaction.delete_response(id).await;
|
CACHES.transaction.delete_response(id).await;
|
||||||
|
CACHES.transaction_bin.delete_response(id).await;
|
||||||
CACHES.list_transactions.clear().await;
|
CACHES.list_transactions.clear().await;
|
||||||
|
CACHES.list_transactions_bin.clear().await;
|
||||||
CACHES.list_transactions_by_shop_id.clear().await;
|
CACHES.list_transactions_by_shop_id.clear().await;
|
||||||
|
CACHES.list_transactions_by_shop_id_bin.clear().await;
|
||||||
});
|
});
|
||||||
Ok(StatusCode::NO_CONTENT)
|
Ok(StatusCode::NO_CONTENT)
|
||||||
}
|
}
|
||||||
|
12
src/main.rs
12
src/main.rs
@ -82,6 +82,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::owner::get),
|
.and_then(handlers::owner::get),
|
||||||
);
|
);
|
||||||
@ -92,6 +93,7 @@ async fn main() -> Result<()> {
|
|||||||
.and(warp::addr::remote())
|
.and(warp::addr::remote())
|
||||||
.and(warp::header::optional("api-key"))
|
.and(warp::header::optional("api-key"))
|
||||||
.and(warp::header::optional("x-real-ip"))
|
.and(warp::header::optional("x-real-ip"))
|
||||||
|
.and(warp::header::optional("content-type"))
|
||||||
.and(with_env(env.clone()))
|
.and(with_env(env.clone()))
|
||||||
.and_then(handlers::owner::create),
|
.and_then(handlers::owner::create),
|
||||||
);
|
);
|
||||||
@ -109,6 +111,7 @@ async fn main() -> Result<()> {
|
|||||||
.and(warp::patch())
|
.and(warp::patch())
|
||||||
.and(json_body::<Owner>())
|
.and(json_body::<Owner>())
|
||||||
.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::owner::update),
|
.and_then(handlers::owner::update),
|
||||||
);
|
);
|
||||||
@ -117,6 +120,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::owner::list),
|
.and_then(handlers::owner::list),
|
||||||
);
|
);
|
||||||
@ -125,6 +129,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::shop::get),
|
.and_then(handlers::shop::get),
|
||||||
);
|
);
|
||||||
@ -133,6 +138,7 @@ async fn main() -> Result<()> {
|
|||||||
.and(warp::post())
|
.and(warp::post())
|
||||||
.and(json_body::<Shop>())
|
.and(json_body::<Shop>())
|
||||||
.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::shop::create),
|
.and_then(handlers::shop::create),
|
||||||
);
|
);
|
||||||
@ -150,6 +156,7 @@ async fn main() -> Result<()> {
|
|||||||
.and(warp::patch())
|
.and(warp::patch())
|
||||||
.and(json_body::<Shop>())
|
.and(json_body::<Shop>())
|
||||||
.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::shop::update),
|
.and_then(handlers::shop::update),
|
||||||
);
|
);
|
||||||
@ -158,6 +165,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::shop::list),
|
.and_then(handlers::shop::list),
|
||||||
);
|
);
|
||||||
@ -298,6 +306,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::transaction::get),
|
.and_then(handlers::transaction::get),
|
||||||
);
|
);
|
||||||
@ -306,6 +315,7 @@ async fn main() -> Result<()> {
|
|||||||
.and(warp::post())
|
.and(warp::post())
|
||||||
.and(json_body::<Transaction>())
|
.and(json_body::<Transaction>())
|
||||||
.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::transaction::create),
|
.and_then(handlers::transaction::create),
|
||||||
);
|
);
|
||||||
@ -322,6 +332,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::transaction::list),
|
.and_then(handlers::transaction::list),
|
||||||
);
|
);
|
||||||
@ -332,6 +343,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::transaction::list_by_shop_id),
|
.and_then(handlers::transaction::list_by_shop_id),
|
||||||
);
|
);
|
||||||
|
Loading…
Reference in New Issue
Block a user