pub struct Client<T> { /* private fields */ }Expand description
A cache in front of a transport.
Implementations§
Source§impl<T: Transport> Client<T>
impl<T: Transport> Client<T>
Sourcepub fn new(transport: T, cache: Cache, mode: Mode) -> Self
pub fn new(transport: T, cache: Cache, mode: Mode) -> Self
A client over transport and cache, in mode.
Sourcepub fn fetch(
&self,
source: &Source,
url: &str,
now_s: u64,
) -> Result<Fetched, NetError>
pub fn fetch( &self, source: &Source, url: &str, now_s: u64, ) -> Result<Fetched, NetError>
The body at url from source, at time now_s (seconds since the Unix epoch).
A cached copy younger than source.ttl_s is returned as Freshness::Cached without a
fetch. Otherwise, online, the transport is called and its body cached; if it fails, a stale
copy is returned as Freshness::Stale when there is one. Offline, the transport is never
called: any cached copy is returned, Freshness::Stale if it is past its TTL.
A copy dated after now_s is never fresh. Online, a cache entry that cannot be read is
treated as missing and overwritten by the fetch.
§Errors
NetError::NotCached offline with no copy; NetError::Transport online when the fetch
fails with no copy; cache errors as Cache::get and Cache::put give them.
Sourcepub fn fetch_checked(
&self,
source: &Source,
url: &str,
now_s: u64,
check: impl Fn(&[u8]) -> Result<(), String>,
) -> Result<Fetched, NetError>
pub fn fetch_checked( &self, source: &Source, url: &str, now_s: u64, check: impl Fn(&[u8]) -> Result<(), String>, ) -> Result<Fetched, NetError>
As Client::fetch, but only a body that check accepts is cached or returned: a data
source passes its parser, so an answer it can’t read (an error page served as 200, a
forecast with its hours still empty) is never kept in place of a good copy.
Online, a fetched body the check refuses is not cached; a stale copy is returned in its place, with the reason, when there is one. A cached copy the check refuses counts as missing online, and is the error offline.
§Errors
As Client::fetch, and NetError::Refused when the check refuses the only body there
is.