Write metadata to file cache
Then, before a GET, read the metadata and add any etag to the headers and handle 304 in response.
This commit is contained in:
27
src/owner.rs
27
src/owner.rs
@@ -9,7 +9,10 @@ use log::{error, info};
|
||||
#[cfg(test)]
|
||||
use std::{println as info, println as error};
|
||||
|
||||
use crate::{cache::file_cache_dir, cache::update_file_cache, result::FFIResult};
|
||||
use crate::{
|
||||
cache::file_cache_dir, cache::update_file_cache, cache::update_file_caches,
|
||||
cache::update_metadata_file_cache, result::FFIResult,
|
||||
};
|
||||
|
||||
#[derive(Serialize, Deserialize, Debug)]
|
||||
pub struct Owner {
|
||||
@@ -69,12 +72,16 @@ pub extern "C" fn create_owner(
|
||||
.json(&owner)
|
||||
.send()?;
|
||||
info!("create owner response from api: {:?}", &resp);
|
||||
|
||||
let cache_dir = file_cache_dir(api_url)?;
|
||||
let headers = resp.headers().clone();
|
||||
let bytes = resp.bytes()?;
|
||||
let json: Owner = serde_json::from_slice(&bytes)?;
|
||||
if let Some(id) = json.id {
|
||||
update_file_cache(
|
||||
&file_cache_dir(api_url)?.join(format!("owner_{}.json", id)),
|
||||
&bytes,
|
||||
update_file_cache(&cache_dir.join(format!("owner_{}.json", id)), &bytes)?;
|
||||
update_metadata_file_cache(
|
||||
&cache_dir.join(format!("owner_{}_metadata.json", id)),
|
||||
&headers,
|
||||
)?;
|
||||
}
|
||||
Ok(json)
|
||||
@@ -144,14 +151,12 @@ pub extern "C" fn update_owner(
|
||||
.json(&owner)
|
||||
.send()?;
|
||||
info!("update owner response from api: {:?}", &resp);
|
||||
let bytes = resp.bytes()?;
|
||||
|
||||
let cache_dir = file_cache_dir(api_url)?;
|
||||
let body_cache_path = cache_dir.join(format!("owner_{}.json", id));
|
||||
let metadata_cache_path = cache_dir.join(format!("owner_{}_metadata.json", id));
|
||||
let bytes = update_file_caches(&body_cache_path, &metadata_cache_path, resp)?;
|
||||
let json: Owner = serde_json::from_slice(&bytes)?;
|
||||
if let Some(id) = json.id {
|
||||
update_file_cache(
|
||||
&file_cache_dir(api_url)?.join(format!("owner_{}.json", id)),
|
||||
&bytes,
|
||||
)?;
|
||||
}
|
||||
Ok(json)
|
||||
} else {
|
||||
Err(anyhow!("api-key not defined"))
|
||||
|
||||
Reference in New Issue
Block a user