Skip to content

Commit

Permalink
fix expiry date not being reset after receiving NotModified
Browse files Browse the repository at this point in the history
fix flatpak
  • Loading branch information
xou816 committed Mar 1, 2021
1 parent 2767b06 commit 87c0e36
Show file tree
Hide file tree
Showing 4 changed files with 50 additions and 53 deletions.
2 changes: 1 addition & 1 deletion dev.alextren.Spot.json
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@
"config-opts" : [
"-Doffline=true",
"-Dbuildtype=debug",
"-Dfeatures=''"
"-Dfeatures="
],
"sources" : [
{
Expand Down
10 changes: 7 additions & 3 deletions src/api/cache.rs
Original file line number Diff line number Diff line change
Expand Up @@ -249,14 +249,18 @@ impl CacheManager {
match file {
CacheFile::Fresh(buf, _) => Ok(buf),
CacheFile::Expired(buf, etag) => match fetch(etag).await? {
FetchResult::NotModified => Ok(buf),
FetchResult::NotModified(expiry) => {
let meta = self.cache_meta_path(resource);
self.set_expiry_for_path(&meta, expiry).await?;
Ok(buf)
}
FetchResult::Modified(fresh, expiry) => {
self.write_cache_file(resource, &fresh, expiry).await?;
Ok(fresh)
}
},
CacheFile::None => match fetch(None).await? {
FetchResult::NotModified => Err(E::from(CacheError::NoContent)),
FetchResult::NotModified(_) => Err(E::from(CacheError::NoContent)),
FetchResult::Modified(fresh, expiry) => {
self.write_cache_file(resource, &fresh, expiry).await?;
Ok(fresh)
Expand All @@ -267,6 +271,6 @@ impl CacheManager {
}

pub enum FetchResult {
NotModified,
NotModified(CacheExpiry),
Modified(Vec<u8>, CacheExpiry),
}
24 changes: 12 additions & 12 deletions src/api/cached_client.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ use std::future::Future;

use super::api_models::*;
use super::cache::{CacheExpiry, CacheManager, CachePolicy, FetchResult};
use super::client::{SpotifyApiError, SpotifyClient, SpotifyResponse};
use super::client::{SpotifyApiError, SpotifyClient, SpotifyResponse, SpotifyResponseKind};
use crate::app::models::*;

lazy_static! {
Expand Down Expand Up @@ -133,17 +133,17 @@ impl CachedSpotifyClient {
cache_policy.unwrap_or_else(|| self.default_cache_policy()),
move |etag| {
write(etag).map(|r| {
SpotifyResult::Ok(match r? {
SpotifyResponse::Ok {
content,
max_age,
etag,
..
} => FetchResult::Modified(
content.into_bytes(),
CacheExpiry::expire_in_seconds(u64::max(max_age, 60), etag),
),
SpotifyResponse::NotModified => FetchResult::NotModified,
let SpotifyResponse {
kind,
max_age,
etag,
} = r?;
let expiry = CacheExpiry::expire_in_seconds(u64::max(max_age, 60), etag);
SpotifyResult::Ok(match kind {
SpotifyResponseKind::Ok(content, _) => {
FetchResult::Modified(content.into_bytes(), expiry)
}
SpotifyResponseKind::NotModified => FetchResult::NotModified(expiry),
})
})
},
Expand Down
67 changes: 30 additions & 37 deletions src/api/client.rs
Original file line number Diff line number Diff line change
Expand Up @@ -89,33 +89,23 @@ where
}
}

pub(crate) enum SpotifyResponse<T> {
Ok {
content: String,
max_age: u64,
etag: Option<String>,
_type: PhantomData<T>,
},
pub(crate) enum SpotifyResponseKind<T> {
Ok(String, PhantomData<T>),
NotModified,
}

impl<T> SpotifyResponse<T> {
pub(crate) fn new(content: String, max_age: u64, etag: Option<String>) -> Self {
Self::Ok {
content,
max_age,
etag,
_type: PhantomData,
}
}
pub(crate) struct SpotifyResponse<T> {
pub kind: SpotifyResponseKind<T>,
pub max_age: u64,
pub etag: Option<String>,
}

impl<'a, T> SpotifyResponse<T>
where
T: Deserialize<'a>,
{
pub(crate) fn deserialize(&'a self) -> Option<T> {
if let Self::Ok { ref content, .. } = self {
if let SpotifyResponseKind::Ok(ref content, _) = self.kind {
from_str(content).ok()
} else {
None
Expand Down Expand Up @@ -198,28 +188,31 @@ impl SpotifyClient {
B: Into<isahc::AsyncBody>,
{
let mut result = self.client.send_async(request).await?;

let etag = result
.headers()
.get("etag")
.and_then(|header| header.to_str().ok())
.map(|s| s.to_owned());

let cache_control = result
.headers()
.get("cache-control")
.and_then(|header| header.to_str().ok())
.and_then(|s| Self::parse_cache_control(s));

match result.status() {
StatusCode::UNAUTHORIZED => Err(SpotifyApiError::InvalidToken),
s if s.is_success() => {
let etag = result
.headers()
.get("etag")
.and_then(|header| header.to_str().ok())
.map(|s| s.to_owned());

let cache_control = result
.headers()
.get("cache-control")
.and_then(|header| header.to_str().ok())
.and_then(|s| Self::parse_cache_control(s));

Ok(SpotifyResponse::new(
result.text().await?,
cache_control.unwrap_or(10),
etag,
))
}
StatusCode::NOT_MODIFIED => Ok(SpotifyResponse::NotModified),
s if s.is_success() => Ok(SpotifyResponse {
kind: SpotifyResponseKind::Ok(result.text().await?, PhantomData),
max_age: cache_control.unwrap_or(10),
etag,
}),
StatusCode::NOT_MODIFIED => Ok(SpotifyResponse {
kind: SpotifyResponseKind::NotModified,
max_age: cache_control.unwrap_or(10),
etag,
}),
s => Err(SpotifyApiError::BadStatus(s.as_u16())),
}
}
Expand Down

0 comments on commit 87c0e36

Please sign in to comment.