Add ETag headers to get/list endpoints

Now the client can opt out of receiving the whole JSON body if it hasn't changed since they last requested.

Right now, only `ETag` and `If-None-Match` headers are implemeted which isn't very RFC-spec compliant but it's all I need so I don't care.
This commit is contained in:
2020-11-02 20:22:12 -05:00
parent 4074ad0c97
commit 8cb76d6ff4
11 changed files with 215 additions and 66 deletions

View File

@@ -9,30 +9,38 @@ use crate::models::{InteriorRefList, ListParams, MerchandiseList, Shop};
use crate::problem::reject_anyhow;
use crate::Environment;
use super::authenticate;
use super::{authenticate, check_etag, JsonWithETag};
pub async fn get(id: i32, env: Environment) -> Result<impl Reply, Rejection> {
env.caches
pub async fn get(id: i32, etag: Option<String>, env: Environment) -> Result<impl Reply, Rejection> {
let response = env
.caches
.shop
.get_response(id, || async {
let shop = Shop::get(&env.db, id).await?;
let reply = json(&shop);
let reply = JsonWithETag::from_serializable(&shop)?;
let reply = with_status(reply, StatusCode::OK);
Ok(reply)
})
.await
.await?;
Ok(check_etag(etag, response))
}
pub async fn list(list_params: ListParams, env: Environment) -> Result<impl Reply, Rejection> {
env.caches
pub async fn list(
list_params: ListParams,
etag: Option<String>,
env: Environment,
) -> Result<impl Reply, Rejection> {
let response = env
.caches
.list_shops
.get_response(list_params.clone(), || async {
let shops = Shop::list(&env.db, &list_params).await?;
let reply = json(&shops);
let reply = JsonWithETag::from_serializable(&shops)?;
let reply = with_status(reply, StatusCode::OK);
Ok(reply)
})
.await
.await?;
Ok(check_etag(etag, response))
}
pub async fn create(