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

@@ -10,30 +10,38 @@ use crate::models::{ListParams, Owner};
use crate::problem::{reject_anyhow, unauthorized_no_api_key};
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
.owner
.get_response(id, || async {
let owner = Owner::get(&env.db, id).await?;
let reply = json(&owner);
let reply = JsonWithETag::from_serializable(&owner)?;
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_owners
.get_response(list_params.clone(), || async {
let owners = Owner::list(&env.db, &list_params).await?;
let reply = json(&owners);
let reply = JsonWithETag::from_serializable(&owners)?;
let reply = with_status(reply, StatusCode::OK);
Ok(reply)
})
.await
.await?;
Ok(check_etag(etag, response))
}
pub async fn create(